From b30fb270c19289eaa42bdf8e3c8f6f03b530b3af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Sat, 28 Sep 2024 13:32:36 +0200 Subject: [PATCH] naming + simpler tests + remove redundant checks --- lib/checkunusedvar.cpp | 12 +++++------ test/testunusedvar.cpp | 49 ++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index c8a165fa1ed..5cc5485ec81 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1535,11 +1535,11 @@ void CheckUnusedVar::checkStructMemberUsage() continue; // Keep track of number of non-static members encountered so far - int nummembs = 0; + int memberCount = 0; for (const Variable &var : scope.varlist) { if (!var.isStatic()) { - nummembs++; + memberCount++; } // only warn for variables without side effects @@ -1551,18 +1551,18 @@ void CheckUnusedVar::checkStructMemberUsage() // Check if the struct member variable is used anywhere in the file bool use = false; for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { - if (Token::Match(tok, "%name% {") && !Token::Match(tok->previous(), "class|struct") && tok->str() == scope.className && tok->linkAt(1)) { + if (Token::Match(tok, "%name% {") && !Token::Match(tok->previous(), "class|struct") && tok->str() == scope.className) { const Token *inner = tok->next()->astOperand2(); // Find number of initialized members in braced initalizer - int initmembs = 0; + int initializedMemberCount = 0; while (inner) { - initmembs++; + initializedMemberCount++; if (!inner->isInitComma()) { break; } inner = inner->astOperand1(); } - if (initmembs >= nummembs) { + if (initializedMemberCount >= memberCount) { use = true; break; } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 82d37d70d6d..5626987e37e 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1967,49 +1967,38 @@ class TestUnusedVar : public TestFixture { void structmember26() { // #10305 checkStructMemberUsage("struct S {\n" - " const int* p{};\n" + " int p;\n" "};\n" - "struct C {\n" - " int i{};\n" - " S f() const {\n" - " return S{ &i };\n" - " }\n" - "};\n"); + "S f() const {\n" + " return S{ 0 };\n" + "}\n"); ASSERT_EQUALS("", errout_str()); } void structmember27() { checkStructMemberUsage("struct S {\n" - " const int* p{};\n" - " static int* q;\n" - " const int* r{};\n" - "};\n" - "struct C {\n" - " int i{};\n" - " int j{};\n" - " S f() const {\n" - " return S{ &i, &j };\n" - " }\n" + " int p;\n" + " static int q;\n" + " int r;\n" "};\n" - "int *S::q = 0;\n"); + "S f() const {\n" + " return S{ 0, 0 };\n" + "}\n" + "int S::q = 0;\n"); ASSERT_EQUALS("", errout_str()); } void structmember28() { checkStructMemberUsage("struct S {\n" - " const int* p{};\n" - " static int* q;\n" - " const int* r{};\n" - " const int* s{};" - "};\n" - "struct C {\n" - " int i{};\n" - " int j{};\n" - " S f() const {\n" - " return S{ &i, &j };\n" - " }\n" + " int p;\n" + " static int q;\n" + " int r;\n" + " int s;" "};\n" - "int *S::q = 0;\n"); + "S f() const {\n" + " return S{ 0, 0 };\n" + "}\n" + "int S::q = 0;\n"); ASSERT_EQUALS("[test.cpp:5]: (style) struct member 'S::s' is never used.\n", errout_str()); }