From 5654630099cf0eac6b12270787938f5d69f15e21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 16 Nov 2019 19:49:43 +0100 Subject: [PATCH] Fixed #8978 (False positive: Variable assigned value that is never used when assigning via iterator) --- lib/checkunusedvar.cpp | 25 +++++++++++++++++-------- test/testunusedvar.cpp | 9 +++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index ef5cac7b6..eff8a38be 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1185,16 +1185,25 @@ void CheckUnusedVar::checkFunctionVariableUsage() op1tok = op1tok->astOperand1(); const Variable *op1Var = op1tok ? op1tok->variable() : nullptr; - if (op1Var && op1Var->isReference() && op1Var->nameToken() != tok->astOperand1()) - // todo: check references - continue; + if (op1Var) { + if (op1Var->isReference() && op1Var->nameToken() != tok->astOperand1()) + // todo: check references + continue; - if (op1Var && op1Var->isStatic()) - // todo: check static variables - continue; + if (op1Var->isStatic()) + // todo: check static variables + continue; - if (op1Var && op1Var->nameToken()->isAttributeUnused()) - continue; + if (op1Var->nameToken()->isAttributeUnused()) + continue; + + // Bailout for unknown template classes, we have no idea what side effects such assignments have + if (mTokenizer->isCPP() && + op1Var->isClass() && + (!op1Var->valueType() || op1Var->valueType()->type == ValueType::Type::UNKNOWN_TYPE) && + Token::findsimplematch(op1Var->typeStartToken(), "<", op1Var->typeEndToken())) + continue; + } // Is there a redundant assignment? const Token *start = tok->findExpressionStartEndTokens().second->next(); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 1a0d0cf0f..2dc908f86 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -201,6 +201,7 @@ private: TEST_CASE(bracesInitCpp11);// #7895 - "int var{123}" initialization TEST_CASE(argument); + TEST_CASE(argumentClass); TEST_CASE(escapeAlias); // #9150 } @@ -4578,6 +4579,14 @@ private: ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'foo.x' is assigned a value that is never used.\n", errout.str()); } + void argumentClass() { + functionVariableUsage( + "void foo(std::insert_iterator it) {\n" + " it = 123;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void escapeAlias() { functionVariableUsage( "struct A {\n"