Merge pull request #106 from udalov/kt2457-fe
Senseless null in when (KT-2457 frontend part)
This commit is contained in:
@@ -286,6 +286,7 @@ public interface Errors {
|
||||
DiagnosticFactory3.create(ERROR);
|
||||
|
||||
DiagnosticFactory2<JetBinaryExpression, JetBinaryExpression, Boolean> SENSELESS_COMPARISON = DiagnosticFactory2.create(WARNING);
|
||||
SimpleDiagnosticFactory<JetElement> SENSELESS_NULL_IN_WHEN = SimpleDiagnosticFactory.create(WARNING);
|
||||
|
||||
DiagnosticFactory2<PsiElement, CallableMemberDescriptor, DeclarationDescriptor> OVERRIDING_FINAL_MEMBER = DiagnosticFactory2.create(ERROR);
|
||||
DiagnosticFactory3<JetModifierListOwner, Visibility, CallableMemberDescriptor, DeclarationDescriptor> CANNOT_WEAKEN_ACCESS_PRIVILEGE =
|
||||
|
||||
+1
@@ -343,6 +343,7 @@ public class DefaultErrorMessages {
|
||||
}, RENDER_TYPE, RENDER_TYPE);
|
||||
|
||||
MAP.put(SENSELESS_COMPARISON, "Condition ''{0}'' is always ''{1}''", ELEMENT_TEXT, TO_STRING);
|
||||
MAP.put(SENSELESS_NULL_IN_WHEN, "Expression under 'when' is never equal to null");
|
||||
|
||||
MAP.put(OVERRIDING_FINAL_MEMBER, "''{0}'' in ''{1}'' is final and cannot be overridden", NAME, NAME);
|
||||
MAP.put(CANNOT_WEAKEN_ACCESS_PRIVILEGE, "Cannot weaken access privilege ''{0}'' for ''{1}'' in ''{2}''", TO_STRING, NAME, NAME);
|
||||
|
||||
+5
@@ -330,6 +330,11 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
|
||||
return;
|
||||
}
|
||||
|
||||
// check if the pattern is essentially a 'null' expression
|
||||
if (type == JetStandardClasses.getNullableNothingType() && !subjectType.isNullable()) {
|
||||
context.trace.report(SENSELESS_NULL_IN_WHEN.on(reportErrorOn));
|
||||
}
|
||||
|
||||
if (BasicExpressionTypingVisitor.isCastErased(subjectType, type, JetTypeChecker.INSTANCE)) {
|
||||
context.trace.report(Errors.CANNOT_CHECK_FOR_ERASED.on(reportErrorOn, type));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
//KT-2457 Verify error when comparing not null value with null in when
|
||||
|
||||
package kt2457
|
||||
|
||||
fun foo(i: Int) : Int =
|
||||
when (i) {
|
||||
1 -> 1
|
||||
<!SENSELESS_NULL_IN_WHEN!>null<!> -> 1
|
||||
else -> 1
|
||||
}
|
||||
@@ -1588,6 +1588,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
|
||||
doTest("compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/ReceiverNullability.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("SenselessNullInWhen.kt")
|
||||
public void testSenselessNullInWhen() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/SenselessNullInWhen.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/objects")
|
||||
|
||||
Reference in New Issue
Block a user