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] FinalFieldCouldBeStatic FN when the right side of the assignment is a constant expression #2440

Closed
wuchiuwong opened this issue Apr 25, 2020 · 0 comments · Fixed by #4940
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Milestone

Comments

@wuchiuwong
Copy link

wuchiuwong commented Apr 25, 2020

Affects PMD Version:
6.22.0+
7.0.0

Rule: FinalFieldCouldBeStatic

Description:
FinalFieldCouldBeStatic can not detect that the right side of the assignment is not a literal, but an expression (such as: Math.PI * 0.5).
This rule is implemented through xpath search:

<![CDATA[
//FieldDeclaration
 [@Final='true' and @Static='false']
   /VariableDeclarator/VariableInitializer/Expression
    /PrimaryExpression[not(PrimarySuffix)]/PrimaryPrefix/Literal
]]>

Code Sample demonstrating the issue:

private final double HALF_PI = Math.PI * 0.5

Expected outcome:

false-negative

Running PMD through:
CLI

@wuchiuwong wuchiuwong added the a:bug PMD crashes or fails to analyse a file. label Apr 25, 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 Apr 25, 2020
@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 31, 2024
@jsotuyod jsotuyod changed the title [java]FinalFieldCouldBeStatic can not detect the case that the right side of the assignment is an expression [java] FinalFieldCouldBeStatic FN when the right side of the assignment is a constant expression Apr 7, 2024
jsotuyod added a commit to Monits/pmd that referenced this issue Apr 7, 2024
jsotuyod added a commit to Monits/pmd that referenced this issue Apr 7, 2024
@jsotuyod jsotuyod added the has:pr label Apr 7, 2024
@jsotuyod jsotuyod added this to the 7.1.0 milestone Apr 18, 2024
adangel added a commit that referenced this issue Apr 18, 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
Development

Successfully merging a pull request may close this issue.

3 participants