Menu

False positive selfAssignment

CHR
2020-06-22
2020-12-29
  • CHR

    CHR - 2020-06-22

    In our code base, we are getting some false positives related to selfAssignment. I have tried to distill it into a minimal example. With version 2.1 and --enable=all, the code below results in

    warning: Redundant assignment of '*this' to itself. [selfAssignment]
    
    class Foo {
    int i = 0;
    void nonconst() { i = 1;}
    public:
    void bar() {
      Foo old = *this;
      nonconst();
      *this = old;}
    };
    
    int main() {
    Foo f;
    f.bar();
    return 0;
    }
    
     
  • Daniel Marjamäki

    Thanks for reporting! I have created this ticket: https://trac.cppcheck.net/ticket/9785

     
  • Daniel Marjamäki

    hmm.. I fail to reproduce this FP with cppcheck-2.0 and cppcheck-2.2 and cppcheck-2.2 ... can you still reproduce the problem?

     
  • CHR

    CHR - 2020-12-29

    I just ran 2.1 again and got this:

    "c:\Program Files\Cppcheck\cppcheck.exe" --enable=all --inconclusive sa.cpp
    Checking sa.cpp ...
    sa.cpp:8:9: warning: Redundant assignment of '*this' to itself. [selfAssignment]
      *this = old;}
            ^
    

    Version 2.3 does not report the warning, I guess the problem was fixed sometime in between 2.1 and 2.2?

     
  • Daniel Marjamäki

    ok thanks! I close it.

     

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.