Introduce inspection for callables with implicit 'Nothing?' type

Inspection is reported only for vars and open callables
So #KT-21023 Fixed
This commit is contained in:
Toshiaki Kameyama
2017-11-01 09:05:40 +03:00
committed by Mikhail Glukhikh
parent bcbeab00d5
commit 0fffb9fb17
12 changed files with 108 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports variables / functions with implicit `Nothing?` type.
</body>
</html>
+9
View File
@@ -2642,6 +2642,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ImplicitNullableNothingTypeInspection"
displayName="Implicit `Nothing?` type"
groupPath="Kotlin"
groupName="Probable bugs"
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,35 @@
/*
* 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 org.jetbrains.kotlin.idea.core.getModalityFromDescriptor
import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.types.typeUtil.isNullableNothing
class ImplicitNullableNothingTypeInspection : IntentionBasedInspection<KtCallableDeclaration>(
intention = SpecifyTypeExplicitlyIntention::class,
additionalChecker = { declaration ->
SpecifyTypeExplicitlyIntention.getTypeForDeclaration(declaration).isNullableNothing() &&
(declaration.getModalityFromDescriptor() == KtTokens.OPEN_KEYWORD ||
declaration is KtProperty && declaration.isVar)
},
problemText = "Implicit `Nothing?` type"
) {
override fun inspectionTarget(element: KtCallableDeclaration) = element.nameIdentifier
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.ImplicitNullableNothingTypeInspection
@@ -0,0 +1,5 @@
// PROBLEM: none
class My {
fun <caret>foo() = null
}
@@ -0,0 +1,3 @@
open class My {
open fun <caret>foo() = null
}
@@ -0,0 +1,3 @@
open class My {
open fun foo(): Nothing? = null
}
@@ -0,0 +1,3 @@
// PROBLEM: none
fun <caret>foo() = null
@@ -0,0 +1,3 @@
// PROBLEM: none
val <caret>x = null
@@ -0,0 +1 @@
var <caret>x = null
@@ -0,0 +1 @@
var x: Nothing? = null
@@ -582,6 +582,45 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/ImplicitNullableNothingType")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ImplicitNullableNothingType extends AbstractLocalInspectionTest {
public void testAllFilesPresentInImplicitNullableNothingType() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/ImplicitNullableNothingType"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("final.kt")
public void testFinal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/ImplicitNullableNothingType/final.kt");
doTest(fileName);
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/ImplicitNullableNothingType/function.kt");
doTest(fileName);
}
@TestMetadata("top.kt")
public void testTop() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/ImplicitNullableNothingType/top.kt");
doTest(fileName);
}
@TestMetadata("val.kt")
public void testVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/ImplicitNullableNothingType/val.kt");
doTest(fileName);
}
@TestMetadata("variable.kt")
public void testVariable() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/ImplicitNullableNothingType/variable.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/kdocMissingDocumentation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)