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] AvoidLiteralsInIfCondition false positive and negative for String literals when ignoreExpressions=true #4514

Closed
adangel opened this issue Apr 27, 2023 · 0 comments · Fixed by #4501
Labels
a:false-negative PMD doesn't flag a problematic piece of code a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@adangel
Copy link
Member

adangel commented Apr 27, 2023

Affects PMD Version: 6.x

Rule: AvoidLiteralsInIfCondition

Description:

String literals are reported for deeper expressions when the property ignoreExpressions is true (that's the default value).

Note: This is fixed already with PMD 7.0.0-rc1.
Found via #3123 .

Code Sample demonstrating the issue:

public class AvoidLiteralsInIfCondition {
    private static final int MY_CONSTANT = 1;
    public void test(int i, String s) {
        if ("test".equals(s)) {}      // expected violation is missing (false negative)
        if (i == 1) {}                // expected violation

        // the following literals should be ignored because ignoreExpression=true
        if (i == MY_CONSTANT && "test".equals(s)) {} // violation for "test" (false positive)
        if (i == 1 && "test".equals(s)) {}           // violation for "test" (false positive)
    }
}

Expected outcome:

PMD reports a violation at lines 8+9, but that's wrong. That's a false positive.
PMD doesn't report a violation at line 4, but that's wrong. That's a false negative.

@adangel adangel added the a:false-positive PMD flags a piece of code that is not problematic label Apr 27, 2023
@adangel adangel added this to the 7.0.0 milestone Apr 27, 2023
@adangel adangel added the a:false-negative PMD doesn't flag a problematic piece of code label Apr 27, 2023
@adangel adangel changed the title [java] AvoidLiteralsInIfCondition false positive for String literals when ignoreExpressions=true [java] AvoidLiteralsInIfCondition false positive and negative for String literals when ignoreExpressions=true Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code 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.

1 participant