diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a4deb8456..33112f766 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3007,9 +3007,11 @@ void CheckOther::sizeofCalculation() if (Token::simpleMatch(tok, "sizeof (")) { const Token* const end = tok->linkAt(1); for (const Token *tok2 = tok->tokAt(2); tok2 != end; tok2 = tok2->next()) { - if (tok2->isOp() && (!tok2->isExpandedMacro() || _settings->inconclusive) && !Token::Match(tok2, ">|<|&|*")) { - sizeofCalculationError(tok2, tok2->isExpandedMacro()); - break; + if (tok2->isOp() && (!tok2->isExpandedMacro() || _settings->inconclusive) && !Token::Match(tok2, ">|<|&") && (Token::Match(tok2->previous(), "%var%") || !Token::Match(tok2, "*"))) { + if (!(Token::Match(tok2->previous(), "%type%") || Token::Match(tok2->next(), "%type%"))) { + sizeofCalculationError(tok2, tok2->isExpandedMacro()); + break; + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index c8425a3fb..4d03c8f06 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1366,12 +1366,18 @@ private: } void sizeofCalculation() { - check("sizeof(a+b)"); + check("int a, b; int a,sizeof(a+b)"); ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof()\n", errout.str()); - check("sizeof(-a)"); + check("int a, b; sizeof(a*b)"); ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof()\n", errout.str()); + check("int a, b; sizeof(-a)"); + ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof()\n", errout.str()); + + check("int a, b; sizeof(*a)"); + ASSERT_EQUALS("", errout.str()); + check("sizeof(void * const)"); ASSERT_EQUALS("", errout.str()); }