"Redundant visibility": Fix false positive for overridden setter
#KT-26051 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
1c8e75eb34
commit
3be5f2b843
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -251,8 +252,20 @@ fun KtModifierListOwner.setVisibility(visibilityModifier: KtModifierKeywordToken
|
||||
addModifier(visibilityModifier)
|
||||
}
|
||||
|
||||
fun KtDeclaration.implicitVisibility(): KtModifierKeywordToken? =
|
||||
when {
|
||||
fun KtDeclaration.implicitVisibility(): KtModifierKeywordToken? {
|
||||
if (this is KtPropertyAccessor && this.isSetter) {
|
||||
val property = this.property
|
||||
if (property.hasModifier(KtTokens.OVERRIDE_KEYWORD)) {
|
||||
val visibility = (property.descriptor as? PropertyDescriptor)
|
||||
?.overriddenDescriptors
|
||||
?.firstOrNull()
|
||||
?.setter
|
||||
?.visibility
|
||||
?.toKeywordToken()
|
||||
if (visibility != null) return visibility
|
||||
}
|
||||
}
|
||||
return when {
|
||||
this is KtConstructor<*> -> {
|
||||
val klass = getContainingClassOrObject()
|
||||
if (klass is KtClass && (klass.isEnum() || klass.isSealed())) KtTokens.PRIVATE_KEYWORD
|
||||
@@ -268,6 +281,7 @@ fun KtDeclaration.implicitVisibility(): KtModifierKeywordToken? =
|
||||
KtTokens.DEFAULT_VISIBILITY_KEYWORD
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun KtModifierListOwner.canBePrivate() = modifierList?.hasModifier(KtTokens.ABSTRACT_KEYWORD) != true
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.RedundantVisibilityModifierInspection
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// PROBLEM: none
|
||||
|
||||
abstract class Foo {
|
||||
abstract var id: Int
|
||||
protected set
|
||||
}
|
||||
|
||||
class Bar : Foo() {
|
||||
override var id: Int = 1
|
||||
<caret>public set
|
||||
}
|
||||
|
||||
fun test() {
|
||||
val bar = Bar()
|
||||
bar.id = 1
|
||||
}
|
||||
+18
@@ -4595,6 +4595,24 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/redundantVisibilityModifier")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RedundantVisibilityModifier extends AbstractLocalInspectionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInRedundantVisibilityModifier() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantVisibilityModifier"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("overridePropertySetter.kt")
|
||||
public void testOverridePropertySetter() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantVisibilityModifier/overridePropertySetter.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/redundantWith")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user