rspec/rules/S1905/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

70 lines
1.0 KiB
Plaintext

== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
[source,java]
----
public void example() {
for (Foo obj : (List<Foo>) getFoos()) { // Noncompliant; cast unnecessary because List<Foo> is what's returned
//...
}
}
public List<Foo> getFoos() {
return this.foos;
}
----
=== Compliant solution
[source,java]
----
public void example() {
for (Foo obj : getFoos()) {
//...
}
}
public List<Foo> getFoos() {
return this.foos;
}
----
=== Exceptions
Casting may be required to distinguish the method to call in the case of overloading:
[source,java]
----
class A {}
class B extends A{}
class C {
void fun(A a){}
void fun(B b){}
void foo() {
B b = new B();
fun(b);
fun((A) b); //call the first method so cast is not redundant.
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]