Introduce "double negation" inspection #KT-4748 Fixed

This commit is contained in:
Dimach
2017-07-25 13:08:27 +03:00
committed by Mikhail Glukhikh
parent be0b01a1e6
commit 88fa7c2952
12 changed files with 140 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports redundant double negation usages, like <code>val truth = !!true</code>
</body>
</html>
+9
View File
@@ -2428,6 +2428,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinDoubleNegationInspection"
displayName="Redundant double negation"
groupPath="Kotlin"
groupName="Redundant constructs"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,69 @@
/*
* 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.inspections
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtParenthesizedExpression
import org.jetbrains.kotlin.psi.KtPrefixExpression
import org.jetbrains.kotlin.psi.KtVisitorVoid
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.types.typeUtil.isBoolean
class KotlinDoubleNegationInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession) =
object : KtVisitorVoid() {
override fun visitPrefixExpression(expression: KtPrefixExpression) {
if (expression.operationToken != KtTokens.EXCL ||
expression.baseExpression?.getType(expression.analyze())?.isBoolean() != true) {
return
}
var parent = expression.parent
while (parent is KtParenthesizedExpression) {
parent = parent.parent
}
if (parent is KtPrefixExpression && parent.operationToken == KtTokens.EXCL) {
holder.registerProblem(expression,
"Redundant double negation",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
DoubleNegationFix())
}
}
}
private class DoubleNegationFix : LocalQuickFix {
override fun getName() = "Remove redundant negations"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
applyFix(descriptor.psiElement as? KtPrefixExpression ?: return)
}
private fun applyFix(expression: KtPrefixExpression) {
var parent = expression.parent
while (parent is KtParenthesizedExpression) {
parent = parent.parent
}
if (parent is KtPrefixExpression && parent.operationToken == KtTokens.EXCL) {
expression.baseExpression?.let { parent.replaced(it) }
}
}
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.KotlinDoubleNegationInspection
@@ -0,0 +1,3 @@
fun foo() {
val b = !!<caret>"".equals("")
}
@@ -0,0 +1,3 @@
fun foo() {
val b = "".equals("")
}
@@ -0,0 +1,5 @@
// PROBLEM: none
operator fun Int.not() = this * -1
fun foo() {
val c = !!<caret>1
}
@@ -0,0 +1,3 @@
fun foo() {
val b = !(!<caret>true)
}
@@ -0,0 +1,3 @@
fun foo() {
val b = true
}
@@ -0,0 +1,3 @@
fun foo() {
val b = !!<caret>true
}
@@ -0,0 +1,3 @@
fun foo() {
val b = true
}
@@ -492,6 +492,39 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/doubleNegation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class DoubleNegation extends AbstractLocalInspectionTest {
public void testAllFilesPresentInDoubleNegation() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/doubleNegation"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/doubleNegation/function.kt");
doTest(fileName);
}
@TestMetadata("invalid.kt")
public void testInvalid() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/doubleNegation/invalid.kt");
doTest(fileName);
}
@TestMetadata("parenthesized.kt")
public void testParenthesized() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/doubleNegation/parenthesized.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/doubleNegation/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/emptyRange")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)