Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SuppressWarnings for 'common-java' rules

Tags:

java

sonarqube

I need to temporary ignore rule "Insufficient branch coverage by unit tests" (common-java:InsufficientBranchCoverage).

Reading http://docs.sonarqube.org/display/SONAR/Frequently+Asked+Questions I see that SuppressWarnings should work for all rules.

But any combination of

@SuppressWarnings("common-java:InsufficientBranchCoverage")
@SuppressWarnings("InsufficientBranchCoverage")
@SuppressWarnings("java:InsufficientBranchCoverage")

does not work for me.

I use Sonar 5.0, Sonar Java plugin 3.0.

Edit:

This warning may be supressed (removed) from sonar UI. I see two solutions

  • disable the rule 'Insufficient branch coverage by unit tests' for my quality profile. The drawback is, that rule is disabled for whole project, not just for single class

  • mark issue as ignored when browsing issues drilldown. This ignores only single occurence of the issue. The drawback is, issue need to be marked in every sonar project (we have project-per-branch). When I need to remove warning, I must do this in sonar UI again, for each project.

like image 956
Bartosz Bilicki Avatar asked Mar 30 '15 10:03

Bartosz Bilicki


People also ask

How do I suppress all warnings in Java?

Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.

How do I check my SonarQube rules?

By default, when entering the top menu item "Rules", you will see all the available rules installed on your SonarQube instance. You have the ability to narrow the selection based on search criteria in the left pane: Language: the language to which a rule applies.

Where do I put Nosonar?

The NOSONAR marker should be added to a comment. For instance, as Python uses # for comments, the correct way is # NOSONAR , Java and many others use // so it'll be // NOSONAR and so on.

How do I ignore SonarQube rule?

The SonarQube JAVA Analyzer allows you to use the "@SuppressWarnings" annotation to disable a specific rule locally. It will allows you to disable issues on a single line, by placing the annotation directly above an instruction, or in an entire block, by placing it above a class or a method for instance.


1 Answers

Unfortunately, it is not possible.

The InsufficientBranchCoverage rule applies directly at File level and it is consequently not linked to any particular line in the file. To remove issues related to a given rule key using @SuppressWarnings, the rule has to apply at Class or Method level (as you can read in the documentation).

Note that to guarantee consistency of the results of the analysis, we can not disable the issue at File level, as it may end by hiding issues which would have been perfectly legit (take for instance the situation of a java file having multiple classes).

like image 51
Wohops Avatar answered Oct 13 '22 12:10

Wohops