Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,14 +911,20 @@ static bool isSimpleExpr(const Token* tok, const Variable* var, const Settings*
return true;
bool needsCheck = tok->varId() > 0;
if (!needsCheck) {
if (tok->isArithmeticalOp())
return isSimpleExpr(tok->astOperand1(), var, settings) && (!tok->astOperand2() || isSimpleExpr(tok->astOperand2(), var, settings));
const Token* ftok = tok->previous();
if (Token::Match(ftok, "%name% (") &&
((ftok->function() && ftok->function()->isConst()) || settings->library.isFunctionConst(ftok->str(), /*pure*/ true)))
needsCheck = true;
if (tok->isArithmeticalOp() &&
(!tok->astOperand1() || isSimpleExpr(tok->astOperand1(), var, settings)) &&
(!tok->astOperand2() || isSimpleExpr(tok->astOperand2(), var, settings)))
return true;
else if (tok->str() == "[") {
needsCheck = tok->astOperand1() && tok->astOperand1()->varId() > 0;
tok = tok->astOperand1();
}
else if (isLeafDot(tok->astOperand2())) {
needsCheck = tok->astOperand2()->varId() > 0;
tok = tok->astOperand2();
}
}
return (needsCheck && !findExpressionChanged(tok, tok->astParent(), var->scope()->bodyEnd, settings));
}
Expand Down
3 changes: 1 addition & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8438,9 +8438,8 @@ bool ValueFlow::isContainerSizeChanged(const Token* tok, int indirect, const Set
return true;
if (astIsLHS(tok) && Token::Match(tok->astParent(), "%assign%|<<"))
return true;
const Library::Container* container = tok->valueType()->container;
if (astIsLHS(tok) && Token::simpleMatch(tok->astParent(), "["))
return container->stdAssociativeLike;
return tok->valueType()->container->stdAssociativeLike;
const Library::Container::Action action = astContainerAction(tok);
switch (action) {
case Library::Container::Action::RESIZE:
Expand Down
15 changes: 14 additions & 1 deletion test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ class TestOther : public TestFixture {
}

void varScope40() {
checkP("#define NUM (-999.9)\n"
checkP("#define NUM (-999.9)\n" // #8862
"double f(int i) {\n"
" double a = NUM;\n"
" double b = -NUM;\n"
Expand All @@ -1732,6 +1732,19 @@ class TestOther : public TestFixture {
"[test.cpp:4]: (style) The scope of the variable 'b' can be reduced.\n"
"[test.cpp:5]: (style) The scope of the variable 'c' can be reduced.\n",
errout_str());

check("struct S { int a; };\n" // #12618
"int f(const S* s, int i) {\n"
" int x = s->a;\n"
" const int b[] = { 1, 2, 3 };\n"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even the scope of 'b' could be reduced.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah.. but that requires that scope of y can be reduced. I don't know if we will warn about b if y is moved.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't warn for arrays because of

if (Token::Match(tok, "= %varid%", var->declarationId()) && (var->isArray() || var->isPointer() || (var->valueType() && var->valueType()->container))) // Create a copy of array/pointer. Bailout, because the memory it points to might be necessary in outer scope
            return false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok it's a bailout we should fix.. good that the comment says "bailout" explicitly so we can search for it..

" int y = b[1];\n"
" if (i)\n"
" return x + y;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) The scope of the variable 'x' can be reduced.\n"
"[test.cpp:5]: (style) The scope of the variable 'y' can be reduced.\n",
errout_str());
}

#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
Expand Down