From 867925d5a19ca6917d21d22138f1cdda8631da21 Mon Sep 17 00:00:00 2001 From: Zack Grannan Date: Sun, 30 Mar 2014 20:42:57 -0700 Subject: [PATCH] Added ifThenToSafeAccessInspection --- .../IfThenToSafeAccess.html | 5 + idea/src/META-INF/plugin.xml | 7 ++ .../IfThenToSafeAccessInspection.kt | 22 ++++ .../inspectionData/expected.xml | 114 ++++++++++++++++++ .../inspectionData/inspections.test | 1 + .../JetInspectionTestGenerated.java | 15 ++- .../CodeTransformationTestGenerated.java | 8 +- 7 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 idea/resources/inspectionDescriptions/IfThenToSafeAccess.html create mode 100644 idea/src/org/jetbrains/jet/plugin/inspections/IfThenToSafeAccessInspection.kt create mode 100644 idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/expected.xml create mode 100644 idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test diff --git a/idea/resources/inspectionDescriptions/IfThenToSafeAccess.html b/idea/resources/inspectionDescriptions/IfThenToSafeAccess.html new file mode 100644 index 00000000000..cf6fff1bd95 --- /dev/null +++ b/idea/resources/inspectionDescriptions/IfThenToSafeAccess.html @@ -0,0 +1,5 @@ + + +This inspection reports any if-then expressions that can be folded into safe-access (?.) expressions + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index ae90fef2ed3..349deea6608 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -665,6 +665,13 @@ Kotlin + + diff --git a/idea/src/org/jetbrains/jet/plugin/inspections/IfThenToSafeAccessInspection.kt b/idea/src/org/jetbrains/jet/plugin/inspections/IfThenToSafeAccessInspection.kt new file mode 100644 index 00000000000..10de83c51ff --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/inspections/IfThenToSafeAccessInspection.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2014 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.jet.plugin.inspections + +import org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.IfThenToSafeAccessIntention +import org.jetbrains.jet.lang.psi.JetIfExpression + +public class IfThenToSafeAccessInspection : IntentionBasedInspection(IfThenToSafeAccessIntention()) diff --git a/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/expected.xml b/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/expected.xml new file mode 100644 index 00000000000..24ef90af04b --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/expected.xml @@ -0,0 +1,114 @@ + + + rhsNotEqualsNull.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + rhsEqualsNull.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + noThenBlock.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + noElseBlock.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + lhsNotEqualsNull.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + lhsEqualsNull.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + ifAsExpression.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + ifAndElseNotInBlocks.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + ifAndElseBothInBlocks.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + emptyThenBlock.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + emptyElseBlock.kt + 7 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + doesNotinlineValueOutsideOfScope.kt + 8 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + doesNotinlineValueIfUsedMoreThanOnce.kt + 10 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + + willNotInlineClassProperty.kt + 3 + light_idea_test_case + + If-Then foldable to '?.' + Replace 'if' expression with safe access expression + + diff --git a/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test b/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test new file mode 100644 index 00000000000..a2552cc8cc2 --- /dev/null +++ b/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test @@ -0,0 +1 @@ +// INSPECTION_CLASS: org.jetbrains.jet.plugin.inspections.IfThenToSafeAccessInspection diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/JetInspectionTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/JetInspectionTestGenerated.java index 8c31f4c530e..88c134fb0ea 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/JetInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/JetInspectionTestGenerated.java @@ -16,17 +16,11 @@ package org.jetbrains.jet.plugin.codeInsight; -import junit.framework.Assert; -import junit.framework.Test; -import junit.framework.TestSuite; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.test.TestMetadata; import java.io.File; import java.util.regex.Pattern; -import org.jetbrains.jet.JetTestUtils; -import org.jetbrains.jet.test.InnerTestClasses; -import org.jetbrains.jet.test.TestMetadata; - -import org.jetbrains.jet.plugin.codeInsight.AbstractJetInspectionTest; /** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ @SuppressWarnings("all") @@ -41,4 +35,9 @@ public class JetInspectionTestGenerated extends AbstractJetInspectionTest { doTest("idea/testData/intentions/attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test"); } + @TestMetadata("branched/ifThenToSafeAccess/inspectionData/inspections.test") + public void testBranched_ifThenToSafeAccess_inspectionData_Inspections_test() throws Exception { + doTest("idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test"); + } + } diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index 00ab17613f1..88735bbf1c9 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -357,6 +357,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT } @TestMetadata("idea/testData/intentions/branched/ifThenToSafeAccess") + @InnerTestClasses({}) public static class IfThenToSafeAccess extends AbstractCodeTransformationTest { public void testAllFilesPresentInIfThenToSafeAccess() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/branched/ifThenToSafeAccess"), Pattern.compile("^(.+)\\.kt$"), true); @@ -532,6 +533,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestIfThenToSafeAccess("idea/testData/intentions/branched/ifThenToSafeAccess/willNotInlineClassProperty.kt"); } + public static Test innerSuite() { + TestSuite suite = new TestSuite("IfThenToSafeAccess"); + suite.addTestSuite(IfThenToSafeAccess.class); + return suite; + } } @TestMetadata("idea/testData/intentions/branched/folding/ifToAssignment") @@ -3420,7 +3426,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT suite.addTestSuite(ElvisToIfThen.class); suite.addTestSuite(IfThenToElvis.class); suite.addTestSuite(SafeAccessToIfThen.class); - suite.addTestSuite(IfThenToSafeAccess.class); + suite.addTest(IfThenToSafeAccess.innerSuite()); suite.addTestSuite(IfToAssignment.class); suite.addTestSuite(IfToReturn.class); suite.addTestSuite(IfToReturnAsymmetrically.class);