Menu

constVariablePointer false positive with append, insert, etc

2024-04-26
2024-07-10
  • Steve Albright

    Steve Albright - 2024-04-26

    CPPCheck 2.14, note, third party source is not available when cppcheck runs

    QVector<qobject*> Example()
    {
    QVector<qobject*> exampleVector;</qobject*></qobject*>

    ** const QObject display = new QObject(); // const version gives compile error
    QObject
    display = new QObject(); // non-const version gives cppcheck warning**

    if(nullptr != display)
    {
    exampleVector.append(display);
    }

    return exampleVector;
    }

    Compile error:
    error: no matching member function for call to 'append'
    exampleVector.append(display);
    ~~~~~~~~~~~~~~^~~~~~
    .../qvector.h:142:10: note: candidate function not viable: 1st argument ('const QObject ') would lose const qualifier
    void append(const T &t);
    ^
    .../qvector.h:143:10: note: candidate function not viable: 1st argument ('const QObject
    ') would lose const qualifier
    void append(T &&t);
    ^
    .../qvector.h:144:17: note: candidate function not viable: no known conversion from 'const QObject ' to 'const QVector<qobject *="">' for 1st argument
    inline void append(const QVector<t> &l) { </t></qobject>
    this += l; }

    CPPCheck warning:
    style: constVariablePointer - Variable 'display' can be declared as pointer to const

     
    • amany balawna

      amany balawna - 2024-07-10

      is this issue was fixed ? if yes ,in witch cppcheck version is update??
      i use 2.14.1 version and i have the same problem

       
  • CHR

    CHR - 2024-04-26

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

     
    • Steve Albright

      Steve Albright - 2024-04-26

      Same type of problem with constParameterPointer.

      typedef QList<qobject*> InputList; // some collection in class</qobject*>

      void SomeClass::SomeFunction(SomeType* pParameter)
      {
      Inputs.push_back(pParameter);
      }

       
  • Steve Albright

    Steve Albright - 2024-04-26

    a call to qt layout -> addWidget() with a const pointer produces compile errors too

    cannot initialize a parameter of type 'QWidget ' with an lvalue of type 'const QWidget '

     
  • Steve Albright

    Steve Albright - 2024-05-24

    constParameterReference too

    class ConstParameterReferenceExample
    {
       ConstParameterReferenceExample() = default;
    };
    
    QObject Serialize(ConstParameterReferenceExample& msg)
    {
       QObject buffer;
       buffer << msg;
       return buffer;
    }
    
     
    • CHR

      CHR - 2024-05-24

      operator<< taking a non-const reference is at least uncommon.

       

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.