diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d045fb142c8..4aaa887d7f0 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1163,6 +1163,8 @@ static bool isSimpleExpr(const Token* tok, const Variable* var, const Settings& return false; if (tok->isNumber() || tok->tokType() == Token::eString || tok->tokType() == Token::eChar || tok->isBoolean()) return true; + if (isNullOperand(tok)) + return true; bool needsCheck = tok->varId() > 0; if (!needsCheck) { if (tok->isArithmeticalOp()) diff --git a/test/testother.cpp b/test/testother.cpp index fadb61426a4..0ccd482c353 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -121,6 +121,7 @@ class TestOther : public TestFixture { TEST_CASE(varScope41); // #11845 TEST_CASE(varScope42); TEST_CASE(varScope43); + TEST_CASE(varScope44); TEST_CASE(oldStylePointerCast); TEST_CASE(intToPointerCast); @@ -1945,6 +1946,32 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:3:12]: (style) The scope of the variable 'x' can be reduced. [variableScope]\n", errout_str()); } + void varScope44() { // #14496 + check("char* f() {\n" + " char* p = nullptr;\n" + " {\n" + " p = strdup(\"abc\");\n" + " if (p) {\n" + " return p;\n" + " }\n" + " }\n" + " return nullptr;\n" + "}\n" + "char* g() {\n" + " char* q = NULL;\n" + " {\n" + " q = strdup(\"abc\");\n" + " if (q) {\n" + " return q;\n" + " }\n" + " }\n" + " return nullptr;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2:11]: (style) The scope of the variable 'p' can be reduced. [variableScope]\n" + "[test.cpp:12:11]: (style) The scope of the variable 'q' can be reduced. [variableScope]\n", + errout_str()); + } + #define checkOldStylePointerCast(...) checkOldStylePointerCast_(__FILE__, __LINE__, __VA_ARGS__) template void checkOldStylePointerCast_(const char* file, int line, const char (&code)[size], Standards::cppstd_t std = Standards::CPPLatest) {