diff --git a/lib/astutils.cpp b/lib/astutils.cpp index f67edc54a..405e8fd6b 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2405,8 +2405,11 @@ bool isNullOperand(const Token *expr) bool isGlobalData(const Token *expr, bool cpp) { bool globalData = false; + bool var = false; visitAstNodes(expr, - [&](const Token *tok) { + [expr, cpp, &globalData, &var](const Token *tok) { + if (tok->varId()) + var = true; if (tok->varId() && !tok->variable()) { // Bailout, this is probably global globalData = true; @@ -2467,7 +2470,7 @@ bool isGlobalData(const Token *expr, bool cpp) return ChildrenToVisit::op1; return ChildrenToVisit::op1_and_op2; }); - return globalData; + return globalData || !var; } struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *startToken, const Token *endToken, const std::set &exprVarIds, bool local, bool inInnerClass, int depth) diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 3470292f7..8bf7d5bab 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -220,6 +220,7 @@ private: TEST_CASE(argumentClass); TEST_CASE(escapeAlias); // #9150 TEST_CASE(volatileData); // #9280 + TEST_CASE(globalData); } void checkStructMemberUsage(const char code[], const std::list *directives=nullptr) { @@ -5809,6 +5810,15 @@ private: "}"); ASSERT_EQUALS("", errout.str()); } + + void globalData() { + // #10276 + functionVariableUsage( + "void f(void) {\n" + " ((uint8_t *) (uint16_t)0x1000)[0] = 0x42;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestUnusedVar)