提交 68924e36 编写于 作者: C caikang.ck

p3c-pmd 2.1.0

上级 87ba6081
......@@ -9,7 +9,7 @@
</parent>
<groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId>
<version>2.0.1</version>
<version>2.1.0</version>
<packaging>jar</packaging>
<name>p3c-pmd</name>
<properties>
......@@ -17,7 +17,7 @@
<pmd.version>6.15.0</pmd.version>
<maven.compiler.target>1.8</maven.compiler.target>
<annotation.version>1.3.2</annotation.version>
<kotlin.version>1.3.50</kotlin.version>
<kotlin.version>1.3.72</kotlin.version>
</properties>
<description>Alibaba Java Coding Guidelines PMD implementations</description>
<url>https://github.com/alibaba/p3c</url>
......
......@@ -38,7 +38,7 @@ public class WrapperTypeEqualityRule extends AbstractAliRule {
final String unaryExpression = "UnaryExpression";
// null presents in either side of "==" or "!=" means no violation
if (node.hasDescendantMatchingXPath(literalPrefix)
|| node.hasDescendantMatchingXPath(unaryExpression)) {
|| node.hasDescendantMatchingXPath(unaryExpression)) {
return super.visit(node, data);
}
......@@ -48,7 +48,10 @@ public class WrapperTypeEqualityRule extends AbstractAliRule {
// PMD can not resolve array length type, but only the
ASTPrimaryExpression left = expressions.get(0);
ASTPrimaryExpression right = expressions.get(1);
// if left is complex expression, skip
if (left.jjtGetNumChildren() > 1) {
return super.visit(node, data);
}
boolean bothArrayLength = isArrayLength(left) && isArrayLength(right);
boolean bothWrapperType = NodeUtils.isWrapperType(left) && NodeUtils.isWrapperType(right);
......@@ -61,7 +64,8 @@ public class WrapperTypeEqualityRule extends AbstractAliRule {
}
private boolean isArrayLength(ASTPrimaryExpression expression) {
// assume expression like "x.length" is the length of array, field with name "length" may result in misrecognition
// assume expression like "x.length" is the length of array, field with name "length" may result in
// misrecognition
return "length".equals(expression.jjtGetLastToken().getImage())
&& ".".equals(expression.jjtGetFirstToken().getNext().getImage());
}
......
......@@ -53,7 +53,7 @@
<description>compare wrapper type objects without equals</description>
<expected-problems>4</expected-problems>
<expected-linenumbers>11,23,31,34</expected-linenumbers>
<code-ref id="wrap-type-not-use-equal" />
<code-ref id="wrap-type-not-use-equal"/>
</test-code>
<!-- ====================================================================== -->
......@@ -148,7 +148,7 @@
<test-code>
<description>bugfix</description>
<expected-problems>0</expected-problems>
<code-ref id="wrong-result-fix" />
<code-ref id="wrong-result-fix"/>
</test-code>
<!-- ====================================================================== -->
......@@ -171,7 +171,37 @@
<expected-problems>0</expected-problems>
<code-ref id="array-length-equals"/>
</test-code>
<code-fragment id="expression-primitive-equals">
<![CDATA[
public class Test {
public void foo(){
int a;
if (Integer.parseInt("0") == a) {
return;
}
if (a == Integer.parseInt("0")) {
return;
}
String s = "0";
if (Integer.parseInt("0") == Integer.parseInt(s)) {
return;
};
Integer b;
if (b == Integer.parseInt("0")) {
return;
}
}
}
]]>
</code-fragment>
<test-code>
<description>expression is primitive type</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>17</expected-linenumbers>
<code-ref id="expression-primitive-equals"/>
</test-code>
<!-- ====================================================================== -->
</test-data>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册