diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 953a3a05e1..dda829b7eb 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -25,6 +25,8 @@ This is a {{ site.pmd.release_type }} release. * java-bestpractices * [#3209](https://github.com/pmd/pmd/issues/3209): \[java] UnusedPrivateMethod false positive with static method and cast expression * [#3468](https://github.com/pmd/pmd/issues/3468): \[java] UnusedPrivateMethod false positive when outer class calls private static method on inner class +* java-design + * [#3679](https://github.com/pmd/pmd/issues/3679): \[java] Make FinalFieldCouldBeStatic detect constant variable * java-errorprone * [#3686](https://github.com/pmd/pmd/issues/3686): \[java] ReturnEmptyCollectionRatherThanNull - false negative with conditioned returns * java-performance @@ -41,6 +43,7 @@ This is a {{ site.pmd.release_type }} release. * [#3704](https://github.com/pmd/pmd/pull/3704): \[java] Fix for #3686 - Fix ReturnEmptyCollectionRatherThanNull - [Oleksii Dykov](https://github.com/dykov) * [#3713](https://github.com/pmd/pmd/pull/3713): \[java] Enhance UnnecessaryModifier to support records - [Vincent Galloy](https://github.com/vgalloy) * [#3719](https://github.com/pmd/pmd/pull/3719): \[java] Upgrade log4j to 2.17.1 - [Daniel Paul Searles](https://github.com/squaresurf) +* [#3724](https://github.com/pmd/pmd/pull/3724): \[java] Fix for #3686 - fix FinalFieldCouldBeStatic - [Oleksii Dykov](https://github.com/dykov) {% endtocmaker %} diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index d1e326ceb3..4ea7bf514e 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -808,7 +808,9 @@ in each object at runtime. [not(preceding-sibling::Annotation/MarkerAnnotation/Name[@Image="Builder.Default"] and //ImportDeclaration/Name[@Image="lombok.Builder"])] /VariableDeclarator - [VariableInitializer/Expression/PrimaryExpression[not(PrimarySuffix)]/PrimaryPrefix/Literal] + [VariableInitializer/Expression/PrimaryExpression[not(PrimarySuffix)] + /PrimaryPrefix/*[self::Literal or self::Name] + ] /VariableDeclaratorId ]]> diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml index ebd108cc17..7611e5fc43 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/FinalFieldCouldBeStatic.xml @@ -149,6 +149,28 @@ package com.example; public class ExampleClass { private final String one = "one", two = "two"; +} + ]]> + + + + #3679 - False-negative on initializing with another object + 1 + 3 + + + + + [OK] initializing with a method invocation + 0 +