Menu

Inline suppression of duplicateExpression

pfaco
2016-08-25
2018-12-21
  • pfaco

    pfaco - 2016-08-25

    Hello guys, I've got, what I hope to be, a simple question.

    I'm trying to suppress inline the "duplicateExpression" but I'm getting a "unmatched suppression" message.

    I know the id is correct, because I configured cppcheck to print the id together with the error using the following template: --template "{file}:{line}: {severity} ({id}): {message}"

    I'm using CppCheck version 1.75.

    Am I missing something here? Thanks.

     
  • Daniel Marjamäki

    it works for me:

    void f(int x) {
      // cppcheck-suppress duplicateExpression
      if (x+1==x+1) {}
    }
    

    Command line output:

    $ ./cppcheck --enable=style --inline-suppr 1.c
    Checking 1.c ...
    $ ./cppcheck --enable=style 1.c
    Checking 1.c ...
    [1.c:3] -> [1.c:3]: (style) Same expression on both sides of '=='.
    

    does that work for you?

     
  • pfaco

    pfaco - 2016-08-27

    It doesn't.

    I managed to create this simple test that demonstrates the problem:

    void test(void)
    {
        // cppcheck-suppress duplicateExpression
        int x = 
            ( 0u << 0u ) | 
            ( 0u << 1u ) | 
            ( 0u << 2u ) | 
            ( 0u << 3u ) | 
            ( 1u << 4u ) | 
            ( 0u << 5u ) | 
            ( 0u << 6u ) | 
            ( 0u << 7u ) | 
            ( 0u << 8u ) | 
            ( 1u << 9u ) | 
            ( 0u << 10u) | 
            ( 0u << 11u) | 
            ( 0u << 12u) | 
            ( 0u << 13u) | 
            ( 0u << 14u) | 
            ( 0u << 15u);  
    }
    
    C:\temp>cppcheck --enable=style --inline-suppr test.c
    Checking test.c...
    [test.c:5] -> [test.c:5]: (style) Same expression on both sides of '|'.
    [test.c:7] -> [test.c:7]: (style) Same expression on both sides of '|'.
    [test.c:8] -> [test.c:8]: (style) Same expression on both sides of '|'.
    [test.c:10] -> [test.c:8]: (style) Same expression on both sides of '|'.
    [test.c:10] -> [test.c:7]: (style) Same expression on both sides of '|'.
    [test.c:11] -> [test.c:11]: (style) Same expression on both sides of '|'.
    [test.c:12] -> [test.c:12]: (style) Same expression on both sides of '|'.
    [test.c:13] -> [test.c:13]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:13]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:12]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:11]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:10]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:8]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:7]: (style) Same expression on both sides of '|'.
    [test.c:16] -> [test.c:16]: (style) Same expression on both sides of '|'.
    [test.c:17] -> [test.c:17]: (style) Same expression on both sides of '|'.
    [test.c:18] -> [test.c:18]: (style) Same expression on both sides of '|'.
    [test.c:19] -> [test.c:19]: (style) Same expression on both sides of '|'.
    [test.c:20] -> [test.c:20]: (style) Same expression on both sides of '|'.
    [test.c:4]: (style) Variable 'x' is assigned a value that is never used.
    
    C:\temp>cppcheck --enable=style test.c
    Checking test.c...
    [test.c:5] -> [test.c:5]: (style) Same expression on both sides of '|'.
    [test.c:7] -> [test.c:7]: (style) Same expression on both sides of '|'.
    [test.c:8] -> [test.c:8]: (style) Same expression on both sides of '|'.
    [test.c:10] -> [test.c:8]: (style) Same expression on both sides of '|'.
    [test.c:10] -> [test.c:7]: (style) Same expression on both sides of '|'.
    [test.c:11] -> [test.c:11]: (style) Same expression on both sides of '|'.
    [test.c:12] -> [test.c:12]: (style) Same expression on both sides of '|'.
    [test.c:13] -> [test.c:13]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:13]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:12]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:11]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:10]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:8]: (style) Same expression on both sides of '|'.
    [test.c:15] -> [test.c:7]: (style) Same expression on both sides of '|'.
    [test.c:16] -> [test.c:16]: (style) Same expression on both sides of '|'.
    [test.c:17] -> [test.c:17]: (style) Same expression on both sides of '|'.
    [test.c:18] -> [test.c:18]: (style) Same expression on both sides of '|'.
    [test.c:19] -> [test.c:19]: (style) Same expression on both sides of '|'.
    [test.c:20] -> [test.c:20]: (style) Same expression on both sides of '|'.
    [test.c:4]: (style) Variable 'x' is assigned a value that is never used.
    

    Any ideas how to solve this?

    Thanks.

     
  • Daniel Marjamäki

    Sorry for the late reply. But Cppcheck should not warn about those because the expressions are not the same. Don't suppress when you see wrong warnings, tell us about it so we can fix Cppcheck.

     
  • Daniel Marjamäki

    Anyway.. I guess you saw that this has been fixed. Latest Cppcheck does not warn.

     
    • Bob Stafford

      Bob Stafford - 2018-12-18

      I'm using 1.86 and having trouble suppressing duplicat expressions. Here's my code the last line divides two results from getScale() and cppcheck obviously does not know that they are not the same so it generates an error. I tried to disable it with inline suppressions but in this case I still get the error

      [layerdisplay/lib/LayerGLWidget.cpp:487] -> [layerdisplay/lib/LayerGLWidget.cpp:495]: (style) Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.

                  // cppcheck-suppress duplicateExpression
                  double old_scale = getScale();   // <<<< Line 487
                  const double zoom_step = 1.10;
                  float delta = -0.1 * mouseDelta.y();
                  setZoom((delta < 0) ? zoom() * zoom_step : zoom() / zoom_step);
      
                  // Adjust the pan on the widget so that the point under the mouse
                  // cursor stays in the same location when you zoom in.
                  // cppcheck-suppress duplicateExpression
                  double scale_ratio = getScale() / old_scale; // Line 495
      
       
  • versat

    versat - 2018-12-19

    I can not reproduce the duplicateExpression message with Cppcheck 1.86 or git HEAD using the code snippet. I guess Cppcheck only prints a message when the full code is analyzed?
    Could you try to find a reduced code example where the issue still can be reproduced?
    Or can you try if it still occurrs with git HEAD?

    Regarding the supression:
    Have you tried to move the inline suppression just before line 495? I guess this should be the line that really triggers the message. The marker for line 487 is just a note i think.

     
  • Bob Stafford

    Bob Stafford - 2018-12-20

    I already have a supression comment before 495, but found neither suppressed the message.

    Using the code from HEAD I get a different message

    [layerdisplay/lib/LayerGLWidget.cpp:487] -> [layerdisplay/lib/LayerGLWidget.cpp:495]: (style) Same expression on both sides of '/' because 'getScale()' and 'old_scale' represent the same value.

    and the error is not suppressed.

    I did find that the following will suppress the error

    // cppcheck-suppress *

    I tried to generate a standalone code snippet before posting but also could not reproduce it. I'll try again.

     
  • Bob Stafford

    Bob Stafford - 2018-12-20

    Attached is a cut down version of my original code that still exhibits the problem (false positive and lack of suppression)

    I'm invoking cppcheck (from HEAD) as follows

    cppcheck
    --inline-suppr
    --library=qt.cfg
    --enable=warning,style,performance,portability --platform=unix64 ~/LayerGLWidget.cpp

    and get this message
    
    [/home/bobstaff/LayerGLWidget.cpp:71] -> [/home/bobstaff/LayerGLWidget.cpp:79]: (style) Same expression on both sides of '/' because 'getScale()' and 'old_scale' represent the same value.
    
     

    Last edit: Bob Stafford 2018-12-20
    • versat

      versat - 2018-12-21

      Thanks, now i can reproduce the issue.
      If i change the command line a little bit i get the following result for the file:

      $ ./cppcheck --inline-suppr --library=qt --enable=all --platform=unix64 --template=gcc LayerGLWidget.cpp
      Checking LayerGLWidget.cpp ...
      LayerGLWidget.cpp:79:36: warning: Same expression on both sides of '/' because 'getScale()' and 'old_scale' represent the same value. [knownConditionTrueFalse]
         double scale_ratio = getScale() / old_scale;
                                         ^
      LayerGLWidget.cpp:71:31: note: 'old_scale' is assigned value 'getScale()' here.
         double old_scale = getScale();
                                    ^
      LayerGLWidget.cpp:79:36: note: Same expression on both sides of '/' because 'getScale()' and 'old_scale' represent the same value.
         double scale_ratio = getScale() / old_scale;
                                         ^
      LayerGLWidget.cpp:12:0: warning: The function 'getZoom' is never used. [unusedFunction]
      
      ^
      LayerGLWidget.cpp:63:0: warning: The function 'mouseMoveEvent' is never used. [unusedFunction]
      
      ^
      LayerGLWidget.cpp:16:0: warning: The function 'range' is never used. [unusedFunction]
      
      ^
      LayerGLWidget.cpp:71:0: warning: Unmatched suppression: duplicateExpression [unmatchedSuppression]
         double old_scale = getScale();
      ^
      LayerGLWidget.cpp:79:0: warning: Unmatched suppression: duplicateExpression [unmatchedSuppression]
         double scale_ratio = getScale() / old_scale;
      ^
      

      With the option --template=gcc the id of the message is shown (knownConditionTrueFalse in this case).
      So the suppression in line 70 is useless and can be removed.
      And the suppression in line 78 (or 77 after removing line 70) can be changed to // cppcheck-suppress knownConditionTrueFalse
      Then i get:

      $ ./cppcheck --inline-suppr --library=qt --enable=all --platform=unix64 --template=gcc LayerGLWidget.cpp
      Checking LayerGLWidget.cpp ...
      LayerGLWidget.cpp:12:0: warning: The function 'getZoom' is never used. [unusedFunction]
      
      ^
      LayerGLWidget.cpp:63:0: warning: The function 'mouseMoveEvent' is never used. [unusedFunction]
      
      ^
      LayerGLWidget.cpp:16:0: warning: The function 'range' is never used. [unusedFunction]
      
      ^
      

      While this does not fix the false positive it is at least possible to suppress it.
      IMHO this is a false positive that should be fixed. I will create a ticket for that.

       
      • versat

        versat - 2018-12-21

        I have created a ticket:
        https://trac.cppcheck.net/ticket/8916

         
  • Bob Stafford

    Bob Stafford - 2018-12-21

    Thanks

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.