Automate fixes for issues found by Sonar

2019-02-18 07:07发布

I am working on fixing issues caught by sonar on a very old Java project(8 years old approximately ). It is a huge project with a lot of faulty code that is caught by sonar. Although they are very trivial fixes but there are a number of them. Is there a way to automatically fix a series of similar issues ? Like i have around 1200 cases of "statements should be on separate lines" , to do it manually would take ages. Can i automate these fixes somehow ?

4条回答
祖国的老花朵
2楼-- · 2019-02-18 07:56

Currently there are no tools available for automatic fixing of sonarube bugs. There is walkmod, but it is only for trivial code-smells.

I am currently writing an automatic repair tool for sonarube bugs in java. It uses INRIA's spoon to fix the bugs. The github repo is sonarqube-repair. It has fixes for five bugs: rule numbers 1854, 1948, 2055, 2259 and 2095. I will update this answer once there is some significant progress.

查看更多
混吃等死
3楼-- · 2019-02-18 08:01

I don't think I would want Sonar to change source. It's a code analyzer.

You could configure your IDE to format on save and do an initial format of all afflicted source files, so that it puts the satements on separate lines.

Also, you might want to review the importance of the problem and change/edit the Quality Profile.

查看更多
Melony?
4楼-- · 2019-02-18 08:04

Do it step by step (for Eclipse IDE):

  1. You can use Save action on your whole project: Right-click on your Java project source -> Source -> Format...
  2. Right-click on your Java project source -> Source -> Clean Up...
  3. Use the AutoRefactor plugin: Right-click on your code -> AutoRefactor -> Automatic refactoring
  4. Use WalkMod (not tested).
查看更多
劳资没心,怎么记你
5楼-- · 2019-02-18 08:11

If you want to apply the same resolution to a big number of issues, you can use the Bulk Change feature (introduced in SonarQube 3.7 LTS).

In your particular case, you could also define issue exclusion rules to prevent these issues from being raised on selected parts of the code (introduced in SonarQube 4.0).

Finally, if a particular rule annoys you to the point that it prevents you from working on real issues, you can disable this rule altogether in your quality profile.

查看更多
登录 后发表回答