Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EqualsAvoidNull confused when passing null to equals #55

Closed
romani opened this issue Nov 12, 2013 · 4 comments
Closed

EqualsAvoidNull confused when passing null to equals #55

romani opened this issue Nov 12, 2013 · 4 comments
Labels

Comments

@romani
Copy link
Member

romani commented Nov 12, 2013

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
Copy link
Contributor

isopov commented Nov 12, 2013

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

@isopov
Copy link
Contributor

isopov commented Nov 12, 2013

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

@romani
Copy link
Member Author

romani commented Nov 12, 2013

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
Copy link
Member Author

romani commented Nov 12, 2013

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

@romani romani closed this as completed Nov 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants