cppcheck How to suppress inline unmatched suppression?
Asked Answered
G

2

11

I found that --suppress=unmatchedSuppression only suppresses unmatched suppression types in cppcheck options, but NOT unmatched inline suppressions.

Is this the expected behavior?

test.c

  • Line 4 is wrong. It should be warned arrayIndexOutOfBounds

  • Line 7 is okay. It should NOT be warned arrayIndexOutOfBounds

I have inline cppcheck-suppress for both lines.

  1 void f() {
  2     char arr[5];
  3     // cppcheck-suppress arrayIndexOutOfBounds
  4     arr[10] = 0;
  5
  6     // cppcheck-suppress arrayIndexOutOfBounds
  7     const char ok[] = "this line is ok";
  8 }

Situation 1

Suppress cstyleCast, which does NOT exist in code.

 cppcheck --inline-suppr --force --enable=all 
          --xml-version=2 --suppress=cstyleCast test.c 
          2>cppcheckresults.xml

I get warned about (among other irrelevant warnings)

  1. unmatchedSuppression: arrayIndexOutOfBounds in test.c line 7 (as expected)

  2. unmatchedSuppression: cstyleCast in * line 0 (as expected)

Situation 2

Same as situation 1, but with additional --suppress=unmatchedSuppression option

 cppcheck --inline-suppr --force --enable=all 
          --xml-version=2 --suppress=cstyleCast --suppress=unmatchedSuppressiontest.c 
          2>cppcheckresults.xml

I expect both previous unmatchedSuppression warnings to go away. But I still get

  1. unmatchedSuppression in test.c line 7 (NOT expected)
Gen answered 9/12, 2016 at 16:16 Comment(2)
Do you have a typo in the last code block? should say "unmatchedSuppression:test.c" instead of "unmatchedSuppressiontest.c"?Plotinus
This problem is still present in cppcheck 2.3. I found a workaround, see below.Plotinus
C
5

What I recently found is that an unmatchedSuppression warning from inline suppressions cannot be suppressed by a generic --suppress=unmatchedSuppression or putting just that warning ID in a --suppressions-list file. You have to qualify it with the filename. For example --suppress=unmatchedSuppression:test.c.

Conjugation answered 6/3, 2017 at 21:18 Comment(1)
You can say --suppress=unmatchedSuppression:{}, see my answer.Plotinus
P
2

The problem with the order solution is that you have to do this for every file that has the unmatched suppression.

A. In the command line use the option --suppress=unmatchedSuppression:{}. (note the curly braces)

B. What I discovered is that you can also suppress the unmatched suppression itself in place, for example:

        // cppcheck-suppress[assertWithSideEffect,unmatchedSuppression]
        assert(...);

(tested with cppcheck 2.3 and 2.5)

This is necessary for example when running cppcheck 2.3 which didn't have the assertWithSideEffect feature, later availabe in cppcheck 2.5.

Ideally, as you bump the cppcheck version you can start removing the unnecessary unmatchedSuppression. Needless to say, this all needs the command line --inline-suppr.

Plotinus answered 29/4, 2022 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.