Highlighting "as Foo" for useless casts in gray.

#KT-6070 fixed
This commit is contained in:
Evgeny Gerashchenko
2014-11-28 21:33:52 +03:00
parent 20ee59c1e5
commit a3fdce8131
34 changed files with 60 additions and 53 deletions
@@ -530,8 +530,8 @@ public interface Errors {
DiagnosticFactory1<JetElement, JetType> CANNOT_CHECK_FOR_ERASED = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<JetBinaryExpressionWithTypeRHS, JetType, JetType> UNCHECKED_CAST = DiagnosticFactory2.create(WARNING);
DiagnosticFactory0<JetSimpleNameExpression> USELESS_CAST_STATIC_ASSERT_IS_FINE = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<JetSimpleNameExpression> USELESS_CAST = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<JetBinaryExpressionWithTypeRHS> USELESS_CAST_STATIC_ASSERT_IS_FINE = DiagnosticFactory0.create(WARNING, AS_TYPE);
DiagnosticFactory0<JetBinaryExpressionWithTypeRHS> USELESS_CAST = DiagnosticFactory0.create(WARNING, AS_TYPE);
DiagnosticFactory0<JetSimpleNameExpression> CAST_NEVER_SUCCEEDS = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<JetTypeReference> DYNAMIC_NOT_ALLOWED = DiagnosticFactory0.create(ERROR);
@@ -631,7 +631,7 @@ public interface Errors {
INVISIBLE_MEMBER, INVISIBLE_MEMBER_FROM_INLINE, INVISIBLE_REFERENCE, INVISIBLE_SETTER);
ImmutableSet<? extends DiagnosticFactory<?>> UNUSED_ELEMENT_DIAGNOSTICS = ImmutableSet.of(
UNUSED_VARIABLE, UNUSED_PARAMETER, ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE, VARIABLE_WITH_REDUNDANT_INITIALIZER,
UNUSED_FUNCTION_LITERAL);
UNUSED_FUNCTION_LITERAL, USELESS_CAST, USELESS_CAST_STATIC_ASSERT_IS_FINE);
ImmutableSet<? extends DiagnosticFactory<?>> TYPE_INFERENCE_ERRORS = ImmutableSet.of(
TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER, TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS, TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH,
TYPE_INFERENCE_UPPER_BOUND_VIOLATED, TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH);
@@ -365,4 +365,10 @@ public object PositioningStrategies {
return Errors.UNREACHABLE_CODE.cast(diagnostic).getA()
}
}
public val AS_TYPE: PositioningStrategy<JetBinaryExpressionWithTypeRHS> = object : PositioningStrategy<JetBinaryExpressionWithTypeRHS>() {
override fun mark(element: JetBinaryExpressionWithTypeRHS): List<TextRange> {
return markRange(element.getOperationReference(), element)
}
}
}
@@ -223,14 +223,14 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
JetTypeChecker typeChecker = JetTypeChecker.DEFAULT;
if (actualType.equals(targetType)) {
// cast to itself: String as String
context.trace.report(USELESS_CAST.on(expression.getOperationReference()));
context.trace.report(USELESS_CAST.on(expression));
return;
}
Collection<JetType> possibleTypes = DataFlowUtils.getAllPossibleTypes(
expression.getLeft(), context.dataFlowInfo, actualType, context.trace.getBindingContext());
for (JetType possibleType : possibleTypes) {
if (typeChecker.isSubtypeOf(possibleType, targetType)) {
context.trace.report(USELESS_CAST_STATIC_ASSERT_IS_FINE.on(expression.getOperationReference()));
context.trace.report(USELESS_CAST_STATIC_ASSERT_IS_FINE.on(expression));
return;
}
}
+6 -6
View File
@@ -5,12 +5,12 @@ fun test() : Unit {
x : Int?
y : Int
x as Int : Int
y <!USELESS_CAST!>as<!> Int : Int
x <!USELESS_CAST!>as<!> Int? : Int?
y <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> Int? : Int?
y <!USELESS_CAST!>as Int<!> : Int
x <!USELESS_CAST!>as Int?<!> : Int?
y <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as Int?<!> : Int?
x as? Int : Int?
y <!USELESS_CAST!>as?<!> Int : Int?
x <!USELESS_CAST!>as?<!> Int? : Int?
y <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as?<!> Int? : Int?
y <!USELESS_CAST!>as? Int<!> : Int?
x <!USELESS_CAST!>as? Int?<!> : Int?
y <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as? Int?<!> : Int?
Unit
}
@@ -31,7 +31,7 @@ fun test() {
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>: Float
1 <!CAST_NEVER_SUCCEEDS!>as<!> Byte
1 <!USELESS_CAST!>as<!> Int
1 <!USELESS_CAST!>as Int<!>
0xff <!CAST_NEVER_SUCCEEDS!>as<!> Long
1.1 <!CAST_NEVER_SUCCEEDS!>as<!> Int
@@ -45,7 +45,7 @@ fun cannotBe() {
<!VARIABLE_EXPECTED!>""<!> = "";
<!VARIABLE_EXPECTED!>foo()<!> = Unit;
(<!VARIABLE_EXPECTED!>i <!USELESS_CAST!>as<!> Int<!>) = 34
(<!VARIABLE_EXPECTED!>i <!USELESS_CAST!>as Int<!><!>) = 34
(<!VARIABLE_EXPECTED!>i is Int<!>) = false
<!VARIABLE_EXPECTED!>A()<!> = A()
<!VARIABLE_EXPECTED!>5<!> = 34
@@ -1,6 +1,6 @@
class G<T>
fun foo(p: <!UNRESOLVED_REFERENCE!>P<!>) {
val v = <!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>p<!> <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> <!NO_TYPE_ARGUMENTS_ON_RHS!>G?<!>
val v = <!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>p<!> <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as <!NO_TYPE_ARGUMENTS_ON_RHS!>G?<!><!>
v!!: G<*>
}
@@ -1,6 +1,6 @@
class P
fun foo(p: P): Any {
val v = p <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> <!UNRESOLVED_REFERENCE!>G<!>
val v = p <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as <!UNRESOLVED_REFERENCE!>G<!><!>
return <!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>v<!>
}
@@ -1,5 +1,5 @@
fun asCall() {
1 <!USELESS_CAST!>as<!> Int
1 <!USELESS_CAST!>as Int<!>
1 <!CAST_NEVER_SUCCEEDS!>as<!> Byte
1 <!CAST_NEVER_SUCCEEDS!>as<!> Short
1 <!CAST_NEVER_SUCCEEDS!>as<!> Long
@@ -12,7 +12,7 @@ fun asCall() {
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Short
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Long
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Char
1.0 <!USELESS_CAST!>as<!> Double
1.0 <!USELESS_CAST!>as Double<!>
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Float
1f <!CAST_NEVER_SUCCEEDS!>as<!> Int
@@ -21,11 +21,11 @@ fun asCall() {
1f <!CAST_NEVER_SUCCEEDS!>as<!> Long
1f <!CAST_NEVER_SUCCEEDS!>as<!> Char
1f <!CAST_NEVER_SUCCEEDS!>as<!> Double
1f <!USELESS_CAST!>as<!> Float
1f <!USELESS_CAST!>as Float<!>
}
fun asSafe() {
1 <!USELESS_CAST!>as?<!> Int
1 <!USELESS_CAST!>as? Int<!>
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Byte
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Short
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Long
@@ -38,7 +38,7 @@ fun asSafe() {
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Short
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Long
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Char
1.0 <!USELESS_CAST!>as?<!> Double
1.0 <!USELESS_CAST!>as? Double<!>
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Float
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Int
@@ -47,5 +47,5 @@ fun asSafe() {
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Long
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Char
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Double
1f <!USELESS_CAST!>as?<!> Float
1f <!USELESS_CAST!>as? Float<!>
}
@@ -16,6 +16,6 @@ fun bar() {
x.foo()
X.foo()
(X : C).foo()
(X <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> C).foo()
(X <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as C<!>).foo()
((if (1<2) X else Y) : C).foo()
}
@@ -11,7 +11,7 @@ fun f2(s: Number?) {
}
fun f3(s: Number?) {
if (s is Int && s <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> Int == 42);
if (s is Int && s <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as Int<!> == 42);
<!TYPE_MISMATCH!>s<!> : Int
}
@@ -22,6 +22,6 @@ fun f4(a: Any) {
}
fun f5(a: String) {
a <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> Any?
a <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as Any?<!>
a: String
}
@@ -1,7 +1,7 @@
//KT-5455 Need warning about redundant type cast
fun foo(o: Any): Int {
if (o is String) {
return (o <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String).length()
return (o <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String<!>).length()
}
return -1
}
@@ -13,16 +13,16 @@ class B: A()
fun test(a: Any?) {
if (a is B) {
(a <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> A).foo()
(a <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as A<!>).foo()
}
}
fun test1(a: B) {
(a <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> A?)?.foo()
(a <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as A?<!>)?.foo()
}
fun test2(b: B?) {
if (b != null) {
(b <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> A).foo()
(b <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as A<!>).foo()
}
}
@@ -1,5 +1,5 @@
suppress("REDUNDANT_NULLABLE")
class C {
suppress("UNNECESSARY_NOT_NULL_ASSERTION")
fun foo(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??
fun foo(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>
}
@@ -1,4 +1,4 @@
suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION")
class C {
fun foo(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??
fun foo(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>
}
@@ -1,6 +1,6 @@
class C {
suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION")
class object {
val foo: String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??
val foo: String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>
}
}
@@ -1,4 +1,4 @@
fun foo(): Any? {
[suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION")]
return ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??
return ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>
}
@@ -1,4 +1,4 @@
class C {
suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION")
fun foo(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??
fun foo(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>
}
@@ -1,4 +1,4 @@
suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION")
object C {
fun foo(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??
fun foo(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>
}
@@ -1,3 +1,3 @@
class C {
fun foo(suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION") p: String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??) = p
fun foo(suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION") p: String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>) = p
}
@@ -1,4 +1,4 @@
class C {
suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION")
val foo: String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??
val foo: String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>
}
@@ -1,5 +1,5 @@
class C {
val foo: String?
[suppress("REDUNDANT_NULLABLE", "UNNECESSARY_NOT_NULL_ASSERTION")]
get(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as<!> String??
get(): String?? = ""!! <!USELESS_CAST_STATIC_ASSERT_IS_FINE!>as String??<!>
}
@@ -1,4 +1,4 @@
suppress("REDUNDANT_NULLABLE")
class C {
fun foo(): String?? = null <!USELESS_CAST!>as<!> Nothing??
fun foo(): String?? = null <!USELESS_CAST!>as Nothing??<!>
}
@@ -1,6 +1,6 @@
class C {
suppress("REDUNDANT_NULLABLE")
class object {
val foo: String?? = null <!USELESS_CAST!>as<!> Nothing??
val foo: String?? = null <!USELESS_CAST!>as Nothing??<!>
}
}
@@ -1,4 +1,4 @@
fun foo(): Any? {
[suppress("REDUNDANT_NULLABLE")]
return null <!USELESS_CAST!>as<!> Nothing??
return null <!USELESS_CAST!>as Nothing??<!>
}
@@ -1,4 +1,4 @@
class C {
suppress("REDUNDANT_NULLABLE")
fun foo(): String?? = null <!USELESS_CAST!>as<!> Nothing??
fun foo(): String?? = null <!USELESS_CAST!>as Nothing??<!>
}
@@ -1,7 +1,7 @@
class C {
fun foo(): Any? {
[suppress("REDUNDANT_NULLABLE")]
val v: String?? = null <!USELESS_CAST!>as<!> Nothing??
val v: String?? = null <!USELESS_CAST!>as Nothing??<!>
return v
}
}
@@ -1,4 +1,4 @@
suppress("REDUNDANT_NULLABLE")
object C {
fun foo(): String?? = null <!USELESS_CAST!>as<!> Nothing??
fun foo(): String?? = null <!USELESS_CAST!>as Nothing??<!>
}
@@ -1,3 +1,3 @@
class C {
fun foo(suppress("REDUNDANT_NULLABLE") p: String?? = null <!USELESS_CAST!>as<!> Nothing??) = p
fun foo(suppress("REDUNDANT_NULLABLE") p: String?? = null <!USELESS_CAST!>as Nothing??<!>) = p
}
@@ -1,4 +1,4 @@
class C {
suppress("REDUNDANT_NULLABLE")
val foo: String?? = null <!USELESS_CAST!>as<!> Nothing?
val foo: String?? = null <!USELESS_CAST!>as Nothing?<!>
}
@@ -1,5 +1,5 @@
class C {
val foo: String?
[suppress("REDUNDANT_NULLABLE")]
get(): String?? = null <!USELESS_CAST!>as<!> Nothing??
get(): String?? = null <!USELESS_CAST!>as Nothing??<!>
}
@@ -49,6 +49,7 @@ import org.jetbrains.jet.plugin.caches.resolve.*
import org.jetbrains.jet.plugin.quickfix.JetIntentionActionsFactory
import org.jetbrains.jet.plugin.quickfix.QuickFixes
import kotlin.platform.platformStatic
import org.jetbrains.jet.lang.psi.JetBinaryExpressionWithTypeRHS
public open class JetPsiChecker : Annotator, HighlightRangeExtension {
+6 -6
View File
@@ -5,12 +5,12 @@ fun test() : Unit {
x : Int?
y : Int
x as Int : Int
y <warning>as</warning> Int : Int
x <warning>as</warning> Int? : Int?
y <warning>as</warning> Int? : Int?
y <warning textAttributesKey="NOT_USED_ELEMENT_ATTRIBUTES">as Int</warning> : Int
x <warning textAttributesKey="NOT_USED_ELEMENT_ATTRIBUTES">as Int?</warning> : Int?
y <warning textAttributesKey="NOT_USED_ELEMENT_ATTRIBUTES">as Int?</warning> : Int?
x as? Int : Int?
y <warning>as?</warning> Int : Int?
x <warning>as?</warning> Int? : Int?
y <warning>as?</warning> Int? : Int?
y <warning textAttributesKey="NOT_USED_ELEMENT_ATTRIBUTES">as? Int</warning> : Int?
x <warning textAttributesKey="NOT_USED_ELEMENT_ATTRIBUTES">as? Int?</warning> : Int?
y <warning textAttributesKey="NOT_USED_ELEMENT_ATTRIBUTES">as? Int?</warning> : Int?
Unit
}
+1 -1
View File
@@ -3,6 +3,6 @@ fun test() {
1 : Int
<error>1</error> : Double
1 <warning>as</warning> Byte
1 <warning>as</warning> Int
1 <warning>as Int</warning>
1 <warning>as</warning> Double
}