How do I exclude certain code from certain rules?

I am on SonarQube 10.3.

I have a particular function which breaks rule c:S1244 “Floating point numbers should not be tested for equality”. In this function, there is nothing at all wrong with testing for equality, so I want to exclude it from the rule. How do I do this exclusion without going and clicking on each instance in the SonarQube web site?

What I am really looking for is exclusion tags, e.g.

/* SONARQUBE_EXCLUDE_START (c:S1244) */
void
myFunc(void)
{
   ...
}
/* SONARQUBE_EXCLUDE_END (c:S1244) */

I cannot find documentation on this. Does it not exist? What is the alternative?

Hi,

Welcome to the community!

I guess you don’t want to have to mark it Won’t Fix / Accepted across branches?

While you can turn off specific rules for specific files, your best bet is probably to use a //NOSONAR comment on the line where the issues is raised. Note that this will suppress all issues on the line, not just this one. But it happens at the code level, so it will take effect across (all future) branches.

 
HTH,
Ann

Hello!

I was actually just reading about sonar.issue.ignore.block, which I think would be more what I’m looking for, but thank you anyway.

Do you know if there is a timeline for turning off individual rules with tags like this?

Hi,

Per the docs, sonar.issue.ignore.block ignores all issues within the block not just your selected one, and it requires you to denote the blocks with begin and end strings. It’s intended to allow you to exclude blocks of generated code within a file. I don’t believe it’s appropriate for your situation.

There’s been some discussion on this topic, but I’m not aware of a timeline.

 
HTH,
Ann

Hello,

I’d just like to raise this issue again, with a more concrete example:

void
Cams::getImage_handler(
    int                   const  port,
    ImageAcquisitionError       &return_error,
    CameraDeviceId        const &device_id,
    POINTER_CAST          const  image_data_ptr,
    uint32_t              const  frame_size,
    uint32_t              const  frame_width,
    uint32_t              const  frame_height)

This function signature is autocoded to conform to a certain interface. There is little I can do about its parameters. Unfortunately, it is falling foul of " The order for arguments of the same type in a function call should be obviouscpp:S5419".

I understand the rule, and I would not want to disable it generally, but also, this warning will arise again and again in these interface-conformant autocoded function signatures. It is so onerous to have to open up the browser to deal with these issues when you know that these function signatures categorically will not satisfy the rule.

/* NOSONAR_BEGIN (S5419) */
void
Cams::getImage_handler(
    int                   const  port,
    ImageError                  &return_error,
    CameraDeviceId        const &device_id,
    POINTER_CAST          const  image_data_ptr,
    uint32_t              const  frame_size,
    uint32_t              const  frame_width,
    uint32_t              const  frame_height)
/* NOSONAR_END (S5419) */

Lovely!

Hi @JWRWSEU,

Thank you for your contribution. We are painfully aware of the actual behavior of NOSONAR which is… less than desirable.
This is a topic of discussion right now, to see in which direction we need to go with this. This use-case will feed into our discussion.

Thank you again!

2 Likes