Correct report of "redundant setter" for override & empty block cases
So #KT-22364 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
0ad3872d1b
commit
69f3f04fe4
@@ -28,11 +28,9 @@ class RedundantSetterInspection : AbstractKotlinInspection(), CleanupLocalInspec
|
||||
private fun KtPropertyAccessor.isRedundantSetter(): Boolean {
|
||||
if (!isSetter) return false
|
||||
if (annotationEntries.isNotEmpty()) return false
|
||||
if (property.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return false
|
||||
if (hasLowerVisibilityThanProperty()) return false
|
||||
val expression = bodyExpression ?: return true
|
||||
if (expression is KtBlockExpression) {
|
||||
if (expression.statements.isEmpty()) return true
|
||||
val statement = expression.statements.takeIf { it.size == 1 }?.firstOrNull() ?: return false
|
||||
val parameter = valueParameters.takeIf { it.size == 1 }?.firstOrNull() ?: return false
|
||||
val binaryExpression = statement as? KtBinaryExpression ?: return false
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
class Test {
|
||||
var x = 1
|
||||
<caret>set(value) {
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
class Test {
|
||||
var x = 1
|
||||
}
|
||||
+1
-1
@@ -5,5 +5,5 @@ interface A {
|
||||
|
||||
class X : A {
|
||||
override var myVar: Boolean = false
|
||||
<caret>set(value) {}
|
||||
<caret>set(value) {}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
interface A {
|
||||
var myVar: Boolean
|
||||
}
|
||||
|
||||
class X : A {
|
||||
override var myVar: Boolean = false
|
||||
<caret>set
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
interface A {
|
||||
var myVar: Boolean
|
||||
}
|
||||
|
||||
class X : A {
|
||||
override var myVar: Boolean = false
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// PROBLEM: none
|
||||
interface A {
|
||||
var myVar: Boolean
|
||||
}
|
||||
|
||||
class X : A {
|
||||
override var myVar: Boolean = false
|
||||
<caret>set(value) {
|
||||
foo()
|
||||
field = value
|
||||
}
|
||||
fun foo() {}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
interface A {
|
||||
var myVar: Boolean
|
||||
}
|
||||
|
||||
class X : A {
|
||||
override var myVar: Boolean = false
|
||||
<caret>set(value) {
|
||||
field = value
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
interface A {
|
||||
var myVar: Boolean
|
||||
}
|
||||
|
||||
class X : A {
|
||||
override var myVar: Boolean = false
|
||||
}
|
||||
+27
-9
@@ -2898,21 +2898,39 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notOnlyReturnField.kt")
|
||||
public void testNotOnlyReturnField() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/notOnlyReturnField.kt");
|
||||
@TestMetadata("notOnlyFieldSetBody.kt")
|
||||
public void testNotOnlyFieldSetBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/notOnlyFieldSetBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("onlyReturnFieldBody.kt")
|
||||
public void testOnlyReturnFieldBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/onlyReturnFieldBody.kt");
|
||||
@TestMetadata("onlyFieldSetBody.kt")
|
||||
public void testOnlyFieldSetBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/onlyFieldSetBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("override.kt")
|
||||
public void testOverride() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/override.kt");
|
||||
@TestMetadata("overrideBlankBody.kt")
|
||||
public void testOverrideBlankBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/overrideBlankBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("overrideDefault.kt")
|
||||
public void testOverrideDefault() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/overrideDefault.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("overrideNotOnlyFieldSetBody.kt")
|
||||
public void testOverrideNotOnlyFieldSetBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/overrideNotOnlyFieldSetBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("overrideOnlyFieldSetBody.kt")
|
||||
public void testOverrideOnlyFieldSetBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/overrideOnlyFieldSetBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user