From a463699d24fe405d6bc59107de03da74980d92c3 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 19 Mar 2024 15:19:52 +0100 Subject: [PATCH 1/2] Fix #12529 false negative: unreadVariable with library type --- cfg/qt.cfg | 6 +++--- lib/checkunusedvar.cpp | 11 +++++++++-- test/cfg/qt.cpp | 22 +++++++++++----------- test/cfg/wxwidgets.cpp | 24 ++++++++++++------------ 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/cfg/qt.cfg b/cfg/qt.cfg index 69bc205a316..8c2d34073b0 100644 --- a/cfg/qt.cfg +++ b/cfg/qt.cfg @@ -5236,12 +5236,12 @@ QApplication QMutexLocker - QRectF - QSizeF - QPointF QRect + QRectF QSize + QSizeF QPoint + QPointF QRegion diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 7d7ccd7b023..57d68883304 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -713,10 +713,17 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const type = Variables::pointer; else if (mTokenizer->isC() || i->typeEndToken()->isStandardType() || - isRecordTypeWithoutSideEffects(i->type()) || - mSettings->library.detectContainer(i->typeStartToken()) || i->isStlType()) type = Variables::standard; + else { + if (isRecordTypeWithoutSideEffects(i->type()) || mSettings->library.detectContainer(i->typeStartToken())) + type = Variables::standard; + else { + Library::TypeCheck typeCheck = mSettings->library.getTypeCheck("unusedvar", i->typeStartToken()->str()); + if (typeCheck == Library::TypeCheck::check) + type = Variables::standard; + } + } if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken())) continue; const Token* defValTok = i->nameToken()->next(); diff --git a/test/cfg/qt.cpp b/test/cfg/qt.cpp index 5fcb803e372..b6cbff9fe96 100644 --- a/test/cfg/qt.cpp +++ b/test/cfg/qt.cpp @@ -23,19 +23,19 @@ #include #include #include -#include -#include -#include -#include #include +#include #include +#include #include +#include +#include #include void unreadVariable_QRegion(const int x, const QRegion::RegionType type, const QPolygon &polygon, const QBitmap &bm, const QRegion ®ion, const Qt::FillRule fillRule) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable QRegion a; // cppcheck-suppress unreadVariable QRegion b{}; @@ -53,7 +53,7 @@ void unreadVariable_QRegion(const int x, const QRegion::RegionType type, const Q void unreadVariable_QPoint(const QPoint &s) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable QPoint a; // cppcheck-suppress unreadVariable QPoint b{}; @@ -67,7 +67,7 @@ void unreadVariable_QPoint(const QPoint &s) void unreadVariable_QPointF(const QPointF &s) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable QPointF a; // cppcheck-suppress unreadVariable QPointF b{}; @@ -81,7 +81,7 @@ void unreadVariable_QPointF(const QPointF &s) void unreadVariable_QSizeF(const QSize &s) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable QSizeF a; // cppcheck-suppress unreadVariable QSizeF b{}; @@ -95,7 +95,7 @@ void unreadVariable_QSizeF(const QSize &s) void unreadVariable_QSize(const QSize &s) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable QSize a; // cppcheck-suppress unreadVariable QSize b{}; @@ -108,7 +108,7 @@ void unreadVariable_QSize(const QSize &s) } void unreadVariable_QRect(const QPoint &topLeft, const QSize &size, const QPoint &bottomRight, const int x) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable QRect a; // cppcheck-suppress unreadVariable QRect b{}; @@ -123,7 +123,7 @@ void unreadVariable_QRect(const QPoint &topLeft, const QSize &size, const QPoint } void unreadVariable_QRectF(const QPointF &topLeft, const QSizeF &size, const QPointF &bottomRight, const QRectF &rect, const qreal x) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable QRectF a; // cppcheck-suppress unreadVariable QRectF b{}; diff --git a/test/cfg/wxwidgets.cpp b/test/cfg/wxwidgets.cpp index 85e233cdf9a..95b240fd688 100644 --- a/test/cfg/wxwidgets.cpp +++ b/test/cfg/wxwidgets.cpp @@ -36,15 +36,15 @@ #include #include -void unreadVariable_wxAcceleratorEntry() +void unusedVariable_wxAcceleratorEntry() { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxAcceleratorEntry a; } void unreadVariable_wxDateSpan(const int x) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxDateSpan a; // cppcheck-suppress unreadVariable wxDateSpan b{x}; @@ -58,7 +58,7 @@ void unreadVariable_wxDateSpan(const int x) void unreadVariable_wxTimeSpan(const long x, const wxLongLong y) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxTimeSpan a; // cppcheck-suppress unreadVariable wxTimeSpan b{}; @@ -80,7 +80,7 @@ void unreadVariable_wxFileType(const wxFileTypeInfo &info) void unreadVariable_wxPosition(const int x) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxPosition a; // cppcheck-suppress unreadVariable wxPosition b{}; @@ -90,7 +90,7 @@ void unreadVariable_wxPosition(const int x) void unreadVariable_wxRegEx(const wxString &expr, const int flags) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxRegEx a; // cppcheck-suppress unreadVariable wxRegEx b{expr}; @@ -100,7 +100,7 @@ void unreadVariable_wxRegEx(const wxString &expr, const int flags) void unreadVariable_wxRegion(const wxCoord x, const wxPoint &pt, const wxRect &rect, const wxRegion ®ion, const wxBitmap &bmp) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxRegion a; // cppcheck-suppress unreadVariable wxRegion b{}; @@ -118,7 +118,7 @@ void unreadVariable_wxRegion(const wxCoord x, const wxPoint &pt, const wxRect &r void unreadVariable_wxVersionInfo(const wxString &name, const int major, const int minor, const int micro, const wxString &description, const wxString ©right) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxVersionInfo a; // cppcheck-suppress unreadVariable wxVersionInfo b(name); @@ -136,7 +136,7 @@ void unreadVariable_wxVersionInfo(const wxString &name, const int major, const i void unreadVariable_wxSize(const wxSize &s) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxSize a; // cppcheck-suppress unreadVariable wxSize b{}; @@ -150,7 +150,7 @@ void unreadVariable_wxSize(const wxSize &s) void unreadVariable_wxPoint(const wxRealPoint &rp, const int x, const int y) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxPoint a; // cppcheck-suppress unreadVariable wxPoint b{}; @@ -168,7 +168,7 @@ void unreadVariable_wxPoint(const wxRealPoint &rp, const int x, const int y) void unreadVariable_wxRealPoint(const wxPoint &pt, const double x, const double y) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxRealPoint a; // cppcheck-suppress unreadVariable wxRealPoint b{}; @@ -186,7 +186,7 @@ void unreadVariable_wxRealPoint(const wxPoint &pt, const double x, const double void unreadVariable_wxRect(const int x, const wxPoint &pt, const wxSize &sz) { - // cppcheck-suppress unreadVariable + // cppcheck-suppress unusedVariable wxRect a; // cppcheck-suppress unreadVariable wxRect b{}; From 926f99baceff27a6c6d025b7702c2bf101ed496c Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 21 Mar 2024 10:57:18 +0100 Subject: [PATCH 2/2] Simplify --- lib/checkunusedvar.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 57d68883304..e6d6ccd03d6 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -713,17 +713,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const type = Variables::pointer; else if (mTokenizer->isC() || i->typeEndToken()->isStandardType() || - i->isStlType()) + i->isStlType() || + isRecordTypeWithoutSideEffects(i->type()) || + mSettings->library.detectContainer(i->typeStartToken()) || + mSettings->library.getTypeCheck("unusedvar", i->typeStartToken()->str()) == Library::TypeCheck::check) type = Variables::standard; - else { - if (isRecordTypeWithoutSideEffects(i->type()) || mSettings->library.detectContainer(i->typeStartToken())) - type = Variables::standard; - else { - Library::TypeCheck typeCheck = mSettings->library.getTypeCheck("unusedvar", i->typeStartToken()->str()); - if (typeCheck == Library::TypeCheck::check) - type = Variables::standard; - } - } + if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken())) continue; const Token* defValTok = i->nameToken()->next();