4

I have this sonar error Major:

Found 'UR'-anomaly for variable 'language' (lines '83'-'85')

in this function:

public void saveAll(List<Language> languages){
   //Found 'UR'-anomaly for variable 'country' (lines '83'-'85').       
   //Code Smell   Major    Open    Not assigned   20min effort   Comment
   for (Language language: languages) {
        save(language);
   }
}

how to fix this major error please, thanks for advance

croxy
  • 4,082
  • 9
  • 28
  • 46
KIBOU Hassan
  • 381
  • 4
  • 13

1 Answers1

2

Edit: Found even more information it this other SO post. While that is more PMD centric, the background information can be of interest to you. Java for each loop being flagged as UR anomaly by PMD.


This is a rule from PMD it seems. Definition:

The dataflow analysis tracks local definitions, undefinitions and references to variables on different paths on the data flow. From those informations there can be found various problems. 1. UR - Anomaly: There is a reference to a variable that was not defined before. This is a bug and leads to an error. 2. DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text. 3. DD - Anomaly: A recently defined variable is redefined. This is ominous but don't have to be a bug.

There is an open bug report for this: https://sourceforge.net/p/pmd/bugs/1190/

In the example they report it for Arrays, but somebody commented that it happens for them also for collections.

Example:

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();
    }
}

In our sonar setup we don't use this rule. Based on the available information you may wish not to use it in yours.

Community
  • 1
  • 1
dbalakirev
  • 1,918
  • 3
  • 20
  • 31