Menu

False positive: missingReturn and static_assert

2022-07-18
2022-07-18
  • Andrey Alekseenko

    Using the latest commit (71d386819e70ea13fbd6d1a5be073ad8fe05cff6), the following code triggers an error, despite the only possible codepath returning a value:

    template <int i> int decrement() {
      static_assert(i == 1);
      if constexpr (i == 1) {
        return 0;
      }
    }
    
    $ ~/cppcheck/cppcheck test.cpp 
    Checking test.cpp ...
    test.cpp:5:3: error: Found a exit path from function with non-void return type that has missing return statement [missingReturn]
      }
      ^
    
     
  • CHR

    CHR - 2022-07-18

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11204

    Can't you remove the if (...), since you already have the assert?

     
  • Andrey Alekseenko

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11204

    Thanks!

    Can't you remove the if (...), since you already have the assert?

    That is definitely possible here. But in the real code this example is based on, there was a long chain of if - else if - else if. The last else if could be replaced by simple else there too, but it's debatable which approach is more readable.

     

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.