wpilibsuite / allwpilib

Official Repository of WPILibJ and WPILibC
https://wpilib.org/
Other
1.05k stars 611 forks source link

Upgrade to PMD 7.1.0 #6597

Closed calcmogul closed 2 months ago

calcmogul commented 3 months ago

Bump the PMD version here:

diff --git a/shared/java/javastyle.gradle b/shared/java/javastyle.gradle
index 41ddb47e6..5c9a140ee 100644
--- a/shared/java/javastyle.gradle
+++ b/shared/java/javastyle.gradle
@@ -13,7 +13,7 @@ checkstyle {
 apply plugin: 'pmd'

 pmd {
-    toolVersion = '6.55.0'
+    toolVersion = '7.1.0'
     consoleOutput = true
     reportsDir = file("$project.buildDir/reports/pmd")
     ruleSetFiles = files(new File(rootDir, "styleguide/pmd-ruleset.xml"))

then run ./gradlew pmdDev pmdMain pmdPrintlog pmdTest and fix the XML validation errors PMD prints.

Warning at /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml:56:5
 54|     <exclude name="LoosePackageCoupling" />
 55|     <exclude name="NPathComplexity" />
 56|     <exclude name="NcssConstructorCount" />
         ^^^^^^^^ Exclude pattern 'NcssConstructorCount' did not match any rule in ruleset 'category/java/design.xml'

 57|     <exclude name="NcssCount" />
 58|     <exclude name="NcssMethodCount" />
Warning at /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml:49:5
 47|     <exclude name="ExcessiveClassLength" />
 48|     <exclude name="ExcessiveImports" />
 49|     <exclude name="ExcessiveMethodLength" />
         ^^^^^^^^ Exclude pattern 'ExcessiveMethodLength' did not match any rule in ruleset 'category/java/design.xml'

 50|     <exclude name="ExcessiveParameterList" />
 51|     <exclude name="ExcessivePublicCount" />
Warning at /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml:47:5
 45|     <exclude name="CyclomaticComplexity" />
 46|     <exclude name="DataClass" />
 47|     <exclude name="ExcessiveClassLength" />
         ^^^^^^^^ Exclude pattern 'ExcessiveClassLength' did not match any rule in ruleset 'category/java/design.xml'

 48|     <exclude name="ExcessiveImports" />
 49|     <exclude name="ExcessiveMethodLength" />
Warning at /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml:58:5
 56|     <exclude name="NcssConstructorCount" />
 57|     <exclude name="NcssCount" />
 58|     <exclude name="NcssMethodCount" />
         ^^^^^^^^ Exclude pattern 'NcssMethodCount' did not match any rule in ruleset 'category/java/design.xml'

 59|     <exclude name="TooManyFields" />
 60|     <exclude name="TooManyMethods" />
Warning at /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml:73:5
 71|     <exclude name="DoNotTerminateVM" />
 72|     <exclude name="EmptyCatchBlock" />
 73|     <exclude name="EmptyWhileStmt" />
         ^^^^^^^^ Exclude pattern 'EmptyWhileStmt' did not match any rule in ruleset 'category/java/errorprone.xml'

 74|     <exclude name="FinalizeDoesNotCallSuperFinalize" />
 75|     <exclude name="JUnitSpelling" />
Warning at /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml:70:5
 68|     <exclude name="CloseResource" />
 69|     <exclude name="ConstructorCallsOverridableMethod" />
 70|     <exclude name="DataflowAnomalyAnalysis" />
         ^^^^^^^^ Exclude pattern 'DataflowAnomalyAnalysis' did not match any rule in ruleset 'category/java/errorprone.xml'

 71|     <exclude name="DoNotTerminateVM" />
 72|     <exclude name="EmptyCatchBlock" />
Warning at /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml:89:5
 87| 
 88|   <rule ref="category/java/performance.xml">
 89|     <exclude name="AvoidUsingShortType" />
         ^^^^^^^^ Exclude pattern 'AvoidUsingShortType' did not match any rule in ruleset 'category/java/performance.xml'

 90|     <exclude name="AvoidInstantiatingObjectsInLoops" />
 91|   </rule>
Error at /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml:101:21
  99|   <rule name="UseRequireNonNull"
 100|     message="Use Objects.requireNonNull() instead of throwing a NullPointerException yourself."
 101|     language="java" class="net.sourceforge.pmd.lang.rule.XPathRule">
                          ^^^^^ net.sourceforge.pmd.lang.rule.XPathRule

 102|     <description>Use Objects.requireNonNull() instead of throwing a
 103|       NullPointerException yourself.</description>: net.sourceforge.pmd.lang.rule.XPathRule
Cannot load ruleset /home/tav/frc/wpilib/allwpilib/styleguide/pmd-ruleset.xml: An XML validation error occurred
Gold856 commented 3 months ago

I fixed the XML validation errors and updated the exclusions:

diff --git a/shared/java/javastyle.gradle b/shared/java/javastyle.gradle
index 41ddb47e6..5c9a140ee 100644
--- a/shared/java/javastyle.gradle
+++ b/shared/java/javastyle.gradle
@@ -13,7 +13,7 @@ checkstyle {
 apply plugin: 'pmd'

 pmd {
-    toolVersion = '6.55.0'
+    toolVersion = '7.1.0'
     consoleOutput = true
     reportsDir = file("$project.buildDir/reports/pmd")
     ruleSetFiles = files(new File(rootDir, "styleguide/pmd-ruleset.xml"))
diff --git a/styleguide/pmd-ruleset.xml b/styleguide/pmd-ruleset.xml
index 1f6577e6f..b965fc981 100644
--- a/styleguide/pmd-ruleset.xml
+++ b/styleguide/pmd-ruleset.xml
@@ -31,6 +31,7 @@
     <exclude name="SwitchStmtsShouldHaveDefault" />
     <exclude name="SystemPrintln" />
     <exclude name="UseVarargs" />
+    <exclude name="UnusedAssignment" />
   </rule>
   <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod">
     <properties>
@@ -42,20 +43,18 @@
   <rule ref="category/java/design.xml">
     <exclude name="AvoidThrowingRawExceptionTypes" />
     <exclude name="CognitiveComplexity" />
+    <exclude name="CouplingBetweenObjects" />
     <exclude name="CyclomaticComplexity" />
     <exclude name="DataClass" />
-    <exclude name="ExcessiveClassLength" />
     <exclude name="ExcessiveImports" />
-    <exclude name="ExcessiveMethodLength" />
     <exclude name="ExcessiveParameterList" />
     <exclude name="ExcessivePublicCount" />
     <exclude name="GodClass" />
+    <exclude name="ImmutableField" />
     <exclude name="LawOfDemeter" />
     <exclude name="LoosePackageCoupling" />
     <exclude name="NPathComplexity" />
-    <exclude name="NcssConstructorCount" />
     <exclude name="NcssCount" />
-    <exclude name="NcssMethodCount" />
     <exclude name="TooManyFields" />
     <exclude name="TooManyMethods" />
   </rule>
@@ -67,10 +66,8 @@
     <exclude name="AvoidLiteralsInIfCondition" />
     <exclude name="CloseResource" />
     <exclude name="ConstructorCallsOverridableMethod" />
-    <exclude name="DataflowAnomalyAnalysis" />
     <exclude name="DoNotTerminateVM" />
     <exclude name="EmptyCatchBlock" />
-    <exclude name="EmptyWhileStmt" />
     <exclude name="FinalizeDoesNotCallSuperFinalize" />
     <exclude name="JUnitSpelling" />
     <exclude name="MissingSerialVersionUID" />
@@ -86,7 +83,6 @@
   </rule>

   <rule ref="category/java/performance.xml">
-    <exclude name="AvoidUsingShortType" />
     <exclude name="AvoidInstantiatingObjectsInLoops" />
   </rule>

@@ -98,7 +94,7 @@
   <!-- Custom Rules -->
   <rule name="UseRequireNonNull"
     message="Use Objects.requireNonNull() instead of throwing a NullPointerException yourself."
-    language="java" class="net.sourceforge.pmd.lang.rule.XPathRule">
+    language="java" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
     <description>Use Objects.requireNonNull() instead of throwing a
       NullPointerException yourself.</description>
     <properties>

Excluding the other linting warnings which haven't been fixed, the main problem now is that PMD throws a bunch of exceptions with "bad intersection" when trying to process MutableMeasure.java:

org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.IllegalArgumentException: Bad intersection, unrelated class types edu.wpi.first.units.Dimensionless and edu.wpi.first.units.Unit in [edu.wpi.first.units.Unit, edu.wpi.first.units.Dimensionless]
Exception Context:
    [1:Resolving type of=!debug only! [VariableAccess:251:22]multiplier]
    [2:Rule applied on node=!debug only! [MethodCall:251:22]multiplier.baseUnitMagnitude()]
---------------------------------
    at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:236)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:54)
    at net.sourceforge.pmd.lang.java.ast.ASTVariableAccess.getTypeMirror(ASTVariableAccess.java:27)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:39)
    at net.sourceforge.pmd.lang.java.ast.ASTVariableAccess.getTypeMirror(ASTVariableAccess.java:27)
    at net.sourceforge.pmd.lang.java.types.TypeTestUtil.isA(TypeTestUtil.java:65)
    at net.sourceforge.pmd.lang.java.rule.bestpractices.UseCollectionIsEmptyRule.visit(UseCollectionIsEmptyRule.java:29)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor(ASTMethodCall.java:75)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor(AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.apply(AbstractJavaRule.java:30)
    at net.sourceforge.pmd.lang.rule.RuleReference.apply(RuleReference.java:415)
    at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:77)
    at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply(RuleApplicator.java:57)
    at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply(RuleSets.java:155)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:140)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalArgumentException: Bad intersection, unrelated class types edu.wpi.first.units.Dimensionless and edu.wpi.first.units.Unit in [edu.wpi.first.units.Unit, edu.wpi.first.units.Dimensionless]
    at net.sourceforge.pmd.lang.java.types.Lub.glb(Lub.java:344)
    at net.sourceforge.pmd.lang.java.types.TypeSystem.glb(TypeSystem.java:723)
    at net.sourceforge.pmd.lang.java.types.TypeConversion.capture(TypeConversion.java:227)
    at net.sourceforge.pmd.lang.java.types.TypeConversion.capture(TypeConversion.java:154)
    at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit(LazyTypeResolver.java:541)
    at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit(LazyTypeResolver.java:95)
    at net.sourceforge.pmd.lang.java.ast.ASTVariableAccess.acceptVisitor(ASTVariableAccess.java:76)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:51)
    ... 19 more

Not sure how to fix this.