Menu

uneachable code isn't detected by cppcheck

2024-05-17
2024-06-06
  • Rob Deckers

    Rob Deckers - 2024-05-17

    I would expect that Cppcheck 2.14.0 was able to detect this case:

    bool someClasss::function([[maybe_unused]] State currentState, [[maybe_unused]] State pendingState)
    {
        return true;
        < rest of code with multiple returns>
        }
    
     
  • CHR

    CHR - 2024-05-17

    This works:

    bool someClasss::function([[maybe_unused]] State currentState, [[maybe_unused]] State pendingState)
    {
        return true;
        g();
        return false;
    }
    
    foo.cpp:4:5: style: Statements following 'return' will never be executed. [unreachableCode]
        g();
        ^
    
     
  • Rob Deckers

    Rob Deckers - 2024-06-06

    It's indeed a bit more funcy ;)

    It does detect a function call and a switch statement, but not an if-statement.
    This is detected:

    
    

    {
    return true;
    somefunc();
    return false;
    }

    
    

    But this is not:

    {
        return true;
        if (State == ERROR) {
            return true;
        }
        return false;
    }
    

    also an if(true) is not detected.

     

    Last edit: Rob Deckers 2024-06-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.