Redundant getter / setter inspection #KT-19514 Fixed (#1282)
This commit is contained in:
committed by
Dmitry Jemerov
parent
a3060f1073
commit
e01371b231
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports redundant property getter.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports redundant property setter.
|
||||
</body>
|
||||
</html>
|
||||
@@ -2560,6 +2560,26 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantGetterInspection"
|
||||
displayName="Redundant property getter"
|
||||
groupPath="Kotlin"
|
||||
groupName="Redundant constructs"
|
||||
enabledByDefault="true"
|
||||
cleanupTool="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantSetterInspection"
|
||||
displayName="Redundant property setter"
|
||||
groupPath="Kotlin"
|
||||
groupName="Redundant constructs"
|
||||
enabledByDefault="true"
|
||||
cleanupTool="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,66 @@
|
||||
/*
|
||||
* 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 com.intellij.codeInspection.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
class RedundantGetterInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitPropertyAccessor(accessor: KtPropertyAccessor) {
|
||||
super.visitPropertyAccessor(accessor)
|
||||
if (accessor.isRedundantGetter()) {
|
||||
holder.registerProblem(accessor,
|
||||
"Redundant getter",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
RemoveRedundantGetterFix())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtPropertyAccessor.isRedundantGetter(): Boolean {
|
||||
if (!isGetter) return false
|
||||
if (annotationEntries.isNotEmpty()) return false
|
||||
val expression = bodyExpression ?: return true
|
||||
if (expression is KtNameReferenceExpression) {
|
||||
return expression.isFieldText()
|
||||
}
|
||||
if (expression is KtBlockExpression) {
|
||||
val statement = expression.statements.takeIf { it.size == 1 }?.firstOrNull() ?: return false
|
||||
val returnExpression = statement as? KtReturnExpression ?: return false
|
||||
return returnExpression.returnedExpression?.isFieldText() == true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun KtExpression.isFieldText(): Boolean = this.textMatches("field")
|
||||
|
||||
private class RemoveRedundantGetterFix : LocalQuickFix {
|
||||
override fun getName() = "Remove redundant getter"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val accessor = descriptor.psiElement as? KtPropertyAccessor ?: return
|
||||
accessor.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 com.intellij.codeInspection.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
class RedundantSetterInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitPropertyAccessor(accessor: KtPropertyAccessor) {
|
||||
super.visitPropertyAccessor(accessor)
|
||||
if (accessor.isRedundantSetter()) {
|
||||
holder.registerProblem(accessor,
|
||||
"Redundant setter",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
RemoveRedundantSetterFix())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtPropertyAccessor.isRedundantSetter(): Boolean {
|
||||
if (!isSetter) return false
|
||||
if (annotationEntries.isNotEmpty()) 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
|
||||
return binaryExpression.operationToken == KtTokens.EQ
|
||||
&& binaryExpression.left?.isFieldText() == true
|
||||
&& binaryExpression.right?.mainReference?.resolve() == parameter
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun KtPropertyAccessor.hasLowerVisibilityThanProperty(): Boolean {
|
||||
val p = property
|
||||
return when {
|
||||
p.hasModifier(KtTokens.PRIVATE_KEYWORD) ->
|
||||
false
|
||||
p.hasModifier(KtTokens.PROTECTED_KEYWORD) ->
|
||||
hasModifier(KtTokens.PRIVATE_KEYWORD)
|
||||
p.hasModifier(KtTokens.INTERNAL_KEYWORD) ->
|
||||
hasModifier(KtTokens.PRIVATE_KEYWORD) || hasModifier(KtTokens.PROTECTED_KEYWORD)
|
||||
else ->
|
||||
hasModifier(KtTokens.PRIVATE_KEYWORD) || hasModifier(KtTokens.PROTECTED_KEYWORD) || hasModifier(KtTokens.INTERNAL_KEYWORD)
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtExpression.isFieldText(): Boolean = this.textMatches("field")
|
||||
|
||||
private class RemoveRedundantSetterFix : LocalQuickFix {
|
||||
override fun getName() = "Remove redundant setter"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val accessor = descriptor.psiElement as? KtPropertyAccessor ?: return
|
||||
accessor.delete()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.RedundantGetterInspection
|
||||
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
annotation class Inject
|
||||
|
||||
class Test {
|
||||
val x = 1
|
||||
@Inject get<caret>
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class Test {
|
||||
val x = 1
|
||||
<caret>get
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
val x = 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class Test {
|
||||
val x = 1
|
||||
<caret>get() = field
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
val x = 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// PROBLEM: none
|
||||
class Test {
|
||||
val x: Int <caret>get() = 10
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
class Test {
|
||||
val x = 1
|
||||
<caret>get() {
|
||||
foo()
|
||||
return field
|
||||
}
|
||||
|
||||
fun foo() {}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
val x = 1
|
||||
<caret>get() {
|
||||
return field
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
val x = 1
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.RedundantSetterInspection
|
||||
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
annotation class Inject
|
||||
|
||||
class Test {
|
||||
var x = 1
|
||||
@Inject set<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
var x = 1
|
||||
<caret>set(value) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
var x = 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class Test {
|
||||
var x = 1
|
||||
<caret>set
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
var x = 1
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
class Test {
|
||||
var x = 1
|
||||
<caret>private set
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
class Test {
|
||||
internal var x = 1
|
||||
<caret>private set
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
class Test {
|
||||
protected var x = 1
|
||||
<caret>private set
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
class Test {
|
||||
var x = 1
|
||||
<caret>set(value) {
|
||||
foo()
|
||||
field = value
|
||||
}
|
||||
|
||||
fun foo() {}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
var x = 1
|
||||
<caret>set(value) {
|
||||
field = value
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
var x = 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class Test {
|
||||
internal var x = 1
|
||||
<caret>internal set
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
internal var x = 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class Test {
|
||||
protected var x = 1
|
||||
<caret>protected set
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
protected var x = 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class Test {
|
||||
private var x = 1
|
||||
<caret>private set
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
private var x = 1
|
||||
}
|
||||
@@ -1305,6 +1305,51 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/redundantGetter")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RedundantGetter extends AbstractLocalInspectionTest {
|
||||
public void testAllFilesPresentInRedundantGetter() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantGetter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("annotation.kt")
|
||||
public void testAnnotation() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/annotation.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("default.kt")
|
||||
public void testDefault() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/default.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("fieldExpression.kt")
|
||||
public void testFieldExpression() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/fieldExpression.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notFieldExpression.kt")
|
||||
public void testNotFieldExpression() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/notFieldExpression.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notOnlyReturnFieldBody.kt")
|
||||
public void testNotOnlyReturnFieldBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/notOnlyReturnFieldBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("onlyReturnFieldBody.kt")
|
||||
public void testOnlyReturnFieldBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/onlyReturnFieldBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/redundantLambdaArrow")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
@@ -1395,6 +1440,81 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/redundantSetter")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RedundantSetter extends AbstractLocalInspectionTest {
|
||||
public void testAllFilesPresentInRedundantSetter() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantSetter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("annotation.kt")
|
||||
public void testAnnotation() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/annotation.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("blankBody.kt")
|
||||
public void testBlankBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/blankBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("default.kt")
|
||||
public void testDefault() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/default.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("lowerVisibility1.kt")
|
||||
public void testLowerVisibility1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/lowerVisibility1.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("lowerVisibility2.kt")
|
||||
public void testLowerVisibility2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/lowerVisibility2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("lowerVisibility3.kt")
|
||||
public void testLowerVisibility3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/lowerVisibility3.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notOnlyReturnField.kt")
|
||||
public void testNotOnlyReturnField() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/notOnlyReturnField.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("onlyReturnFieldBody.kt")
|
||||
public void testOnlyReturnFieldBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/onlyReturnFieldBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("sameVisibility1.kt")
|
||||
public void testSameVisibility1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/sameVisibility1.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("sameVisibility2.kt")
|
||||
public void testSameVisibility2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/sameVisibility2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("sameVisibility3.kt")
|
||||
public void testSameVisibility3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSetter/sameVisibility3.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user