Add quick-fix for USELESS_IS_CHECK #KT-18965 Fixed

This commit is contained in:
Andrius Semionovas
2017-07-12 20:39:49 +03:00
committed by Mikhail Glukhikh
parent 88fa7c2952
commit e0aca97f9f
8 changed files with 96 additions and 0 deletions
@@ -100,6 +100,8 @@ class QuickFixRegistrar : QuickFixContributor {
USELESS_CAST.registerFactory(RemoveUselessCastFix)
USELESS_IS_CHECK.registerFactory(RemoveUselessIsCheckFix)
WRONG_SETTER_PARAMETER_TYPE.registerFactory(ChangeAccessorTypeFix)
WRONG_GETTER_RETURN_TYPE.registerFactory(ChangeAccessorTypeFix)
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtIsExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveUselessIsCheckFix(element: KtIsExpression) : KotlinQuickFixAction<KtIsExpression>(element) {
override fun getFamilyName() = "Remove useless is check"
override fun getText(): String = familyName
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element?.run {
val expressionsText = this.isNegated.not().toString()
val newExpression = KtPsiFactory(project).createExpression(expressionsText)
replace(newExpression)
}
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtIsExpression>? {
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtIsExpression>() ?: return null
return RemoveUselessIsCheckFix(expression)
}
}
}
@@ -0,0 +1,6 @@
// "Remove useless is check" "true"
fun foo(a: String) {
if (<caret>1 is Int) {
}
}
@@ -0,0 +1,6 @@
// "Remove useless is check" "true"
fun foo(a: String) {
if (<caret>true) {
}
}
@@ -0,0 +1,6 @@
// "Remove useless is check" "false"
fun foo(a: String) {
when (1) {
<caret>is Int -> { }
}
}
@@ -0,0 +1,6 @@
// "Remove useless is check" "true"
fun foo(a: String) {
if (<caret>1 !is Int) {
}
}
@@ -0,0 +1,6 @@
// "Remove useless is check" "true"
fun foo(a: String) {
if (<caret>false) {
}
}
@@ -5777,6 +5777,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("removeUselessIsCheck.kt")
public void testRemoveUselessIsCheck() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/expressions/removeUselessIsCheck.kt");
doTest(fileName);
}
@TestMetadata("removeUselessIsCheckInWhen.kt")
public void testRemoveUselessIsCheckInWhen() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/expressions/removeUselessIsCheckInWhen.kt");
doTest(fileName);
}
@TestMetadata("removeUselessIsCheckNegate.kt")
public void testRemoveUselessIsCheckNegate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt");
doTest(fileName);
}
@TestMetadata("unnecessaryNonNullAssertion1.kt")
public void testUnnecessaryNonNullAssertion1() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/expressions/unnecessaryNonNullAssertion1.kt");