Menu

False postive: constParameter with gcc ternary?

2021-12-15
2021-12-20
  • Tobias Mark

    Tobias Mark - 2021-12-15

    Example:

    class Foo {};
    void Baz(Foo *arg);
    void Bar(Foo *arg) {
          Baz(arg ?: new Foo());
    }
    

    Leads to:

    style: Parameter 'arg' can be declared with const [constParameter]
    

    Version: 2.6

    This caused by the gcc extension. When using "arg ? arg : new Foo()" it is handled correctly.
    Is this considered a bug or should we stop using this gcc extension?

     
  • Daniel Marjamäki

    Is this considered a bug or should we stop using this gcc extension?

    I consider it a bug. Thanks!

     
  • Daniel Marjamäki

    I can reproduce the false positive with cppcheck-2.6 but not with cppcheck HEAD.

     
  • Tobias Mark

    Tobias Mark - 2021-12-16

    Thanks!

    On a somewhat related subject. I also found an: " AST broken, ternary operator missing operand(s) internalAstError" in some crazy code in my project. Maybe it's also gone with HEAD :)

    class Foo {};
    bool check();
    Foo* bar(){
        std::vector<Foo*> vec;
        std::vector<Foo*>::iterator it1 = vec.begin();
        std::vector<Foo*>::iterator it2 = vec.begin();
        // Error here
        Foo* res = check() ? (delete *it2, *it1) : (delete *it1, *it2);
        return res;
     }
    

    Seems to be the combination of deref and delete. Similar Problems:

        Foo* res = *(check() ? (delete *it2, it1) : (delete *it1, it2));
        std::vector<Foo*>::iterator it3 = true ? (delete *it2, it1) : (delete *it1, it2);
    

    No Problem:

        Foo* res = check() ? (dummy(*it2), *it1) : (dummy(*it1), *it2);
    
        Foo* f1 = new Foo();
        Foo* f2 = new Foo();
        Foo* res = check() ? (delete f1, f2) : (delete f2, f1);
    
     
  • CHR

    CHR - 2021-12-20
     

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.