From d09b20f11de54901f900a3136c600687ac9c968d Mon Sep 17 00:00:00 2001 From: Enterman <43851243+Enteerman@users.noreply.github.com> Date: Fri, 7 Aug 2020 15:51:14 +0800 Subject: [PATCH] Inspection to replace !string.isBlank() with string.isNotBlank() #KT-40769 Fixed --- .../ReplaceNegatedIsEmptyWithIsNotEmpty.html | 2 +- .../messages/KotlinBundle.properties | 2 +- ...eNegatedIsEmptyWithIsNotEmptyInspection.kt | 10 ++++++--- .../notNegateBlank.kt | 5 +++++ .../{notNegate.kt => notNegateEmpty.kt} | 0 .../stringIsBlank.kt | 6 ++++++ .../stringIsBlank.kt.after | 6 ++++++ .../stringIsNotBlank.kt | 6 ++++++ .../stringIsNotBlank.kt.after | 6 ++++++ .../intentions/invertIfCondition/isBlank.kt | 10 +++++++++ .../invertIfCondition/isBlank.kt.after | 10 +++++++++ .../invertIfCondition/isNotBlank.kt | 10 +++++++++ .../invertIfCondition/isNotBlank.kt.after | 10 +++++++++ .../invertIfCondition/negatedIsBlank.kt | 10 +++++++++ .../invertIfCondition/negatedIsBlank.kt.after | 10 +++++++++ .../invertIfCondition/negatedIsNotBlank.kt | 10 +++++++++ .../negatedIsNotBlank.kt.after | 10 +++++++++ .../LocalInspectionTestGenerated.java | 21 ++++++++++++++++--- .../intentions/IntentionTestGenerated.java | 20 ++++++++++++++++++ 19 files changed, 156 insertions(+), 8 deletions(-) create mode 100644 idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegateBlank.kt rename idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/{notNegate.kt => notNegateEmpty.kt} (100%) create mode 100644 idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt create mode 100644 idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt.after create mode 100644 idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt create mode 100644 idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt.after create mode 100644 idea/testData/intentions/invertIfCondition/isBlank.kt create mode 100644 idea/testData/intentions/invertIfCondition/isBlank.kt.after create mode 100644 idea/testData/intentions/invertIfCondition/isNotBlank.kt create mode 100644 idea/testData/intentions/invertIfCondition/isNotBlank.kt.after create mode 100644 idea/testData/intentions/invertIfCondition/negatedIsBlank.kt create mode 100644 idea/testData/intentions/invertIfCondition/negatedIsBlank.kt.after create mode 100644 idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt create mode 100644 idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt.after diff --git a/idea/resources-en/inspectionDescriptions/ReplaceNegatedIsEmptyWithIsNotEmpty.html b/idea/resources-en/inspectionDescriptions/ReplaceNegatedIsEmptyWithIsNotEmpty.html index cd61646701f..a8b949f191e 100644 --- a/idea/resources-en/inspectionDescriptions/ReplaceNegatedIsEmptyWithIsNotEmpty.html +++ b/idea/resources-en/inspectionDescriptions/ReplaceNegatedIsEmptyWithIsNotEmpty.html @@ -1,5 +1,5 @@ -This inspection reports !collection.isEmpty()/isNotEmpty() call can be replaced with collection.isNotEmpty()/isEmpty(). +This inspection reports !collection.isEmpty()/isNotEmpty() or !str.isBlank()/isNotBlank() call can be replaced with collection.isNotEmpty()/isEmpty() or str.isNotBlank()/isBlank(). \ No newline at end of file diff --git a/idea/resources-en/messages/KotlinBundle.properties b/idea/resources-en/messages/KotlinBundle.properties index 95060003d32..80172210454 100644 --- a/idea/resources-en/messages/KotlinBundle.properties +++ b/idea/resources-en/messages/KotlinBundle.properties @@ -2017,7 +2017,7 @@ inspection.kotlin.throwable.not.thrown.display.name=Throwable not thrown inspection.redundant.require.not.null.call.display.name=Redundant 'requireNotNull' or 'checkNotNull' call inspection.replace.range.start.end.inclusive.with.first.last.display.name=Replace Range 'start' or 'endInclusive' with 'first' or 'last' inspection.redundant.enum.constructor.invocation.display.name=Redundant enum constructor invocation -inspection.replace.negated.is.empty.with.is.not.empty.display.name=Replace negated 'isEmpty' with 'isNotEmpty' +inspection.replace.negated.is.empty.with.is.not.empty.display.name=Replace negated 'isEmpty'/'isBlank' with 'isNotEmpty'/'isNotBlank' inspection.function.with.lambda.expression.body.display.name=Function with `= { ... }` and inferred return type inspection.suspend.function.on.coroutine.scope.display.name=Ambiguous coroutineContext due to CoroutineScope receiver of suspend function inspection.boolean.literal.argument.display.name=Boolean literal argument without parameter name diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceNegatedIsEmptyWithIsNotEmptyInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceNegatedIsEmptyWithIsNotEmptyInspection.kt index ce0c834aaa4..42c78afee7c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceNegatedIsEmptyWithIsNotEmptyInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceNegatedIsEmptyWithIsNotEmptyInspection.kt @@ -45,10 +45,14 @@ class ReplaceNegatedIsEmptyWithIsNotEmptyInspection : AbstractKotlinInspection() val calleeText = calleeExpression.text val isEmptyCall = calleeText == "isEmpty" val isNotEmptyCall = calleeText == "isNotEmpty" - if (!isEmptyCall && !isNotEmptyCall) return null + val isBlankCall = calleeText == "isBlank" + val isNotBlankCall = calleeText == "isNotBlank" + if (!isEmptyCall && !isNotEmptyCall && !isBlankCall && !isNotBlankCall) return null if (isEmptyCall && isEmptyFunctions.none { callExpression.isCalling(FqName(it)) } - || isNotEmptyCall && isNotEmptyFunctions.none { callExpression.isCalling(FqName(it)) }) return null - val to = if (isEmptyCall) "isNotEmpty" else "isEmpty" + || isNotEmptyCall && isNotEmptyFunctions.none { callExpression.isCalling(FqName(it)) } + || isBlankCall && !callExpression.isCalling(FqName("kotlin.text.isBlank")) + || isNotBlankCall && !callExpression.isCalling(FqName("kotlin.text.isNotBlank"))) return null + val to = if (isEmptyCall) "isNotEmpty" else if (isNotEmptyCall) "isEmpty" else if (isBlankCall) "isNotBlank" else "isBlank" return KtPsiFactory(this).createExpressionByPattern( "$0.$to()", this.receiverExpression, diff --git a/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegateBlank.kt b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegateBlank.kt new file mode 100644 index 00000000000..bcd71d929a4 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegateBlank.kt @@ -0,0 +1,5 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(s: String) { + val b = s.isBlank() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegate.kt b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegateEmpty.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegate.kt rename to idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegateEmpty.kt diff --git a/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt new file mode 100644 index 00000000000..8fde175c433 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt @@ -0,0 +1,6 @@ +// PROBLEM: Replace negated 'isBlank' with 'isNotBlank' +// FIX: Replace negated 'isBlank' with 'isNotBlank' +// WITH_RUNTIME +fun test(s: String) { + val b = !s.isBlank() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt.after b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt.after new file mode 100644 index 00000000000..44a1aaf6dd8 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt.after @@ -0,0 +1,6 @@ +// PROBLEM: Replace negated 'isBlank' with 'isNotBlank' +// FIX: Replace negated 'isBlank' with 'isNotBlank' +// WITH_RUNTIME +fun test(s: String) { + val b = s.isNotBlank() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt new file mode 100644 index 00000000000..9be0e6a646c --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt @@ -0,0 +1,6 @@ +// PROBLEM: Replace negated 'isNotBlank' with 'isBlank' +// FIX: Replace negated 'isNotBlank' with 'isBlank' +// WITH_RUNTIME +fun test(s: String) { + val b = !s.isNotBlank() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt.after b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt.after new file mode 100644 index 00000000000..c52cd389c09 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt.after @@ -0,0 +1,6 @@ +// PROBLEM: Replace negated 'isNotBlank' with 'isBlank' +// FIX: Replace negated 'isNotBlank' with 'isBlank' +// WITH_RUNTIME +fun test(s: String) { + val b = s.isBlank() +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/isBlank.kt b/idea/testData/intentions/invertIfCondition/isBlank.kt new file mode 100644 index 00000000000..cda14a0a933 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/isBlank.kt @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun foo(i: Int) {} + +fun test(s: String) { + if (s.isBlank()) { + foo(1) + } else { + foo(2) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/isBlank.kt.after b/idea/testData/intentions/invertIfCondition/isBlank.kt.after new file mode 100644 index 00000000000..3f17b577582 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/isBlank.kt.after @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun foo(i: Int) {} + +fun test(s: String) { + if (s.isNotBlank()) { + foo(2) + } else { + foo(1) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/isNotBlank.kt b/idea/testData/intentions/invertIfCondition/isNotBlank.kt new file mode 100644 index 00000000000..d5a4312e785 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/isNotBlank.kt @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun foo(i: Int) {} + +fun test(s: String) { + if (s.isNotBlank()) { + foo(1) + } else { + foo(2) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/isNotBlank.kt.after b/idea/testData/intentions/invertIfCondition/isNotBlank.kt.after new file mode 100644 index 00000000000..1ce6f504d2a --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/isNotBlank.kt.after @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun foo(i: Int) {} + +fun test(s: String) { + if (s.isBlank()) { + foo(2) + } else { + foo(1) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/negatedIsBlank.kt b/idea/testData/intentions/invertIfCondition/negatedIsBlank.kt new file mode 100644 index 00000000000..7a52c89790c --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/negatedIsBlank.kt @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun foo(i: Int) {} + +fun test(s: String) { + if (!s.isBlank()) { + foo(1) + } else { + foo(2) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/negatedIsBlank.kt.after b/idea/testData/intentions/invertIfCondition/negatedIsBlank.kt.after new file mode 100644 index 00000000000..1ce6f504d2a --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/negatedIsBlank.kt.after @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun foo(i: Int) {} + +fun test(s: String) { + if (s.isBlank()) { + foo(2) + } else { + foo(1) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt b/idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt new file mode 100644 index 00000000000..b4cb4a55547 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun foo(i: Int) {} + +fun test(s: String) { + if (!s.isNotBlank()) { + foo(1) + } else { + foo(2) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt.after b/idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt.after new file mode 100644 index 00000000000..3f17b577582 --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt.after @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun foo(i: Int) {} + +fun test(s: String) { + if (s.isNotBlank()) { + foo(2) + } else { + foo(1) + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 445671792cd..d0f078f080c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -11223,9 +11223,14 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } - @TestMetadata("notNegate.kt") - public void testNotNegate() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegate.kt"); + @TestMetadata("notNegateBlank.kt") + public void testNotNegateBlank() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegateBlank.kt"); + } + + @TestMetadata("notNegateEmpty.kt") + public void testNotNegateEmpty() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/notNegateEmpty.kt"); } @TestMetadata("simple.kt") @@ -11233,11 +11238,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/simple.kt"); } + @TestMetadata("stringIsBlank.kt") + public void testStringIsBlank() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsBlank.kt"); + } + @TestMetadata("stringIsEmpty.kt") public void testStringIsEmpty() throws Exception { runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsEmpty.kt"); } + @TestMetadata("stringIsNotBlank.kt") + public void testStringIsNotBlank() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotBlank.kt"); + } + @TestMetadata("stringIsNotEmpty.kt") public void testStringIsNotEmpty() throws Exception { runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/stringIsNotEmpty.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 45891da9f04..3f84b3a7029 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -9940,11 +9940,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/invertIfCondition/is.kt"); } + @TestMetadata("isBlank.kt") + public void testIsBlank() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/isBlank.kt"); + } + @TestMetadata("isEmpty.kt") public void testIsEmpty() throws Exception { runTest("idea/testData/intentions/invertIfCondition/isEmpty.kt"); } + @TestMetadata("isNotBlank.kt") + public void testIsNotBlank() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/isNotBlank.kt"); + } + @TestMetadata("isNotEmpty.kt") public void testIsNotEmpty() throws Exception { runTest("idea/testData/intentions/invertIfCondition/isNotEmpty.kt"); @@ -10015,11 +10025,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/invertIfCondition/negatedExpression.kt"); } + @TestMetadata("negatedIsBlank.kt") + public void testNegatedIsBlank() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/negatedIsBlank.kt"); + } + @TestMetadata("negatedIsEmpty.kt") public void testNegatedIsEmpty() throws Exception { runTest("idea/testData/intentions/invertIfCondition/negatedIsEmpty.kt"); } + @TestMetadata("negatedIsNotBlank.kt") + public void testNegatedIsNotBlank() throws Exception { + runTest("idea/testData/intentions/invertIfCondition/negatedIsNotBlank.kt"); + } + @TestMetadata("negatedIsNotEmpty.kt") public void testNegatedIsNotEmpty() throws Exception { runTest("idea/testData/intentions/invertIfCondition/negatedIsNotEmpty.kt");