diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index 07b00557761..0f6fb32e420 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -460,6 +460,8 @@ public interface Errors { DiagnosticFactory2 AUTOCAST_IMPOSSIBLE = DiagnosticFactory2.create(ERROR); + SimpleDiagnosticFactory USELESS_NULLABLE_CHECK = SimpleDiagnosticFactory.create(WARNING, NULLABLE_TYPE); + // Properties / locals DiagnosticFactory1 INITIALIZATION_USING_BACKING_FIELD_CUSTOM_SETTER = DiagnosticFactory1.create(ERROR); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java index 5b7adc54bc9..5e5fc57e97b 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java @@ -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); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/PatternMatchingTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/PatternMatchingTypingVisitor.java index 7cd030a6cb4..f93f8cecbf6 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/PatternMatchingTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/PatternMatchingTypingVisitor.java @@ -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)); diff --git a/compiler/testData/diagnostics/tests/cast/kt614.kt b/compiler/testData/diagnostics/tests/cast/kt614.kt new file mode 100644 index 00000000000..49dae1e8f35 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/kt614.kt @@ -0,0 +1 @@ +fun f(a: Collection<*>) = a is List<*>? \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index e0b1c49cb31..5938ffc3699 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -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");