From d3d34eccb1e74ec4ea0d17ec55492aae4ae01280 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 1 Aug 2025 15:01:37 +0200 Subject: [PATCH 1/6] Update checkother.cpp --- lib/checkother.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index fc9fc3a291c..73e65bb258f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2164,7 +2164,7 @@ static bool isConstStatement(const Token *tok, const Library& library, bool isNe return isConstStatement(tok->astOperand1(), library) && isConstStatement(tok->astOperand2(), library); if (Token::Match(tok, "!|~|%cop%") && (tok->astOperand1() || tok->astOperand2())) return true; - if (Token::simpleMatch(tok->previous(), "sizeof (")) + if (Token::Match(tok->previous(), "sizeof|alignof|noexcept|typeid (")) return true; if (isCPPCast(tok)) { if (Token::simpleMatch(tok->astOperand1(), "dynamic_cast") && Token::simpleMatch(tok->astOperand1()->linkAt(1)->previous(), "& >")) @@ -2299,6 +2299,7 @@ void CheckOther::checkIncompleteStatement() if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") && !Token::Match(tok->previous(), ";|}|{ %any% ;") && !(tok->isCpp() && tok->isCast() && !tok->astParent()) && + !(!tok->astParent() && Token::Match(tok->previous(), "%name% () && tok->previous()->isKeyword()) && !Token::simpleMatch(tok->tokAt(-2), "for (") && !Token::Match(tok->tokAt(-1), "%var% [") && !(tok->str() == "," && tok->astParent() && tok->astParent()->isAssignmentOp())) @@ -2361,7 +2362,9 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type, else if (tok->str() == "[" && !tok->astParent()) msg = "Redundant code: Found unused lambda."; else if (Token::Match(tok, "%name%|::")) - msg = "Redundant code: Found unused function."; + msg = "Redundant code: Found unused function."; + else if (Token::Match(tok->previous(), "%name% (")) + msg = "Redundant code: Found unused '" + tok->strAt(-1) + "' expression."; else if (mSettings->debugwarnings) { reportError(tok, Severity::debug, "debug", "constStatementError not handled."); return; From b4eae9ced477c9b3a14f130fa14a05613453d0ce Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 1 Aug 2025 15:04:56 +0200 Subject: [PATCH 2/6] Update testincompletestatement.cpp --- test/testincompletestatement.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 637aa3d121e..afdacb77c51 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -730,6 +730,19 @@ class TestIncompleteStatement : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:3:5]: (warning) Redundant code: Found unused lambda. [constStatement]\n", errout_str()); + + check("int main() {\n" // #13177 + " sizeof(int);\n" + " alignof(long double*);\n" + " noexcept(int());\n" + " typeid(int);\n" + " return(0);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2:11]: (warning) Redundant code: Found unused 'sizeof' expression. [constStatement]\n" + "[test.cpp:3:12]: (warning) Redundant code: Found unused 'alignof' expression. [constStatement]\n" + "[test.cpp:4:13]: (warning) Redundant code: Found unused 'noexcept' expression. [constStatement]\n" + "[test.cpp:5:11]: (warning) Redundant code: Found unused 'typeid' expression. [constStatement]\n", + errout_str()); } void vardecl() { From 786564880f2f6646d943eea77fcaec51554d2b48 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 1 Aug 2025 15:07:02 +0200 Subject: [PATCH 3/6] 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 73e65bb258f..af799c9da5d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2299,7 +2299,7 @@ void CheckOther::checkIncompleteStatement() if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") && !Token::Match(tok->previous(), ";|}|{ %any% ;") && !(tok->isCpp() && tok->isCast() && !tok->astParent()) && - !(!tok->astParent() && Token::Match(tok->previous(), "%name% () && tok->previous()->isKeyword()) && + !(!tok->astParent() && Token::Match(tok->previous(), "%name% ()") && tok->previous()->isKeyword()) && !Token::simpleMatch(tok->tokAt(-2), "for (") && !Token::Match(tok->tokAt(-1), "%var% [") && !(tok->str() == "," && tok->astParent() && tok->astParent()->isAssignmentOp())) From dbce87693750e189aeab131b5323ff865c2b4a56 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 1 Aug 2025 15:51:36 +0200 Subject: [PATCH 4/6] 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 af799c9da5d..6262627e406 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2299,7 +2299,7 @@ void CheckOther::checkIncompleteStatement() if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") && !Token::Match(tok->previous(), ";|}|{ %any% ;") && !(tok->isCpp() && tok->isCast() && !tok->astParent()) && - !(!tok->astParent() && Token::Match(tok->previous(), "%name% ()") && tok->previous()->isKeyword()) && + !(!tok->astParent() && Token::Match(tok->previous(), "%name% (") && tok->previous()->isKeyword()) && !Token::simpleMatch(tok->tokAt(-2), "for (") && !Token::Match(tok->tokAt(-1), "%var% [") && !(tok->str() == "," && tok->astParent() && tok->astParent()->isAssignmentOp())) From 330d3e28ec96cb6d11ca894fc3e23d2e0f417f8a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 1 Aug 2025 16:01:42 +0200 Subject: [PATCH 5/6] 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 6262627e406..9cdc1531803 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2362,7 +2362,7 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type, else if (tok->str() == "[" && !tok->astParent()) msg = "Redundant code: Found unused lambda."; else if (Token::Match(tok, "%name%|::")) - msg = "Redundant code: Found unused function."; + msg = "Redundant code: Found unused function."; else if (Token::Match(tok->previous(), "%name% (")) msg = "Redundant code: Found unused '" + tok->strAt(-1) + "' expression."; else if (mSettings->debugwarnings) { From cbb40d43cd165e8b500189d6d5f73aa483322bbc Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 4 Aug 2025 09:12:21 +0200 Subject: [PATCH 6/6] 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 9cdc1531803..de39f5bdcda 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2164,7 +2164,7 @@ static bool isConstStatement(const Token *tok, const Library& library, bool isNe return isConstStatement(tok->astOperand1(), library) && isConstStatement(tok->astOperand2(), library); if (Token::Match(tok, "!|~|%cop%") && (tok->astOperand1() || tok->astOperand2())) return true; - if (Token::Match(tok->previous(), "sizeof|alignof|noexcept|typeid (")) + if (Token::Match(tok->previous(), "sizeof|alignof|noexcept|typeid (") && tok->previous()->isKeyword()) return true; if (isCPPCast(tok)) { if (Token::simpleMatch(tok->astOperand1(), "dynamic_cast") && Token::simpleMatch(tok->astOperand1()->linkAt(1)->previous(), "& >"))