add support for unused const local variables
This commit is contained in:
parent
4e6800c474
commit
9a4447c835
|
@ -1888,6 +1888,18 @@ void CheckOther::functionVariableUsage()
|
||||||
tok = tok->next();
|
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 declaration with possible initialization
|
||||||
// std::string s; std::string s = "string";
|
// std::string s; std::string s = "string";
|
||||||
else if (Token::Match(tok, "[;{}] static| std :: string %var% ;|="))
|
else if (Token::Match(tok, "[;{}] static| std :: string %var% ;|="))
|
||||||
|
|
|
@ -101,6 +101,7 @@ private:
|
||||||
TEST_CASE(localvararray1); // ticket #2780
|
TEST_CASE(localvararray1); // ticket #2780
|
||||||
TEST_CASE(localvarstring1);
|
TEST_CASE(localvarstring1);
|
||||||
TEST_CASE(localvarstring2); // ticket #2929
|
TEST_CASE(localvarstring2); // ticket #2929
|
||||||
|
TEST_CASE(localvarconst);
|
||||||
|
|
||||||
// Don't give false positives for variables in structs/unions
|
// Don't give false positives for variables in structs/unions
|
||||||
TEST_CASE(localvarStruct1);
|
TEST_CASE(localvarStruct1);
|
||||||
|
@ -2930,6 +2931,14 @@ private:
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: s\n"
|
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: s\n"
|
||||||
"[test.cpp:3]: (style) Unused variable: i\n", errout.str());
|
"[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)
|
REGISTER_TEST(TestUnusedVar)
|
||||||
|
|
Loading…
Reference in New Issue