QmlJS: Restrict warnings of blocks

Warn only if the block contains a var statement
as this is discouraged.

Fixes: QTCREATORBUG-24214
Change-Id: Ib96c6723e82b6ddce0b7b63f23d3408f45ae7d58
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Christian Stenger
2021-05-04 12:04:01 +02:00
parent 845afb25fc
commit 2447d1d69c
2 changed files with 21 additions and 1 deletions

View File

@@ -1367,7 +1367,8 @@ bool Check::visit(Block *ast)
&& !cast<IfStatement *>(p)
&& !cast<SwitchStatement *>(p)
&& !isCaseOrDefault(p)
&& !cast<WithStatement *>(p)) {
&& !cast<WithStatement *>(p)
&& hasVarStatement(ast)) {
addMessage(WarnBlock, ast->lbraceToken);
}
if (!ast->statements
@@ -1666,6 +1667,24 @@ bool Check::isCaseOrDefault(Node *n)
return false;
}
bool Check::hasVarStatement(AST::Block *b) const
{
QTC_ASSERT(b, return false);
StatementList *s = b->statements;
while (s) {
if (auto var = cast<VariableStatement *>(s->statement)) {
VariableDeclarationList *declList = var->declarations;
while (declList) {
if (declList->declaration && declList->declaration->scope == VariableScope::Var)
return true;
declList = declList->next;
}
}
s = s->next;
}
return false;
}
bool Check::visit(NewExpression *ast)
{
checkNewExpression(ast->expression);

View File

@@ -123,6 +123,7 @@ private:
bool isQtQuick2Ui() const;
bool isCaseOrDefault(AST::Node *n);
bool hasVarStatement(AST::Block *b) const;
AST::Node *parent(int distance = 0);