Minor: cover negative cases with test +m
Add case without reported diagnostic on public companion. Only report diagnostic if @JvmStatic annotation has source in code. KT-25114
This commit is contained in:
+6
-4
@@ -18,14 +18,16 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
|||||||
|
|
||||||
class JvmStaticInPrivateCompanionChecker : DeclarationChecker {
|
class JvmStaticInPrivateCompanionChecker : DeclarationChecker {
|
||||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||||
|
val containingDeclaration = descriptor.containingDeclaration
|
||||||
|
|
||||||
descriptor.containingDeclaration.safeAs<ClassDescriptor>()?.takeIf {
|
if (containingDeclaration !is ClassDescriptor
|
||||||
it.isCompanionObject && Visibilities.isPrivate(it.visibility.delegate)
|
|| !containingDeclaration.isCompanionObject
|
||||||
} ?: return
|
|| !Visibilities.isPrivate(containingDeclaration.visibility.delegate)
|
||||||
|
) return
|
||||||
|
|
||||||
val jvmStaticAnnotation = descriptor.annotations.findAnnotation(JVM_STATIC_ANNOTATION_FQ_NAME) ?: return
|
val jvmStaticAnnotation = descriptor.annotations.findAnnotation(JVM_STATIC_ANNOTATION_FQ_NAME) ?: return
|
||||||
|
|
||||||
val reportTarget = jvmStaticAnnotation.source.safeAs<KotlinSourceElement>()?.psi ?: declaration
|
val reportTarget = jvmStaticAnnotation.source.safeAs<KotlinSourceElement>()?.psi ?: return
|
||||||
context.trace.report(JVM_STATIC_IN_PRIVATE_COMPANION.on(reportTarget))
|
context.trace.report(JVM_STATIC_IN_PRIVATE_COMPANION.on(reportTarget))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Vendored
+27
@@ -27,3 +27,30 @@ class WithPrivateCompanion {
|
|||||||
fun staticFunction() {}
|
fun staticFunction() {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class WithPublicCompanion {
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
val staticVal1: Int = 42
|
||||||
|
|
||||||
|
val staticVal2: Int
|
||||||
|
@JvmStatic get() = 42
|
||||||
|
|
||||||
|
@get:JvmStatic
|
||||||
|
val staticVal3: Int = 42
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
var staticVar1: Int = 42
|
||||||
|
|
||||||
|
var staticVar2: Int
|
||||||
|
@JvmStatic get() = 42
|
||||||
|
@JvmStatic set(value) {}
|
||||||
|
|
||||||
|
@get: JvmStatic
|
||||||
|
@set: JvmStatic
|
||||||
|
var staticVar3: Int = 42
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun staticFunction() {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Vendored
+27
@@ -27,3 +27,30 @@ class WithPrivateCompanion {
|
|||||||
fun staticFunction() {}
|
fun staticFunction() {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class WithPublicCompanion {
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
val staticVal1: Int = 42
|
||||||
|
|
||||||
|
val staticVal2: Int
|
||||||
|
@JvmStatic get() = 42
|
||||||
|
|
||||||
|
@get:JvmStatic
|
||||||
|
val staticVal3: Int = 42
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
var staticVar1: Int = 42
|
||||||
|
|
||||||
|
var staticVar2: Int
|
||||||
|
@JvmStatic get() = 42
|
||||||
|
@JvmStatic set(value) {}
|
||||||
|
|
||||||
|
@get: JvmStatic
|
||||||
|
@set: JvmStatic
|
||||||
|
var staticVar3: Int = 42
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun staticFunction() {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Vendored
+21
@@ -20,3 +20,24 @@ public final class WithPrivateCompanion {
|
|||||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final class WithPublicCompanion {
|
||||||
|
public constructor WithPublicCompanion()
|
||||||
|
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||||
|
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||||
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
|
|
||||||
|
public companion object Companion {
|
||||||
|
private constructor Companion()
|
||||||
|
@kotlin.jvm.JvmStatic public final val staticVal1: kotlin.Int = 42
|
||||||
|
@get:kotlin.jvm.JvmStatic public final val staticVal2: kotlin.Int
|
||||||
|
@get:kotlin.jvm.JvmStatic public final val staticVal3: kotlin.Int = 42
|
||||||
|
@kotlin.jvm.JvmStatic public final var staticVar1: kotlin.Int
|
||||||
|
@get:kotlin.jvm.JvmStatic @set:kotlin.jvm.JvmStatic public final var staticVar2: kotlin.Int
|
||||||
|
@get:kotlin.jvm.JvmStatic @set:kotlin.jvm.JvmStatic public final var staticVar3: kotlin.Int
|
||||||
|
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||||
|
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||||
|
@kotlin.jvm.JvmStatic public final fun staticFunction(): kotlin.Unit
|
||||||
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user