From bbe8667abec35b9a4b789ea6d27483a8f93283a5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 15 Apr 2024 14:31:32 +0200 Subject: [PATCH 1/5] Update checkother.cpp --- lib/checkother.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index e4fcfa73c16..c8f26b37e78 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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)); } From 6730be6da3c0faf2779251f07a9a9d56919ffe15 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 15 Apr 2024 14:33:40 +0200 Subject: [PATCH 2/5] Update testother.cpp --- test/testother.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 4d4286ce1d5..c677c16fbf0 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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); @@ -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) { From f244e7932ce620b1e775e62a5009f81939ad0c92 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 15 Apr 2024 14:35:13 +0200 Subject: [PATCH 3/5] Update testother.cpp --- test/testother.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index c677c16fbf0..fbc5a9da452 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1729,9 +1729,9 @@ class TestOther : public TestFixture { " 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()); + "[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__) From 676b05c97b5dc7c16ed6f0c585685e1cbc2241b8 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:08:26 +0200 Subject: [PATCH 4/5] Update checkother.cpp --- lib/checkother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c8f26b37e78..b4a665113b5 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -915,7 +915,7 @@ 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() && + if (tok->isArithmeticalOp() && (!tok->astOperand1() || isSimpleExpr(tok->astOperand1(), var, settings)) && (!tok->astOperand2() || isSimpleExpr(tok->astOperand2(), var, settings))) return true; From c6026d503a07b287c2035e68f330cfa646d9b23e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:10:05 +0200 Subject: [PATCH 5/5] Update testother.cpp --- test/testother.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index fbc5a9da452..85ee90f7ff6 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1728,10 +1728,10 @@ class TestOther : public TestFixture { " }\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()); + 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__)