Question

I would like to confirm if this is a bug on PMD? How do I file a ticket if it is.

    public static void main(final String[] args) {
        for (final String string : args) {
            string.getBytes(); //UR Anomaly
        }
        for (int i = 0; i < args.length; i++) {
            args[i].getBytes();
        }
    }

Lines 1-3 are being flagged as UR anomaly, while rewriting it to iterate with a local variable is fine.

Would like to eliminate as much PMD violations, but it is inconvenient to have to resort to old loop construct as a workaround.

While controversial, I do not wish to disable this rule since I find DD, and DU anomaly flagging as useful.

Was it helpful?

Solution

It appears that you have hit a bug in PMD. The DataflowAnomalyAnalysis rule does not seem to catch all possible kinds of variable definitions (another example found here). UR stands for "undefined reference", which is obviously incorrect.

So, what can you do?

Since the problem appears to affect mostly the UR part of the rule, you can disable it and continue using the DU and DD parts. You need a fairly recent version of PMD to do this. In your ruleset file, suppress UR findings like this:

<rule ref="rulesets/java/controversial.xml/DataflowAnomalyAnalysis">
    <properties>
        <property name="violationSuppressRegex" value="^Found 'UR'-anomaly.*"/>
    </properties>
</rule>

Update: For PMD 6.+, the rule ref has changed (thanks ZuziaKru):

<rule ref="category/java/errorprone.xml/DataflowAnomalyAnalysis">
    <properties>
        <property name="violationSuppressRegex" value="^Found 'UR'-anomaly.*"/>
    </properties>
</rule>

In my humble opinion, the whole UR checking is a bit over the top, because the compiler will not accept undefined references. And these days, running the compiler is no longer such a big deal.

OTHER TIPS

Please see, appropriate PMD rule DataflowAnomalyAnalysis constantly is considered controversial. I personally caught it on completely crazy detection of almost any type of anomaly:

  • Any inline declaration leads to UR anomaly alerted. This includes variables defined in for loop scope.
  • DU anomaly often is alerted if variable is defined out of loop scope and its value is changed inside loop before end of some local scope.
  • DD anomaly is reported often in parallel with wrong UR or DU detected. In addition it may be reported if we setup variable before loop and then update it's value inside loop (for next iteration). This anomaly is referenced even inside rule description to be not so relevant.

So by my opinion it is worth to turn off this buggy rule at all.

To extend on the answer of Thomas Jensen: Anyone looking this up nowadays (I'm using PMD version 6.2.0) you'll get a warning from PMD about deprecation of this Rule name. To suppress the UR-anomaly you need this:

<rule ref="category/java/errorprone.xml/DataflowAnomalyAnalysis">
    <properties>
        <property name="violationSuppressRegex" value="^Found 'UR'-anomaly.*"/>
    </properties>
</rule>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top