Skip to content

[java] FinalFieldCouldBeStatic FN when the right side of the assignment is a constant expression #2440

@wuchiuwong

Description

@wuchiuwong

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions