From 8465dcde13be78cd5379907790acc77e8fc91920 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 21 Mar 2024 18:20:55 +0100 Subject: [PATCH 1/3] Partial fix for #12529 FN unreadVariable/unusedVariable with library type --- lib/checkunusedvar.cpp | 5 ++++- test/testunusedvar.cpp | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 7ab85367eb5..15f989d8a99 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1201,8 +1201,9 @@ void CheckUnusedVar::checkFunctionVariableUsage() if (!isAssignment && !isInitialization && !isIncrementOrDecrement) continue; + bool isTrivialInit = false; if (isInitialization && Token::Match(tok, "%var% { }")) // don't warn for trivial initialization - continue; + isTrivialInit = true; if (isIncrementOrDecrement && tok->astParent() && precedes(tok, tok->astOperand1())) continue; @@ -1308,6 +1309,8 @@ void CheckUnusedVar::checkFunctionVariableUsage() reportLibraryCfgError(tok, bailoutTypeName); continue; } + if (isTrivialInit && findExpressionChanged(expr, start, scopeEnd, mSettings)) + continue; // warn if (!expr->variable() || !expr->variable()->isMaybeUnused()) diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 749dd206b6b..a1219c43f05 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -3523,7 +3523,7 @@ class TestUnusedVar : public TestFixture { " r += d;\n" " }\n" "}\n"); - ASSERT_EQUALS("", errout_str()); + /*ASSERT_EQUALS("", errout_str());*/errout_str(); functionVariableUsage("int func() {\n" " std::mutex m;\n" @@ -3778,7 +3778,7 @@ class TestUnusedVar : public TestFixture { functionVariableUsage("void f() {\n" " S* s{};\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", "", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout_str()); functionVariableUsage("int f() {\n" " int i = 0, j = 1;\n" @@ -5137,7 +5137,7 @@ class TestUnusedVar : public TestFixture { " b = false;\n" " if (*p) {}\n" "}\n"); - ASSERT_EQUALS("", errout_str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'j' is assigned a value that is never used.\n", errout_str()); } void localvaralias22() { // #11139 From 20472bcfd872bdaadf5e1a7c5d5efdc182ea03e0 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 21 Mar 2024 18:27:59 +0100 Subject: [PATCH 2/3] Undo --- test/testunusedvar.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index a1219c43f05..8f4a4969418 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -3523,7 +3523,7 @@ class TestUnusedVar : public TestFixture { " r += d;\n" " }\n" "}\n"); - /*ASSERT_EQUALS("", errout_str());*/errout_str(); + ASSERT_EQUALS("", errout_str()); functionVariableUsage("int func() {\n" " std::mutex m;\n" From d2e025806161f978591d4931e4362dde70882682 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 21 Mar 2024 18:37:33 +0100 Subject: [PATCH 3/3] Fix test --- test/cfg/wxwidgets.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/cfg/wxwidgets.cpp b/test/cfg/wxwidgets.cpp index 5481edee8d3..0852aaba996 100644 --- a/test/cfg/wxwidgets.cpp +++ b/test/cfg/wxwidgets.cpp @@ -87,7 +87,7 @@ void unreadVariable_wxTimeSpan(const long x, const wxLongLong y) { // cppcheck-suppress unusedVariable wxTimeSpan a; - // TODO cppcheck-suppress unreadVariable + // cppcheck-suppress unreadVariable wxTimeSpan b{}; // cppcheck-suppress unreadVariable wxTimeSpan c{x}; @@ -109,7 +109,7 @@ void unreadVariable_wxPosition(const int x) { // cppcheck-suppress unusedVariable wxPosition a; - // TODO cppcheck-suppress unreadVariable + // cppcheck-suppress unreadVariable wxPosition b{}; // cppcheck-suppress unreadVariable wxPosition c{x,x}; @@ -129,7 +129,7 @@ void unreadVariable_wxRegion(const wxCoord x, const wxPoint &pt, const wxRect &r { // cppcheck-suppress unusedVariable wxRegion a; - // TODO cppcheck-suppress unreadVariable + // cppcheck-suppress unreadVariable wxRegion b{}; // cppcheck-suppress unreadVariable wxRegion c{x,x,x,x}; @@ -165,7 +165,7 @@ void unreadVariable_wxSize(const wxSize &s) { // cppcheck-suppress unusedVariable wxSize a; - // TODO cppcheck-suppress unreadVariable + // cppcheck-suppress unreadVariable wxSize b{}; // cppcheck-suppress unreadVariable wxSize c{4, 2}; @@ -179,7 +179,7 @@ void unreadVariable_wxPoint(const wxRealPoint &rp, const int x, const int y) { // cppcheck-suppress unusedVariable wxPoint a; - // TODO cppcheck-suppress unreadVariable + // cppcheck-suppress unreadVariable wxPoint b{}; // cppcheck-suppress unreadVariable wxPoint c{4, 2}; @@ -197,7 +197,7 @@ void unreadVariable_wxRealPoint(const wxPoint &pt, const double x, const double { // cppcheck-suppress unusedVariable wxRealPoint a; - // TODO cppcheck-suppress unreadVariable + // cppcheck-suppress unreadVariable wxRealPoint b{}; // cppcheck-suppress unreadVariable wxRealPoint c{4.0, 2.0}; @@ -215,7 +215,7 @@ void unreadVariable_wxRect(const int x, const wxPoint &pt, const wxSize &sz) { // cppcheck-suppress unusedVariable wxRect a; - // TODO cppcheck-suppress unreadVariable + // cppcheck-suppress unreadVariable wxRect b{}; // cppcheck-suppress unreadVariable wxRect c{x,x,x,x};