Remove unnecessary lateinit intention / inspection removed (supported in compiler CFA instead)

(cherry picked from commit e17f222)
This commit is contained in:
Mikhail Glukhikh
2016-09-26 12:01:08 +03:00
committed by Mikhail Glukhikh
parent 67cc83af05
commit 4522d2c7da
26 changed files with 0 additions and 431 deletions
@@ -1,5 +0,0 @@
<html>
<body>
This inspection detects lateinit that can be safely removed
</body>
</html>
@@ -1,7 +0,0 @@
class My {
var x: String
init {
x = "My"
}
}
@@ -1,7 +0,0 @@
class My {
lateinit var x: String
init {
x = "My"
}
}
@@ -1,5 +0,0 @@
<html>
<body>
This intention removes unnecessary lateinit modifiers.
</body>
</html>
-13
View File
@@ -1314,11 +1314,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveUnnecessaryLateinitIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveSingleExpressionStringTemplateIntention</className>
<category>Kotlin</category>
@@ -1736,14 +1731,6 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveUnnecessaryLateinitInspection"
displayName="Unnecessary lateinit"
groupName="Kotlin"
enabledByDefault="true"
level="WARNING"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.conventionNameCalls.ReplaceCallWithComparisonInspection"
displayName="Can be replaced with comparison"
groupName="Kotlin"
@@ -1,90 +0,0 @@
/*
* Copyright 2010-2016 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.intentions
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
import org.jetbrains.kotlin.idea.conversion.copy.range
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
class RemoveUnnecessaryLateinitInspection : IntentionBasedInspection<KtProperty>(RemoveUnnecessaryLateinitIntention::class) {
override val problemHighlightType = ProblemHighlightType.LIKE_UNUSED_SYMBOL
override val problemText = "Unnecessary lateinit"
}
class RemoveUnnecessaryLateinitIntention : SelfTargetingRangeIntention<KtProperty>(KtProperty::class.java, "Remove unnecessary lateinit") {
override fun applicabilityRange(element: KtProperty): TextRange? {
if (!element.hasModifier(KtTokens.LATEINIT_KEYWORD)) return null
val ktClass = element.getStrictParentOfType<KtClass>() ?: return null
if (ktClass.getAnonymousInitializers().any { hasAssignmentStatements(it.body, element) }) {
return element.lateinitTextRange()
}
if (ktClass.hasPrimaryConstructor()) return null
val secondaryConstructors = ktClass.getSecondaryConstructors()
val secondaryConstructorsInfo = secondaryConstructors.map { secondaryConstructor ->
val delegationCall = secondaryConstructor.getDelegationCall()
val hasAssignmentStatements = hasAssignmentStatements(secondaryConstructor.bodyExpression, element)
if (!hasAssignmentStatements && !delegationCall.isCallToThis) return null
val resolvedCall = delegationCall.getResolvedCall(delegationCall.analyzeFully()) ?: return null
val delegationCallIndex = secondaryConstructors.indexOfFirst {
DescriptorToSourceUtils.descriptorToDeclaration(resolvedCall.resultingDescriptor) == it
}
SecondaryConstructorInfo(hasAssignmentStatements, delegationCallIndex)
}
return if (assignmentReachableFromAnySecondaryConstructor(secondaryConstructorsInfo)) element.lateinitTextRange()
else null
}
private fun assignmentReachableFromAnySecondaryConstructor(constructorsInfo: List<SecondaryConstructorInfo>): Boolean {
constructorsInfo.forEach {
val visitedInfo = hashSetOf<SecondaryConstructorInfo>()
var currentConstructor = it
while (!currentConstructor.hasAssignmentStatement) {
if (!visitedInfo.add(currentConstructor)) return false
currentConstructor = constructorsInfo.getOrNull(currentConstructor.delegationCallIndex) ?: return false
}
}
return true
}
private fun KtProperty.lateinitTextRange() = modifierList?.getModifier(KtTokens.LATEINIT_KEYWORD)?.range
private fun hasAssignmentStatements(body: KtExpression?, property: KtProperty) =
body?.children?.any {
it is KtBinaryExpression &&
it.operationToken === KtTokens.EQ &&
it.left?.text == property.name
} ?: false
override fun applyTo(element: KtProperty, editor: Editor?) {
element.removeModifier(KtTokens.LATEINIT_KEYWORD)
}
}
private data class SecondaryConstructorInfo(val hasAssignmentStatement: Boolean, val delegationCallIndex: Int)
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.RemoveUnnecessaryLateinitIntention
@@ -1,9 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
<caret>lateinit var bar: String
constructor(baz: Int) {
bar = ""
}
}
@@ -1,9 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
var bar: String
constructor(baz: Int) {
bar = ""
}
}
@@ -1,12 +0,0 @@
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public constructor Foo() defined in Foo<br>public constructor Foo(x: String, y: String) defined in Foo
class Foo {
<caret>lateinit var x: String
constructor() {
x = "Foo"
}
constructor(x: String, y: String): this(y.hashCode())
}
@@ -1,9 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
<caret>lateinit var bar: String
init {
bar = ""
}
}
@@ -1,9 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
var bar: String
init {
bar = ""
}
}
@@ -1,13 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
<caret>lateinit var bar: String
constructor() {
bar = ""
}
constructor(baz: Int) {
bar = ""
}
}
@@ -1,13 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
var bar: String
constructor() {
bar = ""
}
constructor(baz: Int) {
bar = ""
}
}
@@ -1,15 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
<caret>lateinit var bar: String
constructor() {
bar = ""
}
constructor(a: Int) : this() {
}
constructor(a: Int, b: Int) : this(a) {
}
}
@@ -1,15 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
var bar: String
constructor() {
bar = ""
}
constructor(a: Int) : this() {
}
constructor(a: Int, b: Int) : this(a) {
}
}
@@ -1,14 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
<caret>lateinit var bar: String
var baz: Int
init {
baz = 1
}
init {
bar = ""
}
}
@@ -1,14 +0,0 @@
// INTENTION_TEXT: Remove unnecessary lateinit
class Foo {
var bar: String
var baz: Int
init {
baz = 1
}
init {
bar = ""
}
}
@@ -1,9 +0,0 @@
// IS_APPLICABLE: false
class Foo {
<caret>lateinit var bar: String
constructor(baz: Int) {
bar += baz
}
}
@@ -1,9 +0,0 @@
// IS_APPLICABLE: false
class Foo() {
<caret>lateinit var bar: String
constructor(baz: Int) : this() {
bar = ""
}
}
@@ -1,9 +0,0 @@
// IS_APPLICABLE: false
class Foo {
<caret>lateinit var bar: String
fun init() {
bar = ""
}
}
@@ -1,14 +0,0 @@
// IS_APPLICABLE: true
// ERROR: There's a cycle in the delegation calls chain
class Foo {
<caret>lateinit var bar: String
constructor() {
bar = ""
}
constructor(a: Int) : this(a) {
bar = "a"
}
}
@@ -1,14 +0,0 @@
// IS_APPLICABLE: true
// ERROR: There's a cycle in the delegation calls chain
class Foo {
<caret>var bar: String
constructor() {
bar = ""
}
constructor(a: Int) : this(a) {
bar = "a"
}
}
@@ -1,21 +0,0 @@
// IS_APPLICABLE: false
// ERROR: There's a cycle in the delegation calls chain
// ERROR: There's a cycle in the delegation calls chain
// ERROR: There's a cycle in the delegation calls chain
class Foo {
<caret>lateinit var bar: String
constructor() {
bar = ""
}
constructor(a: Int) : this(a, 0, 0) {
}
constructor(a: Int, b: Int) : this(a) {
}
constructor(a: Int, b: Int, c: Int) : this(a, b) {
}
}
@@ -1,14 +0,0 @@
// IS_APPLICABLE: false
open class Bar(val a: Int = 0)
class Foo : Bar {
<caret>lateinit var bar: String
constructor() : super() {
bar = ""
}
constructor(a: Int) : super(a) {
}
}
@@ -10292,87 +10292,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/removeUnnecessaryLateinit")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveUnnecessaryLateinit extends AbstractIntentionTest {
public void testAllFilesPresentInRemoveUnnecessaryLateinit() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeUnnecessaryLateinit"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("lateinitWithConstructor.kt")
public void testLateinitWithConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/lateinitWithConstructor.kt");
doTest(fileName);
}
@TestMetadata("lateinitWithErroneousDelegation.kt")
public void testLateinitWithErroneousDelegation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/lateinitWithErroneousDelegation.kt");
doTest(fileName);
}
@TestMetadata("lateinitWithInit.kt")
public void testLateinitWithInit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/lateinitWithInit.kt");
doTest(fileName);
}
@TestMetadata("lateinitWithMultipleConstructors.kt")
public void testLateinitWithMultipleConstructors() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/lateinitWithMultipleConstructors.kt");
doTest(fileName);
}
@TestMetadata("lateinitWithMultipleConstructorsAndDelegation.kt")
public void testLateinitWithMultipleConstructorsAndDelegation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/lateinitWithMultipleConstructorsAndDelegation.kt");
doTest(fileName);
}
@TestMetadata("lateinitWithMultipleInit.kt")
public void testLateinitWithMultipleInit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/lateinitWithMultipleInit.kt");
doTest(fileName);
}
@TestMetadata("lateinitWithPlusAssign.kt")
public void testLateinitWithPlusAssign() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/lateinitWithPlusAssign.kt");
doTest(fileName);
}
@TestMetadata("lateinitWithPrimaryConstructorAndConstructor.kt")
public void testLateinitWithPrimaryConstructorAndConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/lateinitWithPrimaryConstructorAndConstructor.kt");
doTest(fileName);
}
@TestMetadata("normalLateinit.kt")
public void testNormalLateinit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/normalLateinit.kt");
doTest(fileName);
}
@TestMetadata("secondaryConstructorDelegateItself.kt")
public void testSecondaryConstructorDelegateItself() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/secondaryConstructorDelegateItself.kt");
doTest(fileName);
}
@TestMetadata("secondaryConstructorDelegateLoop.kt")
public void testSecondaryConstructorDelegateLoop() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/secondaryConstructorDelegateLoop.kt");
doTest(fileName);
}
@TestMetadata("secondaryConstructorDelegateSuper.kt")
public void testSecondaryConstructorDelegateSuper() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryLateinit/secondaryConstructorDelegateSuper.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/removeUnnecessaryParentheses")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)