diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4af77ef25..f1810c851 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1888,6 +1888,18 @@ void CheckOther::functionVariableUsage() tok = tok->next(); } + // standard const type declaration + // const int i = x; + if (Token::Match(tok, "[;{}] const %type% %var% =")) + { + tok = tok->next()->next(); + + if (tok->isStandardType() || isRecordTypeWithoutSideEffects(tok->next())) + variables.addVar(tok->next(), Variables::standard, info, true); + + tok = tok->next(); + } + // std::string declaration with possible initialization // std::string s; std::string s = "string"; else if (Token::Match(tok, "[;{}] static| std :: string %var% ;|=")) diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 34fbdd5be..6d73ee5d5 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -101,6 +101,7 @@ private: TEST_CASE(localvararray1); // ticket #2780 TEST_CASE(localvarstring1); TEST_CASE(localvarstring2); // ticket #2929 + TEST_CASE(localvarconst); // Don't give false positives for variables in structs/unions TEST_CASE(localvarStruct1); @@ -2930,6 +2931,14 @@ private: ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: s\n" "[test.cpp:3]: (style) Unused variable: i\n", errout.str()); } + + void localvarconst() + { + functionVariableUsage("void foo() {\n" + " const bool b = true;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'b' is assigned a value that is never used\n", errout.str()); + } }; REGISTER_TEST(TestUnusedVar)