Menu ▾ ▴

#1089 When changing priority in a custom ruleset, violations reported twice

PMD-5.0.4
closed
None
PMD
3-Major
Bug
4.3, 5.0.3
2014-12-19
2013-04-29
No

I've got a custom ruleset file that has the following as part of it:

<rule ref="rulesets/java/basic.xml/CollapsibleIfStatements" message="foobarbaz" >
    <priority>5</priority>
</rule>

When I run pmd via the 3.0.1 version of the maven-pmd-plugin, violations of this rule occur twice; once with the original priority, once with the new one. (If I add a message to the rule reference, the second violation with the new priority will also show up.)

Is this user error, expected behavior, or an actual bug? Is there some other way to CHANGE priority, rather than add a new rule with a new priority? I used the directions at http://pmd.sourceforge.net/pmd-5.0.3/howtomakearuleset.html.

Sample output in pmd.xml is below:

<violation beginline="551" endline="558" begincolumn="17" endcolumn="17" rule="CollapsibleIfStatements" ruleset="Basic" package="com.pmdtest" class="IfsTest" method="hasManyIfs" externalInfoUrl="http://pmd.sourceforge.net/pmd-5.0.3/rules/java/basic.html#CollapsibleIfStatements" priority="3">
These nested if statements could be combined
</violation>
<violation beginline="551" endline="558" begincolumn="17" endcolumn="17" rule="CollapsibleIfStatements" ruleset="Basic" package="com.pmdtest" class="IfsTest" method="hasManyIfs" externalInfoUrl="http://pmd.sourceforge.net/pmd-5.0.3/rules/java/basic.html#CollapsibleIfStatements" priority="5">
foobarbaz
</violation>

Discussion

  • Andreas Dangel

    Andreas Dangel - 2013-04-30
    • status: open --> accepted
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.0.4
     
  • Andreas Dangel

    Andreas Dangel - 2013-04-30

    Hi,
    I assume, that you import in your custom ruleset the basic ruleset and then (re)define the priority of the CollapsibleIfStatements rule.
    If this is the case, you can for now workaround this issue by excluding the rules you redefine:

    <?xml version="1.0"?>
    <ruleset name="Custom ruleset"
        xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
      <description>This ruleset checks my code for bad stuff</description>
    
      <rule ref="rulesets/java/basic.xml">
          <exclude name="CollapsibleIfStatements"/>
      </rule>
    
      <rule ref="rulesets/java/basic.xml/CollapsibleIfStatements" message="foobarbaz" >
        <priority>5</priority>
      </rule>
    </ruleset>
    

    However, I agree, that the default behaviour should be to overwrite/redefine existing rule definitions, rather than adding duplicates. This would make this feature easier to use.

    Thanks,
    Andreas

     
  • Michael Campbell

    Andreas,

    Yes, this is precisely the issue. I'm importing all of basic, and then trying to redefine just this priority. The workaround you propose works perfectly for me.

    Thanks!

    Michael

     
  • Andreas Dangel

    Andreas Dangel - 2013-05-01
    • status: accepted --> closed
     

Log in to post a comment.