Skip to content

Commit 9a871d3

Browse files
zingsheimdanmar
authored andcommitted
Improve Fix #6180 ("reset" and "clear" clears the move status)
1 parent f4ab45f commit 9a871d3

4 files changed

Lines changed: 26 additions & 10 deletions

File tree

lib/checkother.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ void CheckOther::checkAccessOfMovedVariable()
27132713
}
27142714
for (const Token* tok = scopeStart->next(); tok != scope->classEnd; tok = tok->next()) {
27152715
const ValueFlow::Value * movedValue = tok->getMovedValue();
2716-
if (!movedValue)
2716+
if (!movedValue || movedValue->moveKind == ValueFlow::Value::NonMovedVariable)
27172717
continue;
27182718
if (movedValue->inconclusive && !reportInconclusive)
27192719
continue;
@@ -2766,6 +2766,8 @@ void CheckOther::accessMovedError(const Token *tok, const std::string &varname,
27662766
errorId = "accessForwarded";
27672767
kindString = "forwarded";
27682768
break;
2769+
default:
2770+
return;
27692771
}
27702772
const std::string errmsg(std::string("Access of ") + kindString + " variable " + varname + ".");
27712773
reportError(tok, Severity::warning, errorId, errmsg, CWE(0U), inconclusive);

lib/valueflow.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,24 @@ static void valueFlowAfterMove(TokenList *tokenlist, SymbolDatabase* symboldatab
17601760

17611761
for (Token* tok = const_cast<Token*>(start); tok != scope->classEnd; tok = tok->next()) {
17621762
Token * varTok;
1763+
if (Token::Match(tok, "%var% . reset|clear (") && tok->next()->originalName() == emptyString) {
1764+
varTok = tok;
1765+
ValueFlow::Value value;
1766+
value.valueType = ValueFlow::Value::MOVED;
1767+
value.moveKind = ValueFlow::Value::NonMovedVariable;
1768+
value.setKnown();
1769+
std::list<ValueFlow::Value> values;
1770+
values.push_back(value);
1771+
1772+
const Variable *var = varTok->variable();
1773+
if (!var || (!var->isLocal() && !var->isArgument()))
1774+
continue;
1775+
const unsigned int varId = varTok->varId();
1776+
const Token * const endOfVarScope = var->typeStartToken()->scope()->classEnd;
1777+
setTokenValue(varTok, value, settings);
1778+
valueFlowForward(varTok->next(), endOfVarScope, var, varId, values, false, tokenlist, errorLogger, settings);
1779+
continue;
1780+
}
17631781
ValueFlow::Value::MoveKind moveKind;
17641782
if (!isStdMoveOrStdForwarded(tok, &moveKind, &varTok))
17651783
continue;

lib/valueflow.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace ValueFlow {
9292
double floatValue;
9393

9494
/** kind of moved */
95-
enum MoveKind {MovedVariable, ForwardedVariable} moveKind;
95+
enum MoveKind {NonMovedVariable, MovedVariable, ForwardedVariable} moveKind;
9696

9797
/** For calculated values - variable value that calculated value depends on */
9898
long long varvalue;
@@ -114,6 +114,8 @@ namespace ValueFlow {
114114

115115
static const char * toString(MoveKind moveKind) {
116116
switch (moveKind) {
117+
case NonMovedVariable:
118+
return "NonMovedVariable";
117119
case MovedVariable:
118120
return "MovedVariable";
119121
case ForwardedVariable:

test/testother.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6228,12 +6228,7 @@ class TestOther : public TestFixture {
62286228
" h(a);\n"
62296229
"}");
62306230
ASSERT_EQUALS("[test.cpp:4]: (warning) Access of moved variable a.\n"
6231-
"[test.cpp:5]: (warning, inconclusive) Access of moved variable a.\n"
6232-
"[test.cpp:6]: (warning, inconclusive) Access of moved variable a.\n"
6233-
"[test.cpp:7]: (warning, inconclusive) Access of moved variable a.\n"
6234-
"[test.cpp:8]: (warning) Access of moved variable a.\n"
6235-
"[test.cpp:9]: (warning, inconclusive) Access of moved variable a.\n"
6236-
"[test.cpp:10]: (warning, inconclusive) Access of moved variable a.\n", errout.str());
6231+
"[test.cpp:8]: (warning) Access of moved variable a.\n", errout.str());
62376232
}
62386233

62396234
void moveAndFunctionParameter() {
@@ -6301,8 +6296,7 @@ class TestOther : public TestFixture {
63016296
" v.clear();\n"
63026297
" if (v.empty()) {}\n"
63036298
"}");
6304-
ASSERT_EQUALS("[test.cpp:4]: (warning, inconclusive) Access of moved variable v.\n"
6305-
"[test.cpp:5]: (warning, inconclusive) Access of moved variable v.\n", errout.str());
6299+
ASSERT_EQUALS("", errout.str());
63066300
}
63076301

63086302
void movedPointer() {

0 commit comments

Comments
 (0)