From 320381f51938e66a59a158f769716eea25fe7975 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:25:48 +0200 Subject: [PATCH 1/5] Update checkother.cpp --- lib/checkother.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 171437a20db..1beb0fbca2c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -911,14 +911,16 @@ 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; + else if (isLeafDot(tok->astOperand2())) + needsCheck = tok->astOperand2()->varId() > 0; } return (needsCheck && !findExpressionChanged(tok, tok->astParent(), var->scope()->bodyEnd, settings)); } From 26722c2b3c01440a66c7488316b7dbcc237f95b5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:27:33 +0200 Subject: [PATCH 2/5] Update testother.cpp --- test/testother.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/testother.cpp b/test/testother.cpp index 85ee90f7ff6..b9bc5abea20 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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" @@ -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" + " 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__) From a7b0eb8257d3a29dc1042d3d9886240b201370a2 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:03:48 +0200 Subject: [PATCH 3/5] Update valueflow.cpp --- lib/valueflow.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6e7e79c4c1b..6ef0c409211 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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: From 785b11d70cc18ab2e5d5478b960a7dcc61ef2d8f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:01:35 +0200 Subject: [PATCH 4/5] Update checkother.cpp --- lib/checkother.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 1beb0fbca2c..d0dcac355eb 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -917,10 +917,14 @@ 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; - else if (tok->str() == "[") + else if (tok->str() == "[") { needsCheck = tok->astOperand1() && tok->astOperand1()->varId() > 0; - else if (isLeafDot(tok->astOperand2())) + 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)); } From a2af205528517a40a23d6436ba17a94ab93ae471 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:23:11 +0200 Subject: [PATCH 5/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 d0dcac355eb..6d2f57ee23f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -923,7 +923,7 @@ static bool isSimpleExpr(const Token* tok, const Variable* var, const Settings* } else if (isLeafDot(tok->astOperand2())) { needsCheck = tok->astOperand2()->varId() > 0; - tok = tok->astoperand2(); + tok = tok->astOperand2(); } } return (needsCheck && !findExpressionChanged(tok, tok->astParent(), var->scope()->bodyEnd, settings));