checkstyle / checkstyle

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. By default it supports the Google Java Style Guide and Sun Code Conventions, but is highly configurable. It can be invoked with an ANT task and a command line program.
https://checkstyle.org
GNU Lesser General Public License v2.1
8.23k stars 3.63k forks source link

EqualsAvoidNull confused when passing null to equals #55

Closed romani closed 10 years ago

romani commented 10 years ago

Created: 2009-06-21 Creator: Baron Roberts SF issue: 576

http://checkstyle.sourceforge.net/config_coding.html#EqualsAvoidNull

Code:

        String foo = "sdgs";
        boolean b = foo.equals(null);

Violation: "Equals Avoid Null: String literal expressions should be on the left side of an equals comparison."

Expected: no violation

isopov commented 10 years ago

equals(null) seems to be a bad style on its own - for me it is better to write "== null"

isopov commented 10 years ago

Moreover - there is only two possible outcomes of this code - "equals(null)" - false and NPE.

romani commented 10 years ago

yes it is not a very good but it it not different from example in Doc:

should be refactored to:

String nullString = null;
"My_Sweet_String".equals(nullString);

read it as ""My_Sweet_String".equals(null);"

so should not be a problem, just to be consistent in validation. @isopov , does it make sense ?

romani commented 10 years ago

After discussion, we decided to not implement this, as null is kind of Literal, and developer have to do variable == null.