Fixed #8941 (False Positive: Variable 'f' is assigned a value that is never used.)

This commit is contained in:
Daniel Marjamäki 2019-01-20 13:20:23 +01:00
parent 0f19dfea82
commit 8da4e31c42
2 changed files with 24 additions and 2 deletions

View File

@ -18,6 +18,7 @@
#include "token.h" #include "token.h"
#include "astutils.h"
#include "errorlogger.h" #include "errorlogger.h"
#include "library.h" #include "library.h"
#include "settings.h" #include "settings.h"
@ -1173,20 +1174,32 @@ static const Token* goToRightParenthesis(const Token* start, const Token* end)
std::pair<const Token *, const Token *> Token::findExpressionStartEndTokens() const std::pair<const Token *, const Token *> Token::findExpressionStartEndTokens() const
{ {
const Token * const top = this; const Token * const top = this;
// find start node in AST tree
const Token *start = top; const Token *start = top;
while (start->astOperand1() && while (start->astOperand1() &&
(start->astOperand2() || !start->isUnaryPreOp() || Token::simpleMatch(start, "( )") || start->str() == "{")) (start->astOperand2() || !start->isUnaryPreOp() || Token::simpleMatch(start, "( )") || start->str() == "{"))
start = start->astOperand1(); start = start->astOperand1();
// find end node in AST tree
const Token *end = top; const Token *end = top;
while (end->astOperand1() && (end->astOperand2() || end->isUnaryPreOp())) { while (end->astOperand1() && (end->astOperand2() || end->isUnaryPreOp())) {
if (Token::Match(end,"(|[") && // lambda..
!(Token::Match(end, "( %type%") && !end->astOperand2())) { if (end->str() == "[") {
const Token *lambdaEnd = findLambdaEndToken(end);
if (lambdaEnd) {
end = lambdaEnd;
break;
}
} else if (Token::Match(end,"(|[") &&
!(Token::Match(end, "( %type%") && !end->astOperand2())) {
end = end->link(); end = end->link();
break; break;
} }
end = end->astOperand2() ? end->astOperand2() : end->astOperand1(); end = end->astOperand2() ? end->astOperand2() : end->astOperand1();
} }
// skip parentheses
start = goToLeftParenthesis(start, end); start = goToLeftParenthesis(start, end);
end = goToRightParenthesis(start, end); end = goToRightParenthesis(start, end);
if (Token::simpleMatch(end, "{")) if (Token::simpleMatch(end, "{"))

View File

@ -181,6 +181,7 @@ private:
TEST_CASE(localvarFuncPtr); // #7194 TEST_CASE(localvarFuncPtr); // #7194
TEST_CASE(localvarAddr); // #7477 TEST_CASE(localvarAddr); // #7477
TEST_CASE(localvarDelete); TEST_CASE(localvarDelete);
TEST_CASE(localvarReturnCallLambda); // #8941
TEST_CASE(localvarCppInitialization); TEST_CASE(localvarCppInitialization);
TEST_CASE(localvarCpp11Initialization); TEST_CASE(localvarCpp11Initialization);
@ -4232,6 +4233,14 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void localvarReturnCallLambda() {
functionVariableUsage("int foo() {\n"
" auto f = []{return 1};\n"
" return f();\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void localvarCppInitialization() { void localvarCppInitialization() {
functionVariableUsage("void foo() {\n" functionVariableUsage("void foo() {\n"
" int buf[6];\n" " int buf[6];\n"