diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index 1885b57ec0d..61cbf15ddd9 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -2741,6 +2741,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerForInnerComponents.kt"); } + @Test + @TestMetadata("RedundantVisibilityModifierCheckerWithExplicitApiMode.kt") + public void testRedundantVisibilityModifierCheckerWithExplicitApiMode() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt"); + } + @Nested @TestMetadata("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/canBeReplacedWithOperatorAssignment") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index 8a365070109..fcddcfa9e36 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -2394,6 +2394,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerForInnerComponents.kt"); } + @TestMetadata("RedundantVisibilityModifierCheckerWithExplicitApiMode.kt") + public void testRedundantVisibilityModifierCheckerWithExplicitApiMode() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt"); + } + @TestMetadata("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/canBeReplacedWithOperatorAssignment") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.fir.txt b/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.fir.txt new file mode 100644 index 00000000000..a6365ac9bc4 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.fir.txt @@ -0,0 +1,190 @@ +FILE: RedundantVisibilityModifierCheckerWithExplicitApiMode.kt + public final fun f1(): R|kotlin/Unit| { + local final class LocalClass : R|kotlin/Any| { + public constructor(): R|LocalClass| { + super() + } + + public final var foo: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| + public set(value: R|kotlin/Int|): R|kotlin/Unit| + + } + + R|/LocalClass.LocalClass|().R|/foo| = Int(1) + } + public final fun f2(): R|kotlin/Unit| { + R|/f1|() + } + public final inline fun h1(): R|kotlin/Unit| { + R|/f1|() + } + public final inline fun h2(): R|kotlin/Unit| { + R|/f1|() + } + public final class C1 : R|kotlin/Any| { + public constructor(string: R|kotlin/String|): R|C1| { + super() + } + + public final val foo: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| + + public final val z: R|kotlin/Any| = object : R|kotlin/Any| { + private constructor(): R|| { + super() + } + + public final fun foo(): R|kotlin/Int| { + ^foo Int(13) + } + + } + + public get(): R|kotlin/Any| + + public final fun x(): R|kotlin/Unit| { + } + + } + public final class C2 : R|kotlin/Any| { + public constructor(string: R|kotlin/String|): R|C2| { + super() + } + + public final val foo: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| + + public final val z: R|kotlin/Any| = object : R|kotlin/Any| { + private constructor(): R|| { + super() + } + + public final fun foo(): R|kotlin/Int| { + ^foo Int(13) + } + + } + + public get(): R|kotlin/Any| + + public final fun x(): R|kotlin/Unit| { + } + + } + public open class D1 : R|kotlin/Any| { + public constructor(): R|D1| { + super() + } + + public open fun willRemainPublic(): R|kotlin/Unit| { + } + + protected open fun willBecomePublic(): R|kotlin/Unit| { + } + + } + public open class D2 : R|kotlin/Any| { + public constructor(): R|D2| { + super() + } + + public open fun willRemainPublic(): R|kotlin/Unit| { + } + + protected open fun willBecomePublic(): R|kotlin/Unit| { + } + + } + public abstract interface I1 : R|kotlin/Any| { + public abstract fun bar(): R|kotlin/Unit| + + } + public abstract interface I2 : R|kotlin/Any| { + public abstract fun bar(): R|kotlin/Unit| + + } + public final var baz1: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| + public set(value: R|kotlin/Int|): R|kotlin/Unit| + public final var baz2: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| + public set(value: R|kotlin/Int|): R|kotlin/Unit| + public final class J1 : R|kotlin/Any| { + public constructor(): R|J1| { + super() + } + + protected final val baz: R|kotlin/Int| = Int(0) + protected get(): R|kotlin/Int| { + ^ this@R|/J1|.F|/J1.baz|.R|kotlin/Int.times|(Int(2)) + } + + public final var baf: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| { + ^ Int(1) + } + public set(value: R|kotlin/Int|): R|kotlin/Unit| { + this@R|/J1|.F|/J1.baf| = R|/value| + } + + public final var buf: R|kotlin/Int| = Int(0) + private get(): R|kotlin/Int| { + ^ Int(42) + } + protected set(value: R|kotlin/Int|): R|kotlin/Unit| { + this@R|/J1|.F|/J1.buf| = R|/value| + } + + public final var bar: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| { + ^ Double(3.1415926535) + } + public set(value: R|kotlin/Int|): R|kotlin/Unit| { + } + + } + public final class J2 : R|kotlin/Any| { + public constructor(): R|J2| { + super() + } + + protected final val baz: R|kotlin/Int| = Int(0) + protected get(): R|kotlin/Int| { + ^ this@R|/J2|.F|/J2.baz|.R|kotlin/Int.times|(Int(2)) + } + + public final var baf: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| { + ^ Int(1) + } + public set(value: R|kotlin/Int|): R|kotlin/Unit| { + this@R|/J2|.F|/J2.baf| = R|/value| + } + + public final var buf: R|kotlin/Int| = Int(0) + private get(): R|kotlin/Int| { + ^ Int(42) + } + protected set(value: R|kotlin/Int|): R|kotlin/Unit| { + this@R|/J2|.F|/J2.buf| = R|/value| + } + + public final var bar: R|kotlin/Int| = Int(0) + public get(): R|kotlin/Int| { + ^ Double(3.1415926535) + } + public set(value: R|kotlin/Int|): R|kotlin/Unit| { + } + + } + private final class Hidden : R|kotlin/Any| { + public constructor(): R|Hidden| { + super() + } + + public final fun f(): R|kotlin/Int| { + ^f Int(5) + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt b/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt new file mode 100644 index 00000000000..fe0fe9d6bc2 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt @@ -0,0 +1,116 @@ +// EXPLICIT_API_MODE: STRICT + +fun f1() { + class LocalClass { + public var foo = 0 + } + LocalClass().foo = 1 +} + +public fun f2() { + f1() +} + +inline fun h1() { + f1() +} + +public inline fun h2() { + f1() +} + +class C1 { + constructor(string: String) { } + + val foo: Int = 0 + + val z: Any = object { + fun foo() = 13 + } + + fun x() { } +} + +public class C2 { + public constructor(string: String) { } + + public val foo: Int = 0 + + public val z = object { + public fun foo() = 13 + } + + public fun x() { } +} + +open class D1 { + open fun willRemainPublic() { + } + + protected open fun willBecomePublic() { + } +} + +public open class D2 { + public open fun willRemainPublic() { + } + + protected open fun willBecomePublic() { + } +} + +interface I1 { + fun bar() +} + +public interface I2 { + public fun bar() +} + +var baz1: Int = 0 + +public var baz2: Int = 0 + +class J1 { + protected val baz: Int = 0 + protected get() = field * 2 + var baf: Int = 0 + get() = 1 + set(value) { + field = value + } + + var buf: Int = 0 + private get() = 42 + protected set(value) { + field = value + } + + var bar: Int = 0 + get() = 3.1415926535 + set(value) {} +} + +public class J2 { + protected val baz: Int = 0 + protected get() = field * 2 + public var baf: Int = 0 + public get() = 1 + public set(value) { + field = value + } + + public var buf: Int = 0 + private get() = 42 + protected set(value) { + field = value + } + + public var bar: Int = 0 + get() = 3.1415926535 + set(value) {} +} + +private class Hidden { + public fun f(): Int = 5 +} diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 005ab0a24b5..9fa693f643c 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -2741,6 +2741,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerForInnerComponents.kt"); } + @Test + @TestMetadata("RedundantVisibilityModifierCheckerWithExplicitApiMode.kt") + public void testRedundantVisibilityModifierCheckerWithExplicitApiMode() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt"); + } + @Nested @TestMetadata("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/canBeReplacedWithOperatorAssignment") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index d66b8094458..97b09115f88 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -2741,6 +2741,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerForInnerComponents.kt"); } + @Test + @TestMetadata("RedundantVisibilityModifierCheckerWithExplicitApiMode.kt") + public void testRedundantVisibilityModifierCheckerWithExplicitApiMode() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/RedundantVisibilityModifierCheckerWithExplicitApiMode.kt"); + } + @Nested @TestMetadata("compiler/fir/analysis-tests/testData/resolve/extendedCheckers/canBeReplacedWithOperatorAssignment") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/RedundantVisibilityModifierSyntaxChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/RedundantVisibilityModifierSyntaxChecker.kt index 487dc5a3088..8a8505e7a67 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/RedundantVisibilityModifierSyntaxChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/RedundantVisibilityModifierSyntaxChecker.kt @@ -7,6 +7,8 @@ package org.jetbrains.kotlin.fir.analysis.checkers.extended import org.jetbrains.kotlin.KtFakeSourceElementKind import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.config.AnalysisFlags +import org.jetbrains.kotlin.config.ExplicitApiMode import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.diagnostics.DiagnosticReporter @@ -121,11 +123,23 @@ object RedundantVisibilityModifierSyntaxChecker : FirDeclarationSyntaxChecker