Menu

False negative: empty() call suppresses later accessMoved warning for decltype string

Rodri
2 days ago
2 days ago
  • Rodri

    Rodri - 2 days ago

    Hi, I found a false negative regarding the rule accessMoved.

    Cppcheck reports accessMoved when a moved std::string declared through decltype(std::string{"test"}) is accessed directly. However, if that moved-from object is first queried with empty(), cppcheck misses the later access.

    #include <iostream>
    #include <string>
    #include <utility>
    
    int main() {
      decltype(std::string{"test"}) s = "test";
      std::string n = std::move(s);
      if (s.empty()) {
      }
      std::cout << s << '\n' << std::flush;
      return 0;
    }
    

    Here, decltype(std::string{"test"}) is std::string. The object s is moved into n, and then s is accessed by s.empty() and by std::cout << s.

    If the if (s.empty()) block is removed, cppcheck reports accessMoved for the stream insertion. A plain std::string s version also reports accessMoved, even with the empty() call. The warning is missed for the combination of the decltype(std::string{"test"}) declaration and the empty() query before the later access.

    Actual result

    Cppcheck reports no accessMoved warning.

    Expected result

    Cppcheck should report accessMoved for the access to s after std::move(s).

    Verification

    The issue was reproduced with:

    cppcheck --output-format=xmlv2 --enable=all access_moved_decltype.cpp
    

    Version: 2.21.0

     
  • CHR

    CHR - 2 days ago

    I'm getting a warning. Are you passing --inconclusive?

    test.cpp:5:7: warning: inconclusive: Access of moved variable 's'. [accessMoved]
      if (s.empty()) {
          ^
    
     

Log in to post a comment.

Monday.com Logo