diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index e470bbae1..96698f076 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1147,6 +1147,16 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const variables.use(tok->varId(), tok); // use = read + write } + else if (Token::Match(tok, "[?:]")) { + // TODO: This is to avoid FP for 'p = c ? buf1 : buf2;'. + // Check if the address is taken. If not this is just a read. + // maybe handle struct members better + if (tok->astOperand1()) + variables.use(tok->astOperand1()->varId(), tok->astOperand1()); + if (tok->astOperand2()) + variables.use(tok->astOperand2()->varId(), tok->astOperand2()); + } + else if (tok->isExtendedOp() && tok->next() && tok->next()->varId() && tok->strAt(2) != "=") { variables.readAll(tok->next()->varId(), tok); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 715005863..bc2f2995e 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -2019,7 +2019,8 @@ private: ASSERT_EQUALS("[test.cpp:16]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str()); } - void localvar50() { // #6261 + void localvar50() { // #6261, #6542 + // #6261 - ternary operator in function call functionVariableUsage("void foo() {\n" " char buf1[10];\n" " dostuff(cond?buf1:buf2);\n" @@ -2031,6 +2032,14 @@ private: " dostuff(cond?buf2:buf1);\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #6542 - ternary operator + functionVariableUsage("void foo(int c) {\n" + " char buf1[10], buf2[10];\n" + " char *p = c ? buf1 : buf2;\n" + " dostuff(p);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void localvaralias1() {