Skip to content

Commit 0003ad2

Browse files
Fix #13282 FN functionConst with unknown base class (danmar#6990)
1 parent 36513ef commit 0003ad2

15 files changed

+58
-33
lines changed

cfg/qt.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5359,7 +5359,7 @@
53595359
<define name="Q_LIKELY(expr)" value="expr"/>
53605360
<define name="Q_NAMESPACE" value=""/>
53615361
<define name="Q_NULLPTR" value="NULL"/>
5362-
<define name="Q_OBJECT" value="static void qt_static_metacall(QObject*,QMetaObject::Call,int,void**);const MetaObject* metaObject() const;void* qt_metacast(const char*);int qt_metacall(QMetaObject::Call,int,void**);"/>
5362+
<define name="Q_OBJECT" value="static void qt_static_metacall(QObject*,QMetaObject::Call,int,void**);virtual const MetaObject* metaObject() const;virtual void* qt_metacast(const char*);virtual int qt_metacall(QMetaObject::Call,int,void**);"/>
53635363
<define name="Q_PRIVATE_SLOT(d, signature)" value=""/>
53645364
<define name="Q_SLOTS" value=""/>
53655365
<!-- Treat as variadic macro to avoid preprocessorErrorDirective -->

gui/codeeditstylecontrols.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void SelectColorButton::setColor(const QColor& color)
6565
}
6666

6767
// cppcheck-suppress unusedFunction
68-
const QColor& SelectColorButton::getColor()
68+
const QColor& SelectColorButton::getColor() const
6969
{
7070
return mColor;
7171
}
@@ -122,7 +122,7 @@ void SelectFontWeightCombo::setWeight(QFont::Weight weight)
122122
}
123123

124124
// cppcheck-suppress unusedFunction
125-
const QFont::Weight& SelectFontWeightCombo::getWeight()
125+
const QFont::Weight& SelectFontWeightCombo::getWeight() const
126126
{
127127
return mWeight;
128128
}

gui/codeeditstylecontrols.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SelectColorButton : public QPushButton {
3636
explicit SelectColorButton(QWidget* parent);
3737

3838
void setColor(const QColor& color);
39-
const QColor& getColor();
39+
const QColor& getColor() const;
4040

4141
signals:
4242
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
@@ -57,7 +57,7 @@ class SelectFontWeightCombo : public QComboBox {
5757
explicit SelectFontWeightCombo(QWidget* parent);
5858

5959
void setWeight(QFont::Weight weight);
60-
const QFont::Weight& getWeight();
60+
const QFont::Weight& getWeight() const;
6161

6262
signals:
6363
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code

gui/codeeditstyledialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void StyleEditDialog::updateStyle()
235235
mSampleEditor->setStyle(mStyleOutgoing);
236236
}
237237

238-
CodeEditorStyle StyleEditDialog::getStyle()
238+
CodeEditorStyle StyleEditDialog::getStyle() const
239239
{
240240
return mStyleOutgoing;
241241
}

gui/codeeditstyledialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class StyleEditDialog : public QDialog {
4343
explicit StyleEditDialog(const CodeEditorStyle& newStyle,
4444
QWidget *parent = nullptr);
4545

46-
CodeEditorStyle getStyle();
46+
CodeEditorStyle getStyle() const;
4747

4848
private:
4949
void updateControls();

gui/resultstree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ void ResultsTree::setCheckDirectory(const QString &dir)
15241524
}
15251525

15261526

1527-
const QString& ResultsTree::getCheckDirectory()
1527+
const QString& ResultsTree::getCheckDirectory() const
15281528
{
15291529
return mCheckPath;
15301530
}

gui/resultstree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class ResultsTree : public QTreeView {
135135
* @return Directory containing source files
136136
*/
137137

138-
const QString& getCheckDirectory();
138+
const QString& getCheckDirectory() const;
139139

140140
/**
141141
* @brief Check if there are any visible results in view.

gui/resultsview.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ void ResultsView::printPreview()
262262
dialog.exec();
263263
}
264264

265-
void ResultsView::print(QPrinter* printer)
265+
void ResultsView::print(QPrinter* printer) const
266266
{
267267
if (!hasResults()) {
268268
QMessageBox msgBox;
@@ -300,7 +300,7 @@ void ResultsView::setCheckDirectory(const QString &dir)
300300
mUI->mTree->setCheckDirectory(dir);
301301
}
302302

303-
QString ResultsView::getCheckDirectory()
303+
QString ResultsView::getCheckDirectory() const
304304
{
305305
return mUI->mTree->getCheckDirectory();
306306
}

gui/resultsview.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class ResultsView : public QWidget {
139139
* @return Directory containing source files
140140
*/
141141

142-
QString getCheckDirectory();
142+
QString getCheckDirectory() const;
143143

144144
/**
145145
* Set settings used in checking
@@ -331,7 +331,7 @@ public slots:
331331
* @brief Slot printing the current report to the printer.
332332
* @param printer The printer used for printing the report.
333333
*/
334-
void print(QPrinter* printer);
334+
void print(QPrinter* printer) const;
335335

336336
/**
337337
* @brief Slot opening a print preview dialog

lib/checkclass.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,8 @@ void CheckClass::checkConst()
21722172
}
21732173

21742174
// check if base class function is virtual
2175-
if (!scope->definedType->derivedFrom.empty() && func.isImplicitlyVirtual(true))
2175+
bool foundAllBaseClasses = true;
2176+
if (!scope->definedType->derivedFrom.empty() && func.isImplicitlyVirtual(true, &foundAllBaseClasses) && foundAllBaseClasses)
21762177
continue;
21772178

21782179
MemberAccess memberAccessed = MemberAccess::NONE;
@@ -2200,9 +2201,9 @@ void CheckClass::checkConst()
22002201
functionName += "]";
22012202

22022203
if (func.isInline())
2203-
checkConstError(func.token, classname, functionName, suggestStatic);
2204+
checkConstError(func.token, classname, functionName, suggestStatic, foundAllBaseClasses);
22042205
else // not inline
2205-
checkConstError2(func.token, func.tokenDef, classname, functionName, suggestStatic);
2206+
checkConstError2(func.token, func.tokenDef, classname, functionName, suggestStatic, foundAllBaseClasses);
22062207
}
22072208
}
22082209
}
@@ -2277,16 +2278,21 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const
22772278
// not found in this class
22782279
if (!scope->definedType->derivedFrom.empty()) {
22792280
// check each base class
2281+
bool foundAllBaseClasses = true;
22802282
for (const Type::BaseInfo & i : scope->definedType->derivedFrom) {
22812283
// find the base class
22822284
const Type *derivedFrom = i.type;
2285+
if (!derivedFrom)
2286+
foundAllBaseClasses = false;
22832287

22842288
// find the function in the base class
22852289
if (derivedFrom && derivedFrom->classScope && derivedFrom->classScope != scope) {
22862290
if (isMemberVar(derivedFrom->classScope, tok))
22872291
return true;
22882292
}
22892293
}
2294+
if (!foundAllBaseClasses)
2295+
return true;
22902296
}
22912297

22922298
return false;
@@ -2609,35 +2615,41 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
26092615
return true;
26102616
}
26112617

2612-
void CheckClass::checkConstError(const Token *tok, const std::string &classname, const std::string &funcname, bool suggestStatic)
2618+
void CheckClass::checkConstError(const Token *tok, const std::string &classname, const std::string &funcname, bool suggestStatic, bool foundAllBaseClasses)
26132619
{
2614-
checkConstError2(tok, nullptr, classname, funcname, suggestStatic);
2620+
checkConstError2(tok, nullptr, classname, funcname, suggestStatic, foundAllBaseClasses);
26152621
}
26162622

2617-
void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname, bool suggestStatic)
2623+
void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname, bool suggestStatic, bool foundAllBaseClasses)
26182624
{
26192625
std::list<const Token *> toks{ tok1 };
26202626
if (tok2)
26212627
toks.push_back(tok2);
2622-
if (!suggestStatic)
2628+
if (!suggestStatic) {
2629+
const std::string msg = foundAllBaseClasses ?
2630+
"Technically the member function '$symbol' can be const.\nThe member function '$symbol' can be made a const " :
2631+
"Either there is a missing 'override', or the member function '$symbol' can be const.\nUnless it overrides a base class member, the member function '$symbol' can be made a const ";
26232632
reportError(toks, Severity::style, "functionConst",
26242633
"$symbol:" + classname + "::" + funcname +"\n"
2625-
"Technically the member function '$symbol' can be const.\n"
2626-
"The member function '$symbol' can be made a const "
2634+
+ msg +
26272635
"function. Making this function 'const' should not cause compiler errors. "
26282636
"Even though the function can be made const function technically it may not make "
26292637
"sense conceptually. Think about your design and the task of the function first - is "
26302638
"it a function that must not change object internal state?", CWE398, Certainty::inconclusive);
2631-
else
2639+
}
2640+
else {
2641+
const std::string msg = foundAllBaseClasses ?
2642+
"Technically the member function '$symbol' can be static (but you may consider moving to unnamed namespace).\nThe member function '$symbol' can be made a static " :
2643+
"Either there is a missing 'override', or the member function '$symbol' can be static.\nUnless it overrides a base class member, the member function '$symbol' can be made a static ";
26322644
reportError(toks, Severity::performance, "functionStatic",
26332645
"$symbol:" + classname + "::" + funcname +"\n"
2634-
"Technically the member function '$symbol' can be static (but you may consider moving to unnamed namespace).\n"
2635-
"The member function '$symbol' can be made a static "
2646+
+ msg +
26362647
"function. Making a function static can bring a performance benefit since no 'this' instance is "
26372648
"passed to the function. This change should not cause compiler errors but it does not "
26382649
"necessarily make sense conceptually. Think about your design and the task of the function first - "
26392650
"is it a function that must not access members of class instances? And maybe it is more appropriate "
26402651
"to move this function to an unnamed namespace.", CWE398, Certainty::inconclusive);
2652+
}
26412653
}
26422654

26432655
//---------------------------------------------------------------------------

0 commit comments

Comments
 (0)