'cppcheck How to suppress inline unmatched suppression?

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)


Solution 1:[1]

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.

Solution 2:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kurtis Rader
Solution 2