ReplaceGuardClause inspection: check contracts availability

This commit is contained in:
Toshiaki Kameyama
2019-07-01 19:19:11 +09:00
committed by Mikhail Glukhikh
parent 8cbcb66197
commit 2ca0056cd0
3 changed files with 20 additions and 0 deletions
@@ -8,10 +8,12 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
@@ -44,6 +46,8 @@ class ReplaceGuardClauseWithFunctionCallInspection : AbstractApplicabilityBasedI
element.getKotlinFunction()?.let { "Replace with '${it.functionName}()' call" } ?: defaultFixText
override fun isApplicable(element: KtIfExpression): Boolean {
val languageVersionSettings = element.languageVersionSettings
if (!languageVersionSettings.supportsFeature(LanguageFeature.UseReturnsEffect)) return false
if (element.condition == null) return false
val call = element.getCallExpression() ?: return false
val calleeText = call.calleeExpression?.text ?: return false
@@ -0,0 +1,11 @@
// PROBLEM: none
// WITH_RUNTIME
// LANGUAGE_VERSION: 1.2
fun test(foo: Int?) {
<caret>if (foo == null) {
throw IllegalArgumentException("test")
}
bar(foo)
}
fun bar(i: Int) {}
@@ -8924,6 +8924,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/notTargetException.kt");
}
@TestMetadata("version12.kt")
public void testVersion12() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/version12.kt");
}
@TestMetadata("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/check")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)