Menu

False positive: [returnTempReference]

2020-07-16
2020-09-10
  • Andreas Grob

    Andreas Grob - 2020-07-16

    This one produces the error:

    struct Base {
        virtual auto get() -> int & = 0;
    };
    
    struct A : public Base {
        int z = 42;
        auto get() -> int & override { return z; }
        auto getMore() -> int & { return get(); }
    };
    
    int main() {
        A a;
        int &z = a.getMore();
        ++z;
    }
    

    But if you remove the Base struct like this, the error is gone (also removing just the override keyword gets rid of the error):

    /*struct Base {
        virtual auto get() -> int & = 0;
    };*/
    
    struct A /*: public Base*/ {
        int z = 42;
        auto get() -> int & /*override*/ { return z; }
        auto getMore() -> int & { return get(); }
    };
    
    int main() {
        A a;
        int &z = a.getMore();
        ++z;
    }
    

    cppcheck: commit 62702a6
    built with: make MATCHCOMPILER=yes FILESDIR=/usr/share/cppcheck HAVE_RULES=yes

    $ cppcheck main.cpp
    Checking main.cpp ...
    main.cpp:8:41: error: Reference to temporary returned. [returnTempReference]
        auto getMore() -> int & { return get(); }
                                            ^
    
     

    Last edit: Andreas Grob 2020-07-17
  • Daniel Marjamäki

     
  • Andreas Grob

    Andreas Grob - 2020-09-10

    Fixed for me in 48a6852.

     

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.