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] SingularField ignores static variables #3694

Closed
ghost opened this issue Dec 20, 2021 · 3 comments · Fixed by #4535
Closed

[java] SingularField ignores static variables #3694

ghost opened this issue Dec 20, 2021 · 3 comments · Fixed by #4535
Assignees
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Milestone

Comments

@ghost
Copy link

ghost commented Dec 20, 2021

Affects PMD Version:
6.41.0
7.0.0

Rule:SingularField

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_design.html#singularfield

Description:
Hi, I found two false negatives about the rule SingularField. Please refer to the following case, PMD should have reported a false negative at line 2 and 3. I think these cases are caused by modifiers of var1 and var2.

Code Sample demonstrating the issue:

class Impl {
  final private int var1 = 10;  // should report a warning here
  static private int var2 = 10; // also should report a warning here
  public int foo() {
    return var1;
  }
  public int foo(int y) {
    var2 = y   5;
    return var2;
  }
}

Expected outcome:

PMD should report violations at line 2 and 3, but doesn't. This is a false-negative.

Running PMD through: [Maven]

@ghost ghost added the a:false-negative PMD doesn't flag a problematic piece of code label Dec 20, 2021
@jsotuyod

This comment was marked as outdated.

@oowekyala oowekyala added this to the 7.0.0 milestone Apr 24, 2023
@adangel adangel reopened this Apr 28, 2023
@adangel
Copy link
Member

adangel commented Apr 28, 2023

Actually, this is not fixed yet. The following test case still doesn't report any violations:

    <test-code>
        <description>[java] Make SingularField can detect modified variables #3694</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>2,3</expected-linenumbers>
        <code><![CDATA[
class Impl {
  final private final int var1 = 10;  // should report a warning here
  static private int var2 = 10; // also should report a warning here
  public int foo() {
    return var1;
  }
  public int foo(int y) {
    var2 = y   5;
    return var2;
  }
}
]]></code>
    </test-code>

@adangel adangel removed this from the 7.0.0 milestone Apr 28, 2023
@oowekyala
Copy link
Member

oowekyala commented Apr 28, 2023

The rule doesn't check static or final fields by design, although that could probably be relaxed. The issue is probably misnamed.

@oowekyala oowekyala self-assigned this May 2, 2023
oowekyala added a commit to oowekyala/pmd that referenced this issue May 3, 2023
@oowekyala oowekyala changed the title [java] Make SingularField can detect modified variables [java] SingularField ignores static variables May 3, 2023
@oowekyala oowekyala added this to the 7.0.0 milestone May 3, 2023
@adangel adangel modified the milestones: 7.0.0, 7.x Feb 27, 2024
@jsotuyod jsotuyod added the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Mar 17, 2024
@jsotuyod jsotuyod removed the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Mar 26, 2024
@adangel adangel modified the milestones: 7.x, 7.0.1 Apr 3, 2024
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
Projects
None yet
3 participants