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 class LabelsAnnotator implements Annotator {
|
||||||
public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) {
|
public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) {
|
||||||
if (ApplicationManager.getApplication().isUnitTestMode()) return;
|
// if (ApplicationManager.getApplication().isUnitTestMode()) return;
|
||||||
element.accept(new JetVisitor() {
|
element.accept(new JetVisitor() {
|
||||||
@Override
|
@Override
|
||||||
public void visitPrefixExpression(JetPrefixExpression expression) {
|
public void visitPrefixExpression(JetPrefixExpression expression) {
|
||||||
|
|||||||
@@ -590,12 +590,11 @@ public class JetTypeInferrer {
|
|||||||
result = targetType;
|
result = targetType;
|
||||||
}
|
}
|
||||||
else if (operationType == JetTokens.AS_KEYWORD) {
|
else if (operationType == JetTokens.AS_KEYWORD) {
|
||||||
// TODO : Check for cast impossibility
|
checkForCastImpossibility(expression, actualType, targetType);
|
||||||
result = targetType;
|
result = targetType;
|
||||||
}
|
}
|
||||||
else if (operationType == JetTokens.AS_SAFE) {
|
else if (operationType == JetTokens.AS_SAFE) {
|
||||||
// TODO : Check for cast impossibility
|
checkForCastImpossibility(expression, actualType, targetType);
|
||||||
|
|
||||||
result = TypeUtils.makeNullable(targetType);
|
result = TypeUtils.makeNullable(targetType);
|
||||||
}
|
}
|
||||||
else {
|
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
|
@Override
|
||||||
public void visitTupleExpression(JetTupleExpression expression) {
|
public void visitTupleExpression(JetTupleExpression expression) {
|
||||||
List<JetExpression> entries = expression.getEntries();
|
List<JetExpression> entries = expression.getEntries();
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ fun test() : Unit {
|
|||||||
x : Int?
|
x : Int?
|
||||||
y : Int
|
y : Int
|
||||||
x as Int : Int
|
x as Int : Int
|
||||||
y as Int : Int
|
y <warning>as</warning> Int : Int
|
||||||
x as Int? : Int?
|
x <warning>as</warning> Int? : Int?
|
||||||
y as Int? : Int?
|
y <warning>as</warning> Int? : Int?
|
||||||
x as? Int : Int?
|
x as? Int : Int?
|
||||||
y as? Int : Int?
|
y <warning>as?</warning> Int : Int?
|
||||||
x as? Int? : Int?
|
x <warning>as?</warning> Int? : Int?
|
||||||
y as? Int? : Int?
|
y <warning>as?</warning> Int? : Int?
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ public abstract class JetTestCaseBase extends LightDaemonAnalyzerTestCase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void runTest() throws Throwable {
|
protected void runTest() throws Throwable {
|
||||||
doTest(getTestFilePath(), false, false);
|
doTest(getTestFilePath(), true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
Reference in New Issue
Block a user