Menu

False positive "unusedStructMember" when wrapped in std::optional

2023-02-06
2023-02-06
  • Dan Workman

    Dan Workman - 2023-02-06

    Found in 2.9 and 2.10

    #include <iostream>
    #include <optional>
    
    struct Result {
      bool isValid{false};
    };
    
    std::optional<Result> GetResult()
    {
      return Result{true};
    }
    
    int main()
    {
      auto res = GetResult();
    
      if (res.has_value())
        std::cout << res->isValid;
    
      return 0;
    }
    

    Output:

    $ cppcheck --std=c++17 --enable=all --platform=win64 cppcheck_false_positives.cpp
    Checking cppcheck_false_positives.cpp ...
    cppcheck_false_positives.cpp:8:8: style: struct member 'Result::isValid' is never used. [unusedStructMember]
      bool isValid{false};
           ^
    
     
  • CHR

    CHR - 2023-02-06

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

     
    👍
    1

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.