forked from qt-creator/qt-creator
QmlJS checks: Add hint about not using var/variant property types.
Change-Id: I79c5c4db78eb96eda7c6f5d543bb5063d5670968 Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
This commit is contained in:
@@ -748,22 +748,39 @@ 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);
|
||||
|
||||
_scopeBuilder.push(ast);
|
||||
Node::accept(ast->statement, this);
|
||||
Node::accept(ast->binding, this);
|
||||
_scopeBuilder.pop();
|
||||
}
|
||||
|
||||
checkBindingRhs(ast->statement);
|
||||
|
||||
_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
|
||||
|
||||
Reference in New Issue
Block a user