diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 48783b1fc0..759d696d12 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -67,6 +67,8 @@ See [the example report]({{ baseurl }}report-examples/cpdhtml-v2.html). * [#4063](https://github.com/pmd/pmd/issues/4063): \[java] AvoidBranchingStatementAsLastInLoop: False-negative about try/finally block * [#4457](https://github.com/pmd/pmd/issues/4457): \[java] OverrideBothEqualsAndHashcode: false negative with anonymous classes * [#4546](https://github.com/pmd/pmd/issues/4546): \[java] OverrideBothEqualsAndHashCode ignores records +* java-performance + * [#4458](https://github.com/pmd/pmd/issues/4458): \[java] RedundantFieldInitializer: false positive with lombok's @Value #### API Changes @@ -98,6 +100,7 @@ See [the example report]({{ baseurl }}report-examples/cpdhtml-v2.html). * [#4537](https://github.com/pmd/pmd/pull/4537): \[java] Fix #4455: A false positive about FieldNamingConventions and UtilityClass - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) * [#4538](https://github.com/pmd/pmd/pull/4538): \[java] Fix #4456: A false positive about FinalFieldCouldBeStatic and UtilityClass - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) * [#4540](https://github.com/pmd/pmd/pull/4540): \[java] Fix #4457: false negative about OverrideBothEqualsAndHashcode - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) +* [#4541](https://github.com/pmd/pmd/pull/4541): \[java] Fix #4458: A false positive about RedundantFieldInitializer and @Value - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) ### 🚀 Major Features and Enhancements @@ -603,6 +606,7 @@ Language specific fixes: * [#3486](https://github.com/pmd/pmd/pull/3486): \[java] InsufficientStringBufferDeclaration: Fix NPE * [#3848](https://github.com/pmd/pmd/issues/3848): \[java] StringInstantiation: false negative when using method result * [#4070](https://github.com/pmd/pmd/issues/4070): \[java] A false positive about the rule RedundantFieldInitializer + * [#4458](https://github.com/pmd/pmd/issues/4458): \[java] RedundantFieldInitializer: false positive with lombok's @Value * kotlin * [#419](https://github.com/pmd/pmd/issues/419): \[kotlin] Add support for Kotlin * [#4389](https://github.com/pmd/pmd/pull/4389): \[kotlin] Update grammar to version 1.8 @@ -643,6 +647,7 @@ Language specific fixes: * [#4537](https://github.com/pmd/pmd/pull/4537): \[java] Fix #4455: A false positive about FieldNamingConventions and UtilityClass - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) * [#4538](https://github.com/pmd/pmd/pull/4538): \[java] Fix #4456: A false positive about FinalFieldCouldBeStatic and UtilityClass - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) * [#4540](https://github.com/pmd/pmd/pull/4540): \[java] Fix #4457: false negative about OverrideBothEqualsAndHashcode - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) +* [#4541](https://github.com/pmd/pmd/pull/4541): \[java] Fix #4458: A false positive about RedundantFieldInitializer and @Value - [AnnaDev](https://github.com/LynnBroe) (@LynnBroe) ### 📈 Stats * 4557 commits diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/RedundantFieldInitializerRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/RedundantFieldInitializerRule.java index 066cdd929e..f274a185f9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/RedundantFieldInitializerRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/RedundantFieldInitializerRule.java @@ -4,6 +4,10 @@ package net.sourceforge.pmd.lang.java.rule.performance; +import static net.sourceforge.pmd.util.CollectionUtil.setOf; + +import java.util.Set; + import net.sourceforge.pmd.lang.java.ast.ASTExpression; import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; @@ -13,6 +17,9 @@ import net.sourceforge.pmd.lang.java.ast.JModifier; import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule; + + + /** * Detects redundant field initializers, i.e. the field initializer expressions * the JVM would assign by default. @@ -22,13 +29,18 @@ import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule; */ public class RedundantFieldInitializerRule extends AbstractJavaRulechainRule { + private static final Set MAKE_FIELD_FINAL_CLASS_ANNOT = + setOf( + "lombok.Value" + ); + public RedundantFieldInitializerRule() { super(ASTFieldDeclaration.class); } @Override public Object visit(ASTFieldDeclaration fieldDeclaration, Object data) { - if (!fieldDeclaration.hasModifiers(JModifier.FINAL)) { + if (!fieldDeclaration.hasModifiers(JModifier.FINAL) && !JavaAstUtils.hasAnyAnnotation(fieldDeclaration.getEnclosingType(), MAKE_FIELD_FINAL_CLASS_ANNOT)) { for (ASTVariableDeclaratorId varId : fieldDeclaration.getVarIds()) { ASTExpression init = varId.getInitializer(); if (init != null) { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/RedundantFieldInitializer.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/RedundantFieldInitializer.xml index abd2cd1723..279f98007a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/RedundantFieldInitializer.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/RedundantFieldInitializer.xml @@ -1481,4 +1481,16 @@ class O { } ]]> + + + [java] RedundantFieldInitializer: false positive with lombok's @Value #4458 + 0 + +