diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 1638ccd0b..13a948051 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1766,6 +1766,24 @@ void CheckOther::functionVariableUsage() tok = tok->next(); } + // standard struct type declaration with possible initialization + // struct S s; struct S s = { 0 }; static struct S s; + else if (Token::Match(tok, "[;{}] static| struct %type% %var% ;|=") && + (isRecordTypeWithoutSideEffects(tok->strAt(1) == "static" ? tok->tokAt(4) : tok->tokAt(3)))) + { + tok = tok->next(); + + bool isStatic = tok->str() == "static"; + if (isStatic) + tok = tok->next(); + + tok = tok->next(); + + variables.addVar(tok->next(), Variables::standard, info, + tok->tokAt(2)->str() == "=" || isStatic); + tok = tok->next(); + } + // standard type declaration and initialization using constructor // int i(0); static int j(0); else if (Token::Match(tok, "[;{}] static| %type% %var% ( %any% ) ;") && @@ -2162,7 +2180,7 @@ void CheckOther::functionVariableUsage() variables.read(tok->varId()); } else if (tok->varId() != varid1 && Token::Match(tok, "%var% .")) - variables.use(tok->varId()); + variables.read(tok->varId()); else if (tok->varId() != varid1 && var2->_type == Variables::standard && tok->strAt(-1) != "&") diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index b0b9de55c..ea390eeb4 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1887,7 +1887,8 @@ private: " struct AB ab;\n" " int * a = &ab.a;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'ab' is not assigned a value\n" + "[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str()); functionVariableUsage("struct AB { int a; int b; };\n" "void foo()\n" @@ -2322,7 +2323,7 @@ private: " struct ABC { int a, b, c; };\n" " struct ABC abc = { 1, 2, 3 };\n" "}\n"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'abc' is assigned a value that is never used\n", errout.str()); } void localvarStruct3()