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

[java] CompareObjectsWithEqualsRule: False positive with Enums #2716

Closed
phoenix384 opened this issue Aug 19, 2020 · 4 comments · Fixed by #2899
Closed

[java] CompareObjectsWithEqualsRule: False positive with Enums #2716

phoenix384 opened this issue Aug 19, 2020 · 4 comments · Fixed by #2899
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@phoenix384
Copy link

phoenix384 commented Aug 19, 2020

Affects PMD Version:
6.26.0
Rule:
CompareObjectsWithEquals

Description:
With #885 this rule was fixed to not show a false-positive violation when comparing enums with '=='.
However there is still a way to get a false-positive.

Code Sample demonstrating the issue:

public class EnumTest {
	enum Type {
		A, B;
	}
	
	private final Type type = Type.A;
	
	public String isTypeA(Type param) {
		return param == type ? "Yes" : "No";
	}
}

Expected outcome:
No violation -> false-positive

Running PMD through: Eclipse

@phoenix384 phoenix384 added the a:bug PMD crashes or fails to analyse a file. label Aug 19, 2020
@oowekyala oowekyala added a:false-negative PMD doesn't flag a problematic piece of code and removed a:bug PMD crashes or fails to analyse a file. labels Aug 19, 2020
@adangel adangel added a:false-positive PMD flags a piece of code that is not problematic and removed a:false-negative PMD doesn't flag a problematic piece of code labels Aug 21, 2020
@oowekyala oowekyala added this to the 7.0.0 milestone Nov 5, 2020
oowekyala added a commit to oowekyala/pmd that referenced this issue Nov 5, 2020
@oowekyala oowekyala linked a pull request Nov 10, 2020 that will close this issue
5 tasks
@oowekyala oowekyala removed a link to a pull request Nov 10, 2020
5 tasks
@oowekyala oowekyala linked a pull request Nov 10, 2020 that will close this issue
5 tasks
@adangel
Copy link
Member

adangel commented Dec 11, 2020

Fixed via #2899 for PMD 7.

@adangel adangel closed this as completed Dec 11, 2020
@uhafner
Copy link

uhafner commented Feb 22, 2021

I also get a lot of false positives for this rule when upgrading from 6.29.0 to 6.30.0 or 6.31.0:
https://github.com/jenkinsci/warnings-ng-plugin/pull/774/checks?check_run_id=1804343680

What are the plans for 7.0.0? The backlog looks quite large before we can expect a new major release.

@adangel
Copy link
Member

adangel commented Feb 25, 2021

What are the plans for 7.0.0? The backlog looks quite large before we can expect a new major release.

That indeed will still take a while. But we are open to fix/backport issues that are already fixed for pmd 7, if there is a demand. We also accept pull requests.

I've looked at this issue again and that's my conclusion:

  • This bug here is not reproducible with 6.26.0 - if PMD is used on a compiled project. Then we correctly detect the Enum and don't report it.
  • The OP mentioned, he ran PMD through Eclipse. It could be a pmd-eclipse-plugin specific issue, however: I could only reproduce the problem, if I analyse the project without compiling it.
  • When the project is not compiled, we actually figure out, that it is a enum since 6.30.0 and don't report it. So, basically, this issue is fixed since 6.30.0
  • I'll add a test case for this specific case (the same we use in pmd 7) - there is no specific fix for this issue - it already works.

Then I looked at your false positives:
There are 12 new violations found with "CompareObjectsWithEquals" and one new violations with "LiteralsFirstInComparisons".
The CompareObjectsWithEquals are from two different files:

The one new violation with "LiteralsFirstInComparisons" appears here: https://github.com/jenkinsci/warnings-ng-plugin/blob/9869a3bff1f20cfc593c2bfe01bacdc55c7dc625/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java#L94
There you could indeed write string -> KEY.equals(string) ? KEY : "tooltip" and your lambda is null-safe.

@uhafner
Copy link

uhafner commented Feb 25, 2021

Thanks for clarifying! You are right, the class actually is not an enum it is just used in the same way. It seems that I forgot about it since I haven't looked into that part of my code since a long time...

oowekyala added a commit to oowekyala/pmd that referenced this issue Jul 17, 2022
Refs pmd#2716 - langs aren't declared in java anymore
adangel added a commit that referenced this issue Jan 13, 2023
- #2716 has been done with PMD 6.32.0 already
- #2528 was listed twice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants