Found 'UR'-anomaly for variable

余生长醉 提交于 2021-01-27 06:27:29

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/40104296/found-ur-anomaly-for-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!