Skip to content

Commit b54613a

Browse files
committed
Fixed #7046 (constexpr value used as template parameter reported as not used)
1 parent 9d22586 commit b54613a

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
773773
variables.clear();
774774
break;
775775
}
776+
777+
// templates
776778
if (tok->isName() && tok->str().back() == '>') {
777779
// TODO: This is a quick fix to handle when constants are used
778780
// as template parameters. Try to handle this better, perhaps
@@ -823,19 +825,30 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
823825
else if (Token::Match(tok->previous(), "[;{}]")) {
824826
for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) {
825827
if (tok2->varId()) {
828+
// Is this a variable declaration?
826829
const Variable *var = tok2->variable();
827-
if (var && var->nameToken() == tok2) { // Declaration: Skip
828-
tok = tok2->next();
829-
if (Token::Match(tok, "( %name% )")) // Simple initialization through copy ctor
830-
tok = tok->next();
831-
else if (Token::Match(tok, "= %var% ;")) { // Simple initialization
832-
tok = tok->next();
833-
if (!var->isReference())
834-
variables.read(tok->varId(), tok);
835-
} else if (var->typeEndToken()->str() == ">") // Be careful with types like std::vector
836-
tok = tok->previous();
837-
break;
830+
if (!var || var->nameToken() != tok2)
831+
continue;
832+
833+
// Mark template parameters used in declaration as use..
834+
if (tok2->strAt(-1) == ">") {
835+
for (const Token *tok3 = tok; tok3 != tok2; tok3 = tok3->next()) {
836+
if (tok3->varId() > 0U)
837+
variables.use(tok3->varId(), tok3);
838+
}
838839
}
840+
841+
// Skip variable declaration..
842+
tok = tok2->next();
843+
if (Token::Match(tok, "( %name% )")) // Simple initialization through copy ctor
844+
tok = tok->next();
845+
else if (Token::Match(tok, "= %var% ;")) { // Simple initialization
846+
tok = tok->next();
847+
if (!var->isReference())
848+
variables.read(tok->varId(), tok);
849+
} else if (var->typeEndToken()->str() == ">") // Be careful with types like std::vector
850+
tok = tok->previous();
851+
break;
839852
} else if (Token::Match(tok2, "[;({=]"))
840853
break;
841854
}

test/testunusedvar.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,6 +3884,12 @@ class TestUnusedVar : public TestFixture {
38843884
" f<x>();\n"
38853885
"}");
38863886
ASSERT_EQUALS("", errout.str());
3887+
3888+
functionVariableUsage("void f() {\n"
3889+
" constexpr std::size_t ArraySize(5);\n"
3890+
" std::array<int, ArraySize> X; X.dostuff();\n"
3891+
"}");
3892+
ASSERT_EQUALS("", errout.str());
38873893
}
38883894

38893895
void localvarFuncPtr() {

0 commit comments

Comments
 (0)