UnnecessaryNullCheckWithInstanceOf
Since Checkstyle 10.25.0
Description
Checks for redundant null checks with the instanceof operator.
The instanceof operator inherently returns false when the left operand is null, making explicit null checks redundant in boolean expressions with instanceof.
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="UnnecessaryNullCheckWithInstanceOf"/>
</module>
</module>
Examples of violations:
public class Example1 {
public void methodWithUnnecessaryNullCheck1(Object obj) {
// violation below, 'Unnecessary nullity check'
if (obj != null && obj instanceof String) {
String str = (String) obj;
}
if (obj instanceof String) {
String str = (String) obj;
}
// violation below, 'Unnecessary nullity check'
boolean isInvalid = obj != null && obj instanceof String;
boolean isValid = obj instanceof String;
}
interface Validator {
boolean validate(Object obj);
}
public void anonymousClassImplementation() {
Validator v = new Validator() {
@Override
public boolean validate(Object obj) {
// violation below, 'Unnecessary nullity check'
return obj != null && obj instanceof String;
}
};
}
private final List<Object> objects = new ArrayList<>();
public String basicTernary(Object obj) {
// violation below, 'Unnecessary nullity check'
return obj != null && obj instanceof String ? ((String) obj) : "";
}
public String basicValidTernary(Object obj) {
return obj instanceof String ? ((String) obj) : "";
}
public void methodWithValidNullCheck(Object obj) {
if (obj != null) {
CharSequence cs = (CharSequence) obj;
}
}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.coding