Merge branch for pr #1647
This commit is contained in:
@ -59,6 +59,9 @@ CPD also parses raw string literals now correctly (see [#1784](https://github.co
|
||||
conventions for property declarations. By default this rule uses the standard Apex naming convention (Camel case),
|
||||
but it can be configured through properties.
|
||||
|
||||
* The new Java rule {% rule "java/codestyle/UseShortArrayInitializer" %} (`java-codestyle`) searches for
|
||||
array initialization expressions, which can be written shorter.
|
||||
|
||||
#### Modified Rules
|
||||
|
||||
* The Apex rule {% rule "apex/codestyle/ClassNamingConventions" %} (`apex-codestyle`) can now be configured
|
||||
@ -105,6 +108,7 @@ CPD also parses raw string literals now correctly (see [#1784](https://github.co
|
||||
* java-bestpractices
|
||||
* [#1738](https://github.com/pmd/pmd/issues/1738): \[java] MethodReturnsInternalArray does not work in inner classes
|
||||
* java-codestyle
|
||||
* [#1495](https://github.com/pmd/pmd/issues/1495): \[java] Rule to detect overly verbose array initializiation
|
||||
* [#1684](https://github.com/pmd/pmd/issues/1684): \[java] Properly whitelist serialPersistentFields
|
||||
* [#1804](https://github.com/pmd/pmd/issues/1804): \[java] NPE in UnnecessaryLocalBeforeReturnRule
|
||||
* python
|
||||
@ -134,6 +138,7 @@ CPD also parses raw string literals now correctly (see [#1784](https://github.co
|
||||
|
||||
### External Contributions
|
||||
|
||||
* [#1647](https://github.com/pmd/pmd/pull/1647): \[java] Rule to detect overly verbose array initialization - [Victor](https://github.com/IDoCodingStuffs)
|
||||
* [#1798](https://github.com/pmd/pmd/pull/1798): \[java] Make CommentDefaultAccessModifier work for top-level classes - [Boris Petrov](https://github.com/boris-petrov)
|
||||
* [#1799](https://github.com/pmd/pmd/pull/1799): \[java] MethodReturnsInternalArray does not work in inner classes - Fixed #1738 - [Srinivasan Venkatachalam](https://github.com/Srini1993)
|
||||
* [#1802](https://github.com/pmd/pmd/pull/1802): \[python] \[cpd] Add support for Python 2 backticks - [Maikel Steneker](https://github.com/maikelsteneker)
|
||||
|
@ -13,5 +13,6 @@ This ruleset contains links to rules that are new in PMD v6.15.0
|
||||
<rule ref="category/apex/codestyle.xml/LocalVariableNamingConventions"/>
|
||||
<rule ref="category/apex/codestyle.xml/PropertyNamingConventions"/>
|
||||
|
||||
<rule ref="category/java/codestyle.xml/UseShortArrayInitializer"/>
|
||||
</ruleset>
|
||||
|
||||
|
@ -2117,6 +2117,39 @@ public class Foo {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="UseShortArrayInitializer"
|
||||
language="java"
|
||||
since="6.15.0"
|
||||
message="Array initialization can be written shorter"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#useshortarrayinitializer">
|
||||
<description>
|
||||
<![CDATA[
|
||||
When declaring and initializing array fields or variables, it is not necessary to explicitly create a new array
|
||||
using `new`. Instead one can simply define the initial content of the array as a expression in curly braces.
|
||||
|
||||
E.g. `int[] x = new int[] { 1, 2, 3 };` can be written as `int[] x = { 1, 2, 3 };`.
|
||||
]]>
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value><![CDATA[
|
||||
//VariableDeclarator
|
||||
[VariableDeclaratorId[@ArrayType = true() and @TypeInferred = false()]]
|
||||
[VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/AllocationExpression/ArrayDimsAndInits/ArrayInitializer]
|
||||
]]></value>
|
||||
</property>
|
||||
<property name="version" value="2.0"/>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
Foo[] x = new Foo[] { ... }; // Overly verbose
|
||||
Foo[] x = { ... }; //Equivalent to above line
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="VariableNamingConventions"
|
||||
since="1.2"
|
||||
deprecated="true"
|
||||
|
@ -0,0 +1,11 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.rule.codestyle;
|
||||
|
||||
import net.sourceforge.pmd.testframework.PmdRuleTst;
|
||||
|
||||
public class UseShortArrayInitializerTest extends PmdRuleTst {
|
||||
// no additional unit tests
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<test-data
|
||||
xmlns="http://pmd.sourceforge.net/rule-tests"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
|
||||
|
||||
<test-code>
|
||||
<description>simple failure case</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>3</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
public class UseShortArrayExample {
|
||||
void foo() {
|
||||
int[] x = new int[] {1,2,3};
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>case with two initializers</description>
|
||||
<expected-problems>2</expected-problems>
|
||||
<expected-linenumbers>3,3</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
public class UseShortArrayExample {
|
||||
void foo() {
|
||||
int ar[] = new int[] { 1,2,3}, foo[] = new int[] { 4, 5, 6 };
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>ok</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class UseShortArrayExample {
|
||||
void foo() {
|
||||
int[] x = {1,2,3};
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>assignment needs to use new</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class UseShortArrayExample {
|
||||
void foo() {
|
||||
int[] a = { 1 };
|
||||
a = new int[] { 2 }; // can't be replaced
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>empty array initialization</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class UseShortArrayExample {
|
||||
public void foo() {
|
||||
int[] a = new int[3]; // no violation
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>array initialization with fields</description>
|
||||
<expected-problems>3</expected-problems>
|
||||
<expected-linenumbers>2,3,3</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
public class UseShortArrayExample {
|
||||
private int[] f1 = new int[] {1,2,3};
|
||||
private int[] f2 = new int[] {1,2,3}, f3 = new int[] { 4,5,6 };
|
||||
|
||||
private int[] good = { 1,2,3 };
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>array initialization with var</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class UseShortArrayExample {
|
||||
public void foo() {
|
||||
var ar1 = new int[] { 1, 2 };
|
||||
//var ar2 = { 1, 2 }; // this is actually a compile-time error and is forbidden. See JLS 14.4.
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
Reference in New Issue
Block a user