Skip to content

Commit ef195b8

Browse files
Fix #13177 FN: constStatement (sizeof(int)) (#7712)
1 parent 4e2b277 commit ef195b8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/checkother.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ static bool isConstStatement(const Token *tok, const Library& library, bool isNe
21642164
return isConstStatement(tok->astOperand1(), library) && isConstStatement(tok->astOperand2(), library);
21652165
if (Token::Match(tok, "!|~|%cop%") && (tok->astOperand1() || tok->astOperand2()))
21662166
return true;
2167-
if (Token::simpleMatch(tok->previous(), "sizeof ("))
2167+
if (Token::Match(tok->previous(), "sizeof|alignof|noexcept|typeid (") && tok->previous()->isKeyword())
21682168
return true;
21692169
if (isCPPCast(tok)) {
21702170
if (Token::simpleMatch(tok->astOperand1(), "dynamic_cast") && Token::simpleMatch(tok->astOperand1()->linkAt(1)->previous(), "& >"))
@@ -2299,6 +2299,7 @@ void CheckOther::checkIncompleteStatement()
22992299
if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") &&
23002300
!Token::Match(tok->previous(), ";|}|{ %any% ;") &&
23012301
!(tok->isCpp() && tok->isCast() && !tok->astParent()) &&
2302+
!(!tok->astParent() && Token::Match(tok->previous(), "%name% (") && tok->previous()->isKeyword()) &&
23022303
!Token::simpleMatch(tok->tokAt(-2), "for (") &&
23032304
!Token::Match(tok->tokAt(-1), "%var% [") &&
23042305
!(tok->str() == "," && tok->astParent() && tok->astParent()->isAssignmentOp()))
@@ -2362,6 +2363,8 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type,
23622363
msg = "Redundant code: Found unused lambda.";
23632364
else if (Token::Match(tok, "%name%|::"))
23642365
msg = "Redundant code: Found unused function.";
2366+
else if (Token::Match(tok->previous(), "%name% ("))
2367+
msg = "Redundant code: Found unused '" + tok->strAt(-1) + "' expression.";
23652368
else if (mSettings->debugwarnings) {
23662369
reportError(tok, Severity::debug, "debug", "constStatementError not handled.");
23672370
return;

test/testincompletestatement.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,19 @@ class TestIncompleteStatement : public TestFixture {
730730
"}\n");
731731
ASSERT_EQUALS("[test.cpp:3:5]: (warning) Redundant code: Found unused lambda. [constStatement]\n",
732732
errout_str());
733+
734+
check("int main() {\n" // #13177
735+
" sizeof(int);\n"
736+
" alignof(long double*);\n"
737+
" noexcept(int());\n"
738+
" typeid(int);\n"
739+
" return(0);\n"
740+
"}\n");
741+
ASSERT_EQUALS("[test.cpp:2:11]: (warning) Redundant code: Found unused 'sizeof' expression. [constStatement]\n"
742+
"[test.cpp:3:12]: (warning) Redundant code: Found unused 'alignof' expression. [constStatement]\n"
743+
"[test.cpp:4:13]: (warning) Redundant code: Found unused 'noexcept' expression. [constStatement]\n"
744+
"[test.cpp:5:11]: (warning) Redundant code: Found unused 'typeid' expression. [constStatement]\n",
745+
errout_str());
733746
}
734747

735748
void vardecl() {

0 commit comments

Comments
 (0)