From 357f206102fee7335d1ab05710b85953def9b4eb Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 15 May 2015 20:24:06 +0200 Subject: [PATCH] #1330 AvoidReassigningParameters does not work with varargs --- .../rule/design/AvoidReassigningParametersRule.java | 1 + .../java/symboltable/VariableNameDeclaration.java | 6 ++++++ .../rule/design/xml/AvoidReassigningParameters.xml | 11 +++++++++++ src/site/markdown/overview/changelog.md | 1 + 4 files changed, 19 insertions(+) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/AvoidReassigningParametersRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/AvoidReassigningParametersRule.java index fe497d763a..b1ff2670e8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/AvoidReassigningParametersRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/AvoidReassigningParametersRule.java @@ -32,6 +32,7 @@ public class AvoidReassigningParametersRule extends AbstractJavaRule { if ((jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) && jocc.getNameForWhichThisIsAQualifier() == null && !jocc.useThisOrSuper() + && !decl.isVarargs() && (!decl.isArray() || jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetNumChildren() == 1)) { // not an array or no primary suffix to access the array // values diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/VariableNameDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/VariableNameDeclaration.java index b3317bf831..35813b0f88 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/VariableNameDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/VariableNameDeclaration.java @@ -36,6 +36,12 @@ public class VariableNameDeclaration extends AbstractNameDeclaration implements } } + public boolean isVarargs() { + ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node; + ASTFormalParameter parameter = astVariableDeclaratorId.getFirstParentOfType(ASTFormalParameter.class); + return parameter != null && parameter.isVarargs(); + } + public boolean isExceptionBlockParameter() { return ((ASTVariableDeclaratorId) node).isExceptionBlockParameter(); } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/AvoidReassigningParameters.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/AvoidReassigningParameters.xml index 97968c48c0..8584a954fb 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/AvoidReassigningParameters.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/AvoidReassigningParameters.xml @@ -234,6 +234,17 @@ public class PmdBug { public static void main(String[] args) { new PmdBug().foo("Hello world"); } +} + ]]> + + + #1330 AvoidReassigningParameters does not work with varargs + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index b604c38368..69576469cd 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -12,6 +12,7 @@ **Bugfixes:** +* [#1330](https://sourceforge.net/p/pmd/bugs/1330/): AvoidReassigningParameters does not work with varargs * [#1335](https://sourceforge.net/p/pmd/bugs/1335/): GuardLogStatementJavaUtil should not apply to SLF4J Logger * [#1342](https://sourceforge.net/p/pmd/bugs/1342/): UseConcurrentHashMap false positive (with documentation example) * [#1343](https://sourceforge.net/p/pmd/bugs/1343/): MethodNamingConventions for overrided methods