Add warning if constant conforms to infinity or zero

#KT-3805 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-02-07 16:08:42 +03:00
parent 292f010e59
commit 935f7b1cc1
7 changed files with 78 additions and 4 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -790,6 +790,8 @@ public interface Errors {
DiagnosticFactory0<KtConstantExpression> WRONG_LONG_SUFFIX = DiagnosticFactory0.create(ERROR, LONG_LITERAL_SUFFIX);
DiagnosticFactory0<KtConstantExpression> INT_LITERAL_OUT_OF_RANGE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtConstantExpression> FLOAT_LITERAL_OUT_OF_RANGE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtConstantExpression> FLOAT_LITERAL_CONFORMS_INFINITY = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtConstantExpression> FLOAT_LITERAL_CONFORMS_ZERO = DiagnosticFactory0.create(WARNING);
DiagnosticFactory2<KtConstantExpression, String, KotlinType> CONSTANT_EXPECTED_TYPE_MISMATCH = DiagnosticFactory2.create(ERROR);
DiagnosticFactory0<KtConstantExpression> INCORRECT_CHARACTER_LITERAL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtConstantExpression> EMPTY_CHARACTER_LITERAL = DiagnosticFactory0.create(ERROR);
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -534,6 +534,8 @@ public class DefaultErrorMessages {
MAP.put(INT_LITERAL_OUT_OF_RANGE, "The value is out of range");
MAP.put(WRONG_LONG_SUFFIX, "Use 'L' instead of 'l'");
MAP.put(FLOAT_LITERAL_OUT_OF_RANGE, "The value is out of range");
MAP.put(FLOAT_LITERAL_CONFORMS_INFINITY, "Floating point number conforms to infinity");
MAP.put(FLOAT_LITERAL_CONFORMS_ZERO, "Floating point number conforms to zero");
MAP.put(INCORRECT_CHARACTER_LITERAL, "Incorrect character literal");
MAP.put(EMPTY_CHARACTER_LITERAL, "Empty character literal");
MAP.put(ILLEGAL_UNDERSCORE, "Illegal underscore");
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.resolve.constants.evaluate
import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.TypeConversionUtil
import com.intellij.util.text.LiteralFormatUtil
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
@@ -313,8 +314,26 @@ private class ConstantExpressionEvaluatorVisitor(
else -> throw IllegalArgumentException("Unsupported constant: " + expression)
} ?: return null
fun isLongWithSuffix() = nodeElementType == KtNodeTypes.INTEGER_CONSTANT && hasLongSuffix(text)
return createConstant(result, expectedType, CompileTimeConstant.Parameters(true, !isLongWithSuffix(), false, usesNonConstValAsConstant = false))
if (result is Double) {
if (result.isInfinite()) {
trace.report(Errors.FLOAT_LITERAL_CONFORMS_INFINITY.on(expression))
}
if (result == 0.0 && !TypeConversionUtil.isFPZero(text)) {
trace.report(Errors.FLOAT_LITERAL_CONFORMS_ZERO.on(expression))
}
}
if (result is Float) {
if (result.isInfinite()) {
trace.report(Errors.FLOAT_LITERAL_CONFORMS_INFINITY.on(expression))
}
if (result == 0.0f && !TypeConversionUtil.isFPZero(text)) {
trace.report(Errors.FLOAT_LITERAL_CONFORMS_ZERO.on(expression))
}
}
val isLongWithSuffix = nodeElementType == KtNodeTypes.INTEGER_CONSTANT && hasLongSuffix(text)
return createConstant(result, expectedType, CompileTimeConstant.Parameters(true, !isLongWithSuffix, false, usesNonConstValAsConstant = false))
}
override fun visitParenthesizedExpression(expression: KtParenthesizedExpression, expectedType: KotlinType?): CompileTimeConstant<*>? {