KT-13521 Warning right part of "expression ?: null" is useless

#KT-13521 Fixed
This commit is contained in:
shiraji
2016-08-22 03:15:12 +09:00
parent a683c2b68d
commit a3f131d6c6
7 changed files with 47 additions and 0 deletions
@@ -686,6 +686,8 @@ public interface Errors {
DiagnosticFactory1<KtBinaryExpression, KotlinType> USELESS_ELVIS = DiagnosticFactory1.create(WARNING, PositioningStrategies.USELESS_ELVIS);
DiagnosticFactory0<PsiElement> USELESS_ELVIS_ON_LAMBDA_EXPRESSION = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtBinaryExpression> USELESS_ELVIS_RIGHT_IS_NULL =
DiagnosticFactory0.create(WARNING, PositioningStrategies.USELESS_ELVIS);
// Compile-time values
@@ -418,6 +418,7 @@ public class DefaultErrorMessages {
MAP.put(DYNAMIC_UPPER_BOUND, "Dynamic type can not be used as an upper bound");
MAP.put(USELESS_ELVIS, "Elvis operator (?:) always returns the left operand of non-nullable type {0}", RENDER_TYPE);
MAP.put(USELESS_ELVIS_ON_LAMBDA_EXPRESSION, "Left operand of elvis operator (?:) is a lambda expression");
MAP.put(USELESS_ELVIS_RIGHT_IS_NULL, "Right operand of elvis operator (?:) is useless if it is null");
MAP.put(CONFLICTING_UPPER_BOUNDS, "Upper bounds of {0} have empty intersection", NAME);
MAP.put(UNSUPPORTED_TYPEALIAS, "Type aliases are unsupported (min Kotlin language level: 1.1)");
@@ -1198,6 +1198,9 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
assert rightTypeInfo != null : "Right expression was not processed: " + expression;
boolean loopBreakContinuePossible = leftTypeInfo.getJumpOutPossible() || rightTypeInfo.getJumpOutPossible();
KotlinType rightType = rightTypeInfo.getType();
if (rightType != null && KtPsiUtil.isNullConstant(right)) {
context.trace.report(USELESS_ELVIS_RIGHT_IS_NULL.on(expression));
}
// Only left argument DFA is taken into account here: we cannot be sure that right argument is joined
// (we merge it with right DFA if right argument contains no jump outside)