rspec/rules/S3026/java/rule.adoc

43 lines
898 B
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
There's no need to null-test a variable before an ``++instanceof++`` test because ``++instanceof++`` tests for ``++null++``. Similarly, there's no need to null-test a variable before dereferencing _some other_ object.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
if (myVar != null && myVar instanceof MyClass) { // Noncompliant
// ...
} else if (myVar != null && myOtherVar.equals(myVar) { // Noncompliant
// ...
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
if (myVar instanceof MyClass) {
// ...
} else if (myVar != null && myVar.equals(myOtherVar) {
// ...
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Remove the null-test of "xxx"; it is redundant.
* Remove the null-test of "xxx" or use its ".equals" method instead of "yyy's".
endif::env-github,rspecator-view[]