Skip to content

Commit 804c4a4

Browse files
Fix #13303 FN unreadVariable (iterator) (#8023)
1 parent 1d897ae commit 804c4a4

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

lib/astutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,15 +3678,15 @@ bool isGlobalData(const Token *expr)
36783678
// TODO check if pointer points at local data
36793679
const Variable *lhsvar = tok->astOperand1()->variable();
36803680
const ValueType *lhstype = tok->astOperand1()->valueType();
3681-
if (lhsvar->isPointer()) {
3681+
if (lhsvar->isPointer() || !lhstype || lhstype->type == ValueType::Type::ITERATOR) {
36823682
globalData = true;
36833683
return ChildrenToVisit::none;
36843684
}
36853685
if (lhsvar->isArgument() && lhsvar->isArray()) {
36863686
globalData = true;
36873687
return ChildrenToVisit::none;
36883688
}
3689-
if (lhsvar->isArgument() && (!lhstype || (lhstype->type <= ValueType::Type::VOID && !lhstype->container))) {
3689+
if (lhsvar->isArgument() && lhstype->type <= ValueType::Type::VOID && !lhstype->container) {
36903690
globalData = true;
36913691
return ChildrenToVisit::none;
36923692
}

lib/checkunusedvar.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,12 +1283,6 @@ void CheckUnusedVar::checkFunctionVariableUsage()
12831283
if (!tok->astOperand1())
12841284
continue;
12851285

1286-
const Token *iteratorToken = tok->astOperand1();
1287-
while (Token::Match(iteratorToken, "[.*]"))
1288-
iteratorToken = iteratorToken->astOperand1();
1289-
if (iteratorToken && iteratorToken->variable() && iteratorToken->variable()->typeEndToken()->str().find("iterator") != std::string::npos)
1290-
continue;
1291-
12921286
const Token *op1tok = tok->astOperand1();
12931287
while (Token::Match(op1tok, ".|[|*"))
12941288
op1tok = op1tok->astOperand1();

test/testunusedvar.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5228,6 +5228,13 @@ class TestUnusedVar : public TestFixture {
52285228
" }\n"
52295229
"}");
52305230
ASSERT_EQUALS("", errout_str());
5231+
5232+
functionVariableUsage("void f(const std::vector<int>& v) {\n" // #13303
5233+
" std::vector<int>::const_iterator it = v.cbegin();\n"
5234+
" if (*it == 0)\n"
5235+
" it = v.cend();\n"
5236+
"}\n");
5237+
ASSERT_EQUALS("[test.cpp:4:12]: (style) Variable 'it' is assigned a value that is never used. [unreadVariable]\n", errout_str());
52315238
}
52325239

52335240
void localvaralias19() { // #9828

0 commit comments

Comments
 (0)