4

In Python we can suppress all Sonarqube warnings at a particular line in the code by applying the # NOSONAR comment. This is not ideal. Is there a way to suppress a specific error, instead of supressing all errors?

For example, you may have a function with two warnings:

Function "foo" has 8 parameters, which is greater than the 7 authorized.
Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed

How can you suppress the first, but not the second?

Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
  • If I got your question right...Do you mean that you want to use "@SuppressWarnings("squid:S###")" instead of NOSONAR? – Ehsan May 10 '20 at 19:06
  • @Ehsan Is `@SupressWarnings` available in Python? I thought this was only allowed in Java. – Matthew Moisen May 10 '20 at 20:29
  • Does this answer your question? [Ignore SonarQube warnings in python](https://stackoverflow.com/questions/37609940/ignore-sonarqube-warnings-in-python) – agabrys May 11 '20 at 20:32
  • 2
    @agabrys No, this answer says that using # NOSONAR "is a global issue suppression: it kills all issues on the line, not just those from a specific rule." I'm looking for a mechanism to suppress one specific rule, not all of them. – Matthew Moisen May 12 '20 at 21:25

1 Answers1

3

This is a workaround if you are using Jenkins, that isn't perfect. It can be used to suppress a specific warning for an entire file (instead of just one function).

In the Jenkins property file add something like:

sonar.issue.ignore.multicriteria=e1
sonar.issue.ignore.multicriteria.e1.ruleKey=python:S107
sonar.issue.ignore.multicriteria.e1.resourceKey=path/to/file.py

Where python:S107 is the rulekey for a function having more than 7 parameters, and path/to/file.py is the file you want to suppress this specific rule for. Unfortunately it will supress it for the entire file, as opposed to the specific function.

Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231