Merge pull request #4541 from LynnBroe:issue4458

[java] Fix #4458: A false positive about RedundantFieldInitializer and @Value #4541
This commit is contained in:
Andreas Dangel
2023-05-28 12:09:41 +02:00
3 changed files with 30 additions and 1 deletions

View File

@ -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

View File

@ -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<String> 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) {

View File

@ -1481,4 +1481,16 @@ class O {
}
]]></code>
</test-code>
<test-code>
<description>[java] RedundantFieldInitializer: false positive with lombok's @Value #4458</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import lombok.Value;
@Value
public class Test {
String bar = null; // false positive warning
}
]]></code>
</test-code>
</test-data>