Changed highlighting range and attributes of useless elvis operator

This commit is contained in:
Valentin Kipyatkov
2015-06-01 22:22:17 +03:00
parent 11290ae9e7
commit aefe0dd192
14 changed files with 26 additions and 21 deletions
@@ -268,7 +268,7 @@ public class JavaNullabilityWarningsChecker : AdditionalTypeChecker {
DataFlowValueFactory.createDataFlowValue(baseExpression, baseExpressionType, c), DataFlowValueFactory.createDataFlowValue(baseExpression, baseExpressionType, c),
c c
) { ) {
c.trace.report(Errors.USELESS_ELVIS.on(expression.getOperationReference(), baseExpressionType)) c.trace.report(Errors.USELESS_ELVIS.on(expression, baseExpressionType))
} }
} }
JetTokens.EQEQ, JetTokens.EQEQ,
@@ -558,7 +558,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, JetType> UNNECESSARY_SAFE_CALL = DiagnosticFactory1.create(WARNING); DiagnosticFactory1<PsiElement, JetType> UNNECESSARY_SAFE_CALL = DiagnosticFactory1.create(WARNING);
DiagnosticFactory1<PsiElement, JetType> UNNECESSARY_NOT_NULL_ASSERTION = DiagnosticFactory1.create(WARNING); DiagnosticFactory1<PsiElement, JetType> UNNECESSARY_NOT_NULL_ASSERTION = DiagnosticFactory1.create(WARNING);
DiagnosticFactory1<JetExpression, JetType> USELESS_ELVIS = DiagnosticFactory1.create(WARNING); DiagnosticFactory1<JetBinaryExpression, JetType> USELESS_ELVIS = DiagnosticFactory1.create(WARNING, PositioningStrategies.USELESS_ELVIS);
// Compile-time values // Compile-time values
@@ -688,7 +688,7 @@ public interface Errors {
INVISIBLE_MEMBER, INVISIBLE_MEMBER_FROM_INLINE, INVISIBLE_REFERENCE, INVISIBLE_SETTER); INVISIBLE_MEMBER, INVISIBLE_MEMBER_FROM_INLINE, INVISIBLE_REFERENCE, INVISIBLE_SETTER);
ImmutableSet<? extends DiagnosticFactory<?>> UNUSED_ELEMENT_DIAGNOSTICS = ImmutableSet.of( ImmutableSet<? extends DiagnosticFactory<?>> UNUSED_ELEMENT_DIAGNOSTICS = ImmutableSet.of(
UNUSED_VARIABLE, UNUSED_PARAMETER, ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE, VARIABLE_WITH_REDUNDANT_INITIALIZER, UNUSED_VARIABLE, UNUSED_PARAMETER, ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE, VARIABLE_WITH_REDUNDANT_INITIALIZER,
UNUSED_FUNCTION_LITERAL, USELESS_CAST, UNUSED_VALUE); UNUSED_FUNCTION_LITERAL, USELESS_CAST, UNUSED_VALUE, USELESS_ELVIS);
ImmutableSet<? extends DiagnosticFactory<?>> TYPE_INFERENCE_ERRORS = ImmutableSet.of( 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_NO_INFORMATION_FOR_PARAMETER, TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS, TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH,
TYPE_INFERENCE_UPPER_BOUND_VIOLATED, TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH); TYPE_INFERENCE_UPPER_BOUND_VIOLATED, TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH);
@@ -447,4 +447,10 @@ public object PositioningStrategies {
return listOf(TextRange(element.getLeft()!!.startOffset, element.getOperationReference().endOffset)) return listOf(TextRange(element.getLeft()!!.startOffset, element.getOperationReference().endOffset))
} }
} }
public val USELESS_ELVIS: PositioningStrategy<JetBinaryExpression> = object: PositioningStrategy<JetBinaryExpression>() {
override fun mark(element: JetBinaryExpression): List<TextRange> {
return listOf(TextRange(element.getOperationReference().startOffset, element.endOffset))
}
}
} }
@@ -1276,7 +1276,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
assert leftTypeInfo != null : "Left expression was not processed: " + expression; assert leftTypeInfo != null : "Left expression was not processed: " + expression;
JetType leftType = leftTypeInfo.getType(); JetType leftType = leftTypeInfo.getType();
if (leftType != null && isKnownToBeNotNull(left, leftType, context)) { if (leftType != null && isKnownToBeNotNull(left, leftType, context)) {
context.trace.report(USELESS_ELVIS.on(left, leftType)); context.trace.report(USELESS_ELVIS.on(expression, leftType));
} }
JetTypeInfo rightTypeInfo = BindingContextUtils.getRecordedTypeInfo(right, context.trace.getBindingContext()); JetTypeInfo rightTypeInfo = BindingContextUtils.getRecordedTypeInfo(right, context.trace.getBindingContext());
assert rightTypeInfo != null : "Right expression was not processed: " + expression; assert rightTypeInfo != null : "Right expression was not processed: " + expression;
@@ -8,7 +8,7 @@ fun testBinary2() {
} }
fun testElvis1() { fun testElvis1() {
<!USELESS_ELVIS!>todo()<!> <!UNREACHABLE_CODE!>?: ""<!> todo() <!USELESS_ELVIS, UNREACHABLE_CODE!>?: ""<!>
} }
fun testElvis2(s: String?) { fun testElvis2(s: String?) {
@@ -50,6 +50,6 @@ fun test(arr: Array<Int>) {
} }
while (true) { while (true) {
<!USELESS_ELVIS!>break<!> <!UNREACHABLE_CODE!>?: null<!> break <!USELESS_ELVIS, UNREACHABLE_CODE!>?: null<!>
} }
} }
@@ -4,6 +4,6 @@ fun foo() {
val x: Int? = null val x: Int? = null
bar(x ?: 0) bar(x ?: 0)
if (x != null) bar(<!USELESS_ELVIS!>x<!> ?: <!DEBUG_INFO_SMARTCAST!>x<!>) if (x != null) bar(x <!USELESS_ELVIS!>?: <!DEBUG_INFO_SMARTCAST!>x<!><!>)
bar(<!TYPE_MISMATCH!>x<!>) bar(<!TYPE_MISMATCH!>x<!>)
} }
@@ -5,12 +5,12 @@ fun <T: Any?> test1(t: Any?): Any {
} }
fun <T: Any> test2(t: Any?): Any { fun <T: Any> test2(t: Any?): Any {
return <!UNCHECKED_CAST, USELESS_ELVIS!>t as T<!> ?: "" return <!UNCHECKED_CAST!>t as T<!> <!USELESS_ELVIS!>?: ""<!>
} }
fun <T: Any?> test3(t: Any?): Any { fun <T: Any?> test3(t: Any?): Any {
if (t != null) { if (t != null) {
return <!USELESS_ELVIS!>t<!> ?: "" return t <!USELESS_ELVIS!>?: ""<!>
} }
return 1 return 1
@@ -25,21 +25,21 @@ fun test() {
// platform type with no annotation // platform type with no annotation
val platformJ = J.staticJ val platformJ = J.staticJ
val v0 = platformNN <!USELESS_ELVIS!>?:<!> J() val v0 = platformNN <!USELESS_ELVIS!>?: J()<!>
platformNN <!USELESS_ELVIS!>?:<!> J() platformNN <!USELESS_ELVIS!>?: J()<!>
platformN ?: J() platformN ?: J()
platformJ ?: J() platformJ ?: J()
if (platformNN != null) { if (platformNN != null) {
<!USELESS_ELVIS!>platformNN<!> ?: J() platformNN <!USELESS_ELVIS!>?: J()<!>
} }
if (platformN != null) { if (platformN != null) {
<!USELESS_ELVIS!>platformN<!> ?: J() platformN <!USELESS_ELVIS!>?: J()<!>
} }
if (platformJ != null) { if (platformJ != null) {
<!USELESS_ELVIS!>platformJ<!> ?: J() platformJ <!USELESS_ELVIS!>?: J()<!>
} }
} }
@@ -18,10 +18,10 @@ fun test() {
// @NotNull platform type // @NotNull platform type
val platformNN = J.staticNN val platformNN = J.staticNN
foo(platformNN <!USELESS_ELVIS!>?:<!> "") foo(platformNN <!USELESS_ELVIS!>?: ""<!>)
val bar = Bar() val bar = Bar()
bar(platformNN <!USELESS_ELVIS!>?:<!> "") bar(platformNN <!USELESS_ELVIS!>?: ""<!>)
} }
fun foo(a: Any) {} fun foo(a: Any) {}
@@ -96,7 +96,7 @@ public open class JetPsiChecker : Annotator, HighlightRangeExtension {
assert(diagnostic.getPsiElement() == element) assert(diagnostic.getPsiElement() == element)
var textRanges = diagnostic.getTextRanges() val textRanges = diagnostic.getTextRanges()
val factory = diagnostic.getFactory() val factory = diagnostic.getFactory()
when (diagnostic.getSeverity()) { when (diagnostic.getSeverity()) {
Severity.ERROR -> { Severity.ERROR -> {
@@ -85,8 +85,7 @@ public class RemoveRightPartOfBinaryExpressionFix<T extends JetExpression> exten
return new JetSingleIntentionActionFactory() { return new JetSingleIntentionActionFactory() {
@Override @Override
public JetIntentionAction<JetBinaryExpression> createAction(Diagnostic diagnostic) { public JetIntentionAction<JetBinaryExpression> createAction(Diagnostic diagnostic) {
JetBinaryExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpression.class); JetBinaryExpression expression = (JetBinaryExpression) diagnostic.getPsiElement();
if (expression == null) return null;
return new RemoveRightPartOfBinaryExpressionFix<JetBinaryExpression>(expression, JetBundle.message("remove.elvis.operator")); return new RemoveRightPartOfBinaryExpressionFix<JetBinaryExpression>(expression, JetBundle.message("remove.elvis.operator"));
} }
}; };
+1 -1
View File
@@ -1,4 +1,4 @@
// "Remove elvis operator" "true" // "Remove elvis operator" "true"
fun foo(a: String) { fun foo(a: String) {
val b : String = <caret>a ?: "s" val b : String = a <caret>?: "s"
} }
+1 -1
View File
@@ -1,4 +1,4 @@
// "Remove elvis operator" "true" // "Remove elvis operator" "true"
fun foo(a: String) { fun foo(a: String) {
val b : String = <caret>a val b : String = a<caret>
} }