diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index d14b67f1f1..7010f2d9e5 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -19,15 +19,34 @@ This is a {{ site.pmd.release_type }} release. ### New and noteworthy +#### Modified rules + +* The Java rule {% rule "java/errorprone/CompareObjectsWithEquals" %} has now a new property + `typesThatCompareByReference`. With that property, you can configure types, that should be whitelisted + for comparison by reference. By default, `java.lang.Enum` and `java.lang.Class` are allowed, but + you could add custom types here. + Additionally comparisons against constants are allowed now. This makes the rule less noisy when two constants + are compared. Constants are identified by looking for an all-caps identifier. + ### Fixed Issues * doc * [#3230](https://github.com/pmd/pmd/issues/3230): \[doc] Remove "Edit me" button for language index pages +* dist + * [#2466](https://github.com/pmd/pmd/issues/2466): \[dist] Distribution archive doesn't include all batch scripts +* java-bestpractices + * [#2737](https://github.com/pmd/pmd/issues/2737): \[java] Fix misleading rule message on rule SwitchStmtsShouldHaveDefault with non-exhaustive enum switch + * [#3236](https://github.com/pmd/pmd/issues/3236): \[java] LiteralsFirstInComparisons should consider constant fields (cont'd) * java-codestyle * [#2655](https://github.com/pmd/pmd/issues/2655): \[java] UnnecessaryImport false positive for on-demand imports * [#3262](https://github.com/pmd/pmd/pull/3262): \[java] FieldDeclarationsShouldBeAtStartOfClass: false negative with anon classes * [#3265](https://github.com/pmd/pmd/pull/3265): \[java] MethodArgumentCouldBeFinal: false negatives with interfaces and inner classes * [#3266](https://github.com/pmd/pmd/pull/3266): \[java] LocalVariableCouldBeFinal: false negatives with interfaces, anon classes +* java-errorprone + * [#3248](https://github.com/pmd/pmd/issues/3248): \[java] Documentation is wrong for SingletonClassReturningNewInstance rule + * [#3110](https://github.com/pmd/pmd/issues/3110): \[java] Enhance CompareObjectsWithEquals with list of exceptions + * [#3205](https://github.com/pmd/pmd/issues/3205): \[java] Make CompareObjectWithEquals allow comparing against constants + ### API Changes diff --git a/pmd-dist/src/main/resources/assemblies/pmd-bin.xml b/pmd-dist/src/main/resources/assemblies/pmd-bin.xml index 571b6d2664..18a8b9e94e 100644 --- a/pmd-dist/src/main/resources/assemblies/pmd-bin.xml +++ b/pmd-dist/src/main/resources/assemblies/pmd-bin.xml @@ -12,10 +12,7 @@ - cpd.bat - cpdgui.bat - designer.bat - pmd.bat + *.bat target/extra-resources/scripts bin diff --git a/pmd-dist/src/test/java/net/sourceforge/pmd/it/BinaryDistributionIT.java b/pmd-dist/src/test/java/net/sourceforge/pmd/it/BinaryDistributionIT.java index ba0ff6637d..3f7631baa5 100644 --- a/pmd-dist/src/test/java/net/sourceforge/pmd/it/BinaryDistributionIT.java +++ b/pmd-dist/src/test/java/net/sourceforge/pmd/it/BinaryDistributionIT.java @@ -43,6 +43,7 @@ public class BinaryDistributionIT extends AbstractBinaryDistributionTest { result.add(basedir + "bin/run.sh"); result.add(basedir + "bin/pmd.bat"); result.add(basedir + "bin/cpd.bat"); + result.add(basedir + "bin/ast-dump.bat"); result.add(basedir + "lib/pmd-core-" + PMDVersion.VERSION + ".jar"); result.add(basedir + "lib/pmd-java-" + PMDVersion.VERSION + ".jar"); return result; diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 5da98c4f20..dc502c94a2 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -1195,11 +1195,14 @@ public class Foo { -All switch statements should include a default option to catch any unspecified values. + Switch statements should be exhaustive, to make their control flow + easier to follow. This can be achieved by adding a `default` case, or, + if the switch is on an enum type, by ensuring there is one switch branch + for each enum constant. 3 @@ -1211,14 +1214,14 @@ All switch statements should include a default option to catch any unspecified v diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index e73cc2e3b6..0c3c55f5a1 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -1091,11 +1091,22 @@ public class Bar { class="net.sourceforge.pmd.lang.rule.XPathRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#compareobjectswithequals"> -Use equals() to compare object references; avoid comparing them with ==. +Use `equals()` to compare object references; avoid comparing them with `==`. + +Since comparing objects with named constants is useful in some cases (eg, when +defining constants for sentinel values), the rule ignores comparisons against +fields with all-caps name (eg `this == SENTINEL`), which is a common naming +convention for constant fields. + +You may allow some types to be compared by reference by listing the exceptions +in the `typesThatCompareByReference` property. 3 + + java.lang.Enum,java.lang.Class + @@ -2794,9 +2808,9 @@ public class Singleton { class="net.sourceforge.pmd.lang.java.rule.errorprone.SingletonClassReturningNewInstanceRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#singletonclassreturningnewinstance"> -Some classes contain overloaded getInstance. The problem with overloaded getInstance methods -is that the instance created using the overloaded method is not cached and so, -for each call and new objects will be created for every invocation. + A singleton class should only ever have one instance. Failure to check + whether an instance has already been created may result in multiple + instances being created. 2 @@ -2805,7 +2819,7 @@ class Singleton { private static Singleton instance = null; public static Singleton getInstance() { synchronized(Singleton.class) { - return new Singleton(); + return new Singleton(); // this should be assigned to the field } } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/LiteralsFirstInComparisons.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/LiteralsFirstInComparisons.xml index 3f62ce023c..34dd84ff76 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/LiteralsFirstInComparisons.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/LiteralsFirstInComparisons.xml @@ -391,4 +391,43 @@ public class Foo { } ]]> + + #3236 [java] LiteralsFirstInComparisons should consider constant fields (cont'd) + 5 + 6,8,17,24,26 + + diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CompareObjectsWithEquals.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CompareObjectsWithEquals.xml index ebddb5c98c..304865ca36 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CompareObjectsWithEquals.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CompareObjectsWithEquals.xml @@ -114,7 +114,7 @@ package net.sourceforge.pmd.lang.java.rule.errorprone.compareobjectswithequals; public class CompareObjectsWithEqualsSample { void array(int[] a, String[] b) { - if (a[1] == b[1]) {} // int == String - this comparison doesn't make sense + if (a[1] == b[1]) {} // int == String - this comparison doesn't make sense (and doesn't compile...) } void array2(int[] c, int[] d) { if (c[1] == d[1]) {} @@ -410,4 +410,93 @@ public class C0 { ]]> + + [java] CompareObjectsWithEqualsRule: False positive with Enums #2716 + 0 + + + + + static constant #3205 + 0 + + + + + static constant in other class #3205 + 0 + + + + constant field on some object #3205 + 0 + + + + constant field on some object, more complicated expr #3205 + 0 + + + + Property typesThatCompareByReference #3110 + java.lang.String + 0 + + +