Merge pull request #156 from crazyproger/KT-614
KT-614 Emit a warning when non-null type is checked for instance of nullable type
This commit is contained in:
@@ -460,6 +460,8 @@ public interface Errors {
|
||||
|
||||
DiagnosticFactory2<JetExpression, JetType, String> AUTOCAST_IMPOSSIBLE = DiagnosticFactory2.create(ERROR);
|
||||
|
||||
SimpleDiagnosticFactory<JetNullableType> USELESS_NULLABLE_CHECK = SimpleDiagnosticFactory.create(WARNING, NULLABLE_TYPE);
|
||||
|
||||
// Properties / locals
|
||||
|
||||
DiagnosticFactory1<JetExpression, DeclarationDescriptor> INITIALIZATION_USING_BACKING_FIELD_CUSTOM_SETTER = DiagnosticFactory1.create(ERROR);
|
||||
|
||||
+1
@@ -223,6 +223,7 @@ public class DefaultErrorMessages {
|
||||
MAP.put(USELESS_CAST_STATIC_ASSERT_IS_FINE, "No cast needed, use ':' instead");
|
||||
MAP.put(USELESS_CAST, "No cast needed");
|
||||
MAP.put(CAST_NEVER_SUCCEEDS, "This cast can never succeed");
|
||||
MAP.put(USELESS_NULLABLE_CHECK, "Non-null type is checked for instance of nullable type");
|
||||
MAP.put(WRONG_SETTER_PARAMETER_TYPE, "Setter parameter type must be equal to the type of the property, i.e. ''{0}''", RENDER_TYPE, RENDER_TYPE);
|
||||
MAP.put(WRONG_GETTER_RETURN_TYPE, "Getter return type must be equal to the type of the property, i.e. ''{0}''", RENDER_TYPE, RENDER_TYPE);
|
||||
MAP.put(NO_CLASS_OBJECT, "Please specify constructor invocation; classifier ''{0}'' does not have a class object", NAME);
|
||||
|
||||
+6
@@ -274,6 +274,12 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
|
||||
return noChange(context);
|
||||
}
|
||||
JetType type = context.expressionTypingServices.getTypeResolver().resolveType(context.scope, typeReferenceAfterIs, context.trace, true);
|
||||
if (!subjectType.isNullable() && type.isNullable()) {
|
||||
JetTypeElement element = typeReferenceAfterIs.getTypeElement();
|
||||
assert element instanceof JetNullableType : "element must be instance of " + JetNullableType.class.getName();
|
||||
JetNullableType nullableType = (JetNullableType) element;
|
||||
context.trace.report(Errors.USELESS_NULLABLE_CHECK.on(nullableType));
|
||||
}
|
||||
checkTypeCompatibility(context, type, subjectType, typeReferenceAfterIs);
|
||||
if (BasicExpressionTypingVisitor.isCastErased(subjectType, type, JetTypeChecker.INSTANCE)) {
|
||||
context.trace.report(Errors.CANNOT_CHECK_FOR_ERASED.on(typeReferenceAfterIs, type));
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
fun f(a: Collection<*>) = a is List<*><!USELESS_NULLABLE_CHECK!>?<!>
|
||||
@@ -783,6 +783,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
|
||||
doTest("compiler/testData/diagnostics/tests/cast/IsTraits.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt614.kt")
|
||||
public void testKt614() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/cast/kt614.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("WhenErasedDisallowFromAny.kt")
|
||||
public void testWhenErasedDisallowFromAny() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/cast/WhenErasedDisallowFromAny.kt");
|
||||
|
||||
Reference in New Issue
Block a user