Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can individual rule violations be suppressed during code-analysis in Sonar?

Tags:

c#

sonarqube

The C# plugin within Sonar flags methods where the cyclomatic complexity is greater than 10 (CSharpsquid:S1541 - Methods should not be too complex). This is great for 'real' code, but my team finds it annoying when a method containing just a simple 'switch' statement with 5 cases (used to translate one enum type into another enum type) is flagged as being too complex.

Is it possible to suppress the flag/rule on individual methods within the code-base? If so, how?

like image 811
David Razzetti Avatar asked Oct 26 '25 22:10

David Razzetti


1 Answers

You cannot do this automatically.

Maybe instead of suppressing the rule you can use IMap. You can create a simple class which can translate enums:

public class Enum1ToEnum2Translator {

   private static IMap<Enum1, Enum2> MAP = new Map<Enum1, Enum2>();

   static {
      MAP.add(Enum1.VAL1, Enum2.VAL1);
      MAP.add(Enum1.VAL2, Enum2.VAL2);
      MAP.add(Enum1.VAL3, Enum3.VAL3);
   }

   public Enum2 translate(Enum1 enum) {
      return MAP.get(enum1);
   }
}
like image 59
agabrys Avatar answered Oct 28 '25 14:10

agabrys



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!