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
4 changes: 4 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,10 @@ static bool isSimpleExpr(const Token* tok, const Variable* var, const Settings*
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;
}
return (needsCheck && !findExpressionChanged(tok, tok->astParent(), var->scope()->bodyEnd, settings));
}
Expand Down
23 changes: 23 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class TestOther : public TestFixture {
TEST_CASE(varScope37); // #12158
TEST_CASE(varScope38);
TEST_CASE(varScope39);
TEST_CASE(varScope40);

TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
Expand Down Expand Up @@ -1710,6 +1711,28 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void varScope40() {
checkP("#define NUM (-999.9)\n"
"double f(int i) {\n"
" double a = NUM;\n"
" double b = -NUM;\n"
" double c = -1.0 * NUM;\n"
" if (i == 1) {\n"
" return a;\n"
" }\n"
" if (i == 2) {\n"
" return b;\n"
" }\n"
" if (i == 3) {\n"
" return c;\n"
" }\n"
" return 0.0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) The scope of the variable 'a' can be reduced.\n"
"[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());
}

#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
Expand Down