Fixed #8941 (False Positive: Variable 'f' is assigned a value that is never used.)
This commit is contained in:
parent
0f19dfea82
commit
8da4e31c42
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include "token.h"
|
||||
|
||||
#include "astutils.h"
|
||||
#include "errorlogger.h"
|
||||
#include "library.h"
|
||||
#include "settings.h"
|
||||
|
@ -1173,13 +1174,24 @@ static const Token* goToRightParenthesis(const Token* start, const Token* end)
|
|||
std::pair<const Token *, const Token *> Token::findExpressionStartEndTokens() const
|
||||
{
|
||||
const Token * const top = this;
|
||||
|
||||
// find start node in AST tree
|
||||
const Token *start = top;
|
||||
while (start->astOperand1() &&
|
||||
(start->astOperand2() || !start->isUnaryPreOp() || Token::simpleMatch(start, "( )") || start->str() == "{"))
|
||||
start = start->astOperand1();
|
||||
|
||||
// find end node in AST tree
|
||||
const Token *end = top;
|
||||
while (end->astOperand1() && (end->astOperand2() || end->isUnaryPreOp())) {
|
||||
if (Token::Match(end,"(|[") &&
|
||||
// lambda..
|
||||
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();
|
||||
break;
|
||||
|
@ -1187,6 +1199,7 @@ std::pair<const Token *, const Token *> Token::findExpressionStartEndTokens() co
|
|||
end = end->astOperand2() ? end->astOperand2() : end->astOperand1();
|
||||
}
|
||||
|
||||
// skip parentheses
|
||||
start = goToLeftParenthesis(start, end);
|
||||
end = goToRightParenthesis(start, end);
|
||||
if (Token::simpleMatch(end, "{"))
|
||||
|
|
|
@ -181,6 +181,7 @@ private:
|
|||
TEST_CASE(localvarFuncPtr); // #7194
|
||||
TEST_CASE(localvarAddr); // #7477
|
||||
TEST_CASE(localvarDelete);
|
||||
TEST_CASE(localvarReturnCallLambda); // #8941
|
||||
|
||||
TEST_CASE(localvarCppInitialization);
|
||||
TEST_CASE(localvarCpp11Initialization);
|
||||
|
@ -4232,6 +4233,14 @@ private:
|
|||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void localvarReturnCallLambda() {
|
||||
functionVariableUsage("int foo() {\n"
|
||||
" auto f = []{return 1};\n"
|
||||
" return f();\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void localvarCppInitialization() {
|
||||
functionVariableUsage("void foo() {\n"
|
||||
" int buf[6];\n"
|
||||
|
|
Loading…
Reference in New Issue