From e0aca97f9f758a9fe25126c28e4539f6c01f489f Mon Sep 17 00:00:00 2001 From: Andrius Semionovas Date: Wed, 12 Jul 2017 20:39:49 +0300 Subject: [PATCH] Add quick-fix for USELESS_IS_CHECK #KT-18965 Fixed --- .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 + .../idea/quickfix/RemoveUselessIsCheckFix.kt | 46 +++++++++++++++++++ .../expressions/removeUselessIsCheck.kt | 6 +++ .../expressions/removeUselessIsCheck.kt.after | 6 +++ .../expressions/removeUselessIsCheckInWhen.kt | 6 +++ .../expressions/removeUselessIsCheckNegate.kt | 6 +++ .../removeUselessIsCheckNegate.kt.after | 6 +++ .../idea/quickfix/QuickFixTestGenerated.java | 18 ++++++++ 8 files changed, 96 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessIsCheckFix.kt create mode 100644 idea/testData/quickfix/expressions/removeUselessIsCheck.kt create mode 100644 idea/testData/quickfix/expressions/removeUselessIsCheck.kt.after create mode 100644 idea/testData/quickfix/expressions/removeUselessIsCheckInWhen.kt create mode 100644 idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt create mode 100644 idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index c7e0a75870e..197a6e0a6ac 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -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) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessIsCheckFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessIsCheckFix.kt new file mode 100644 index 00000000000..75aa0156d8f --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessIsCheckFix.kt @@ -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(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? { + val expression = diagnostic.psiElement.getNonStrictParentOfType() ?: return null + return RemoveUselessIsCheckFix(expression) + } + } +} diff --git a/idea/testData/quickfix/expressions/removeUselessIsCheck.kt b/idea/testData/quickfix/expressions/removeUselessIsCheck.kt new file mode 100644 index 00000000000..d674aa9020f --- /dev/null +++ b/idea/testData/quickfix/expressions/removeUselessIsCheck.kt @@ -0,0 +1,6 @@ +// "Remove useless is check" "true" +fun foo(a: String) { + if (1 is Int) { + + } +} diff --git a/idea/testData/quickfix/expressions/removeUselessIsCheck.kt.after b/idea/testData/quickfix/expressions/removeUselessIsCheck.kt.after new file mode 100644 index 00000000000..383695353a8 --- /dev/null +++ b/idea/testData/quickfix/expressions/removeUselessIsCheck.kt.after @@ -0,0 +1,6 @@ +// "Remove useless is check" "true" +fun foo(a: String) { + if (true) { + + } +} diff --git a/idea/testData/quickfix/expressions/removeUselessIsCheckInWhen.kt b/idea/testData/quickfix/expressions/removeUselessIsCheckInWhen.kt new file mode 100644 index 00000000000..ea6e0b16136 --- /dev/null +++ b/idea/testData/quickfix/expressions/removeUselessIsCheckInWhen.kt @@ -0,0 +1,6 @@ +// "Remove useless is check" "false" +fun foo(a: String) { + when (1) { + is Int -> { } + } +} diff --git a/idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt b/idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt new file mode 100644 index 00000000000..8bba73dadc2 --- /dev/null +++ b/idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt @@ -0,0 +1,6 @@ +// "Remove useless is check" "true" +fun foo(a: String) { + if (1 !is Int) { + + } +} diff --git a/idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt.after b/idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt.after new file mode 100644 index 00000000000..640d386ef3b --- /dev/null +++ b/idea/testData/quickfix/expressions/removeUselessIsCheckNegate.kt.after @@ -0,0 +1,6 @@ +// "Remove useless is check" "true" +fun foo(a: String) { + if (false) { + + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index f8427b87bd2..024f36a91de 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -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");