Maybe this is a complete new rule and not a bug, but i believe PMD should detect this :
public class Toto {
public void copyArray(int [] values)
{
int [] copy = new int[values.length];
// As bad as doing a for loop
copy = (int [])(values.clone());
}
}
To easily reprodue this, here is the XPath query:
//Statement[(ForStatement or WhileStatement) and
count(*//AssignmentOperator[@Image = '='])=1
and
*/Statement
./Block/BlockStatement/Statement/StatementExpression/PrimaryExpression
/PrimaryPrefix/Name/../../PrimarySuffix/Expression
[(PrimaryExpression or AdditiveExpression) and count
(.//PrimaryPrefix/Name)=1//PrimaryPrefix/Name/@Image
and
./Block/BlockStatement/Statement/StatementExpression/Expression/PrimaryExpression
/PrimaryPrefix/Name/../../PrimarySuffix[count
(..//PrimarySuffix)=1]/Expression[(PrimaryExpression
or AdditiveExpression) and count(.//PrimaryPrefix/Name)=1]
//PrimaryPrefix/Name/@Image
]]
It seem that the rule didn't check the method clone.
When I try the XPath Expression with the desgner of pmd 4.2.4 on :
public class Foo {
public void copyArray()
{
int [] values = new int[1];
copy = (int [])(values.clone());
}
}
I didn't get any errors...