Diagnostics for impossible/useless casts
This commit is contained in:
@@ -17,7 +17,7 @@ import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
public class LabelsAnnotator implements Annotator {
|
||||
public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) {
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) return;
|
||||
// if (ApplicationManager.getApplication().isUnitTestMode()) return;
|
||||
element.accept(new JetVisitor() {
|
||||
@Override
|
||||
public void visitPrefixExpression(JetPrefixExpression expression) {
|
||||
|
||||
@@ -590,12 +590,11 @@ public class JetTypeInferrer {
|
||||
result = targetType;
|
||||
}
|
||||
else if (operationType == JetTokens.AS_KEYWORD) {
|
||||
// TODO : Check for cast impossibility
|
||||
checkForCastImpossibility(expression, actualType, targetType);
|
||||
result = targetType;
|
||||
}
|
||||
else if (operationType == JetTokens.AS_SAFE) {
|
||||
// TODO : Check for cast impossibility
|
||||
|
||||
checkForCastImpossibility(expression, actualType, targetType);
|
||||
result = TypeUtils.makeNullable(targetType);
|
||||
}
|
||||
else {
|
||||
@@ -604,6 +603,25 @@ public class JetTypeInferrer {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkForCastImpossibility(JetBinaryExpressionWithTypeRHS expression, JetType actualType, JetType targetType) {
|
||||
if (actualType == null) return;
|
||||
|
||||
JetTypeChecker typeChecker = semanticServices.getTypeChecker();
|
||||
if (!typeChecker.isSubtypeOf(targetType, actualType)) {
|
||||
if (typeChecker.isSubtypeOf(actualType, targetType)) {
|
||||
semanticServices.getErrorHandler().genericWarning(expression.getOperationSign().getNode(), "No cast needed, use ':' instead");
|
||||
}
|
||||
else {
|
||||
semanticServices.getErrorHandler().genericError(expression.getOperationSign().getNode(), "This cast can never succeed");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (typeChecker.isSubtypeOf(actualType, targetType)) {
|
||||
semanticServices.getErrorHandler().genericWarning(expression.getOperationSign().getNode(), "No cast needed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitTupleExpression(JetTupleExpression expression) {
|
||||
List<JetExpression> entries = expression.getEntries();
|
||||
|
||||
@@ -5,12 +5,12 @@ fun test() : Unit {
|
||||
x : Int?
|
||||
y : Int
|
||||
x as Int : Int
|
||||
y as Int : Int
|
||||
x as Int? : Int?
|
||||
y as Int? : Int?
|
||||
y <warning>as</warning> Int : Int
|
||||
x <warning>as</warning> Int? : Int?
|
||||
y <warning>as</warning> Int? : Int?
|
||||
x as? Int : Int?
|
||||
y as? Int : Int?
|
||||
x as? Int? : Int?
|
||||
y as? Int? : Int?
|
||||
y <warning>as?</warning> Int : Int?
|
||||
x <warning>as?</warning> Int? : Int?
|
||||
y <warning>as?</warning> Int? : Int?
|
||||
()
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public abstract class JetTestCaseBase extends LightDaemonAnalyzerTestCase {
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
doTest(getTestFilePath(), false, false);
|
||||
doTest(getTestFilePath(), true, false);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
Reference in New Issue
Block a user