diff --git a/pmd-java/src/main/resources/category/java/performance.xml b/pmd-java/src/main/resources/category/java/performance.xml index da96595d85..c696c2f0ee 100644 --- a/pmd-java/src/main/resources/category/java/performance.xml +++ b/pmd-java/src/main/resources/category/java/performance.xml @@ -28,7 +28,7 @@ It is much better to use one of the type-specific `toString()` methods instead o //AdditiveExpression/PrimaryExpression/PrimaryPrefix/Literal[@Image='""' and not(ancestor::Annotation)] | //AdditiveExpression/PrimaryExpression/PrimaryPrefix/Name - [@Image = //(LocalVariableDeclaration[@Final = true()]|FieldDeclaration[@Final = true()]) + [@Image = (//FieldDeclaration[@Final = true()]|ancestor::MethodDeclaration//LocalVariableDeclaration[@Final = true()]) /VariableDeclarator[@Initializer = true()] [VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='""']] /VariableDeclaratorId/@Name] diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/AddEmptyString.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/AddEmptyString.xml index aa3b071b1c..052a50f8ef 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/AddEmptyString.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/AddEmptyString.xml @@ -102,7 +102,7 @@ class Main { final String outerString1 = ""; final String outerString2 = ""; - public static void main(String[] args) { + public static void main(String[] args, String otherString) { final String innerString1 = ""; final String innerString2 = ""; @@ -110,6 +110,11 @@ class Main { String b = outerString2 + 514; String c = innerString1 + 1919; String d = innerString2 + 810; + String e = otherString + 42; // should not be flagged, otherString is a method parameter. Not to be confused with otherString local var in otherMethod + } + + void otherMethod() { + final String otherString = ""; } } ]]>