From daf574a888dc65fcc955c828b06f9eeda48bba9d Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 27 Apr 2023 19:59:24 +0200 Subject: [PATCH] [java] AvoidLiteralsInIfCondition - add test case for #4514 Closes #4514 --- docs/pages/release_notes.md | 1 + .../resources/category/java/errorprone.xml | 1 + .../xml/AvoidLiteralsInIfCondition.xml | 32 +++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 5912590fc1..2257018d08 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -505,6 +505,7 @@ Language specific fixes: * [#4493](https://github.com/pmd/pmd/issues/4493): \[java] MissingStaticMethodInNonInstantiatableClass: false-positive about @Inject * [#4505](https://github.com/pmd/pmd/issues/4505): \[java] ImplicitSwitchFallThrough NPE in PMD 7.0.0-rc1 * [#4513](https://github.com/pmd/pmd/issues/4513): \[java] UselessOperationOnImmutable various false negatives with String + * [#4514](https://github.com/pmd/pmd/issues/4514): \[java] AvoidLiteralsInIfCondition false positive and negative for String literals when ignoreExpressions=true * java-multithreading * [#2537](https://github.com/pmd/pmd/issues/2537): \[java] DontCallThreadRun can't detect the case that call run() in `this.run()` * [#2538](https://github.com/pmd/pmd/issues/2538): \[java] DontCallThreadRun can't detect the case that call run() in `foo.bar.run()` diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index ac2a1558e0..73b9dfc494 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -566,6 +566,7 @@ public void checkRequests() { // with rule property "ignoreExpressions" set to "false" if (i == pos + 5) {} // violation: magic number 5 within an (additive) expression if (i == pos + SUFFIX_LENGTH) {} // preferred approach + if (i == 5 && "none".equals(aString)) {} // 2 violations: magic number 5 and literal "none" } ]]> diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidLiteralsInIfCondition.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidLiteralsInIfCondition.xml index a03efce856..418dccf581 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidLiteralsInIfCondition.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidLiteralsInIfCondition.xml @@ -144,4 +144,36 @@ public class AvoidLiteralsInIfConditionWithExpressions { } ]]> + + + + + [java] AvoidLiteralsInIfCondition false positive and negative for String literals when ignoreExpressions=true #4514 + true + -1,0 + 2 + 4,5 + + + + + [java] AvoidLiteralsInIfCondition false positive and negative for String literals when ignoreExpressions=false #4514 + false + -1,0 + 5 + 4,5,8,9,9 + +