Reputation: 1507
Is there a way to configure eclipse to tag an == comparison on String objects as Errors? I just want a way to prevent developers from using == to compare strings accidentally or unknowingly. I searched through eclipse and could not find anything. Any ideas.
Upvotes: 4
Views: 136
Reputation: 672
Yes you can add checkstyle in eclipse which is able to display "==" for String as an error
Window -> Preferences -> Checkstyle (download plug-in here)
Then you can create a new Checkstyle.
Upvotes: 2
Reputation: 80603
There is nothing built into Eclipse that you can enable to do this check, afaik. But, you can incorporate a third party static analysis tool to do it for you. FindBugs does have a rule to check for exactly what you've described, and can be incorporated into Eclipse via plugin.
Other static analysis tools you might want to take a look at include Checkstyle and PMD.
Upvotes: 3
Reputation: 4036
Use PMD or Findbugs plugins for eclipse to do this. These tools will also let you catch other common programming mistakes.
Direct link to findbug rule to detect this situation: http://findbugs.sourceforge.net/bugDescriptions.html#ES_COMPARING_STRINGS_WITH_EQ
Upvotes: 3