diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 1d0b30b70..4e70dbbc1 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1647,11 +1647,21 @@ void CheckOther::functionVariableUsage() else if (Token::Match(tok, "; %var% ;")) variables.readAll(tok->next()->varId()); - else if (Token::Match(tok, "++|-- %var%")) - variables.modified(tok->next()->varId()); + if (Token::Match(tok, "++|-- %var%")) + { + if (tok->strAt(-1) != ";") + variables.use(tok->next()->varId()); + else + variables.modified(tok->next()->varId()); + } else if (Token::Match(tok, "%var% ++|--")) - variables.modified(tok->varId()); + { + if (tok->strAt(-1) != ";") + variables.use(tok->varId()); + else + variables.modified(tok->varId()); + } } // Check usage of all variables in the current scope.. diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index fa85cba44..6b5282832 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -73,6 +73,7 @@ private: TEST_CASE(localvar24); // ticket #1803 TEST_CASE(localvar25); // ticket #1729 TEST_CASE(localvar26); // ticket #1894 + TEST_CASE(localvar27); // ticket #2160 TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias3); // ticket #1639 @@ -1271,6 +1272,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvar27() // ticket #2160 + { + functionVariableUsage("void f(struct s *ptr) {\n" + " int param = 1;\n" + " ptr->param = param++;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvaralias1() { functionVariableUsage("void foo()\n"