Menu

if (0) / Unreachable code

2021-09-01
2021-09-06
  • Jacques van der Linde

    Hi,

    Can't cppcheck warn me about "unreachable code" in a if (0) statement, for instance:

    if (0)
    {
         dosomething..
     }
    
     
    👍
    1
  • Daniel Marjamäki

    to avoid false positives, we do not warn about stylistic issues if it seems to be by intention. in your code it looks intentional that the condition is false.

    Try something like if (x==0) when the value of x must be 0. for instance you may have assigned x to 0 before the condition. or the code is placed in a conditional scope that is only executed if x is 0.

     

    Last edit: Daniel Marjamäki 2021-09-01
  • Jacques van der Linde

    But why not give the user the option if he/she wants to have a warning or not (suppress), even if the unreachable code was made intentionally.

    For example, for testing purposes I did intentionally changed operational code from if (1) to if (0), BUT forgot to undo the change after the testing, and only found out months later!!!!

     
    • Daniel Marjamäki

      I don't want to have such option in cppcheck I believe we skip the warning for good reasons to avoid noise.

      But an addon can be created quite easily that would write this warning. I feel that it can be added in cppcheck repo and distributed with cppcheck.

       

      Last edit: Daniel Marjamäki 2021-09-06

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.