Skip to content

Commit 028baa2

Browse files
Fix #14199 FN unusedVariable with templated type (regression) (#8013)
1 parent f65e41b commit 028baa2

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/checkunusedvar.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ class Variables {
148148
void clear() {
149149
mVarUsage.clear();
150150
}
151+
void clearConst() {
152+
std::set<int> keys;
153+
for (const std::pair<const nonneg int, Variables::VariableUsage>& var : mVarUsage) {
154+
if (var.second._var && var.second._var->isConst())
155+
keys.emplace(var.first);
156+
}
157+
for (const int key : keys)
158+
mVarUsage.erase(key);
159+
}
151160
const std::map<nonneg int, VariableUsage> &varUsage() const {
152161
return mVarUsage;
153162
}
@@ -796,9 +805,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
796805
// templates
797806
if (tok->isName() && endsWith(tok->str(), '>')) {
798807
// TODO: This is a quick fix to handle when constants are used
799-
// as template parameters. Try to handle this better, perhaps
800-
// only remove constants.
801-
variables.clear();
808+
// as template parameters.
809+
variables.clearConst();
802810
}
803811

804812
else if (Token::Match(tok->previous(), "[;{}]")) {

test/testunusedvar.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6983,6 +6983,15 @@ class TestUnusedVar : public TestFixture {
69836983
" return hash[0];\n"
69846984
"}\n");
69856985
ASSERT_EQUALS("", errout_str());
6986+
6987+
functionVariableUsage("template <int N>\n" // #14199
6988+
"struct S {\n"
6989+
" int i;\n"
6990+
"};\n"
6991+
"void f() {\n"
6992+
" S<0> s;\n"
6993+
"}\n");
6994+
ASSERT_EQUALS("[test.cpp:6:10]: (style) Unused variable: s [unusedVariable]\n", errout_str());
69866995
}
69876996

69886997
void localvarFuncPtr() {

0 commit comments

Comments
 (0)