Commit 85d78c60 authored by Christian Kamm's avatar Christian Kamm
Browse files

QmlJS checks: Add hint about not using var/variant property types.



Change-Id: I79c5c4db78eb96eda7c6f5d543bb5063d5670968
Reviewed-by: default avatarRoberto Raggi <roberto.raggi@nokia.com>
parent 2ab22fec
......@@ -748,21 +748,38 @@ bool Check::visit(UiArrayBinding *ast)
bool Check::visit(UiPublicMember *ast)
{
// check if the member type is valid
if (!ast->memberType.isEmpty()) {
const QString &name = ast->memberType.toString();
if (!name.isEmpty() && name.at(0).isLower()) {
if (!isValidBuiltinPropertyType(name))
addMessage(ErrInvalidPropertyType, ast->typeToken, name);
if (ast->type == UiPublicMember::Property) {
// check if the member type is valid
if (!ast->memberType.isEmpty()) {
const QString &name = ast->memberType.toString();
if (!name.isEmpty() && name.at(0).isLower()) {
if (!isValidBuiltinPropertyType(name))
addMessage(ErrInvalidPropertyType, ast->typeToken, name);
}
// warn about dubious use of var/variant
if (name == QLatin1String("variant") || name == QLatin1String("var")) {
Evaluate evaluator(&_scopeChain);
const Value *init = evaluator(ast->statement);
QString preferedType;
if (init->asNumberValue())
preferedType = tr("'int' or 'real'");
if (init->asStringValue())
preferedType = QLatin1String("'string'");
if (init->asBooleanValue())
preferedType = QLatin1String("'bool'");
if (!preferedType.isEmpty())
addMessage(HintPreferNonVarPropertyType, ast->typeToken, preferedType);
}
}
}
checkBindingRhs(ast->statement);
checkBindingRhs(ast->statement);
_scopeBuilder.push(ast);
Node::accept(ast->statement, this);
Node::accept(ast->binding, this);
_scopeBuilder.pop();
_scopeBuilder.push(ast);
Node::accept(ast->statement, this);
Node::accept(ast->binding, this);
_scopeBuilder.pop();
}
return false;
}
......
......@@ -182,6 +182,8 @@ StaticAnalysisMessages::StaticAnalysisMessages()
tr("use spaces around binary operators"));
newMsg(WarnUnintentinalEmptyBlock, Warning,
tr("unintentional empty block, use ({}) for empty object literal"));
newMsg(HintPreferNonVarPropertyType, Hint,
tr("use %1 instead of 'var' or 'variant' to improve performance"), 1);
}
} // anonymous namespace
......
......@@ -105,7 +105,8 @@ enum Type
WarnNewWithLowercaseFunction = 307,
WarnNumberConstructor = 308,
HintBinaryOperatorSpacing = 309,
WarnUnintentinalEmptyBlock = 310
WarnUnintentinalEmptyBlock = 310,
HintPreferNonVarPropertyType = 311
};
class QMLJS_EXPORT Message
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment