Can be primary constructor property: check whether property belongs to the same class #KT-12876 Fixed

(cherry picked from commit eb05a7a)
This commit is contained in:
Mikhail Glukhikh
2016-07-11 17:34:44 +03:00
committed by Mikhail Glukhikh
parent e492d23d58
commit 51ccfc120e
2 changed files with 16 additions and 0 deletions
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
@@ -56,6 +57,8 @@ class CanBePrimaryConstructorPropertyInspection : AbstractKotlinInspection() {
if (nameIdentifier.text != assignedDescriptor.name.asString()) return if (nameIdentifier.text != assignedDescriptor.name.asString()) return
val assignedParameter = DescriptorToSourceUtils.descriptorToDeclaration(assignedDescriptor) as? KtParameter ?: return val assignedParameter = DescriptorToSourceUtils.descriptorToDeclaration(assignedDescriptor) as? KtParameter ?: return
if (property.containingClassOrObject !== assignedParameter.containingClassOrObject) return
holder.registerProblem(holder.manager.createProblemDescriptor( holder.registerProblem(holder.manager.createProblemDescriptor(
nameIdentifier, nameIdentifier,
nameIdentifier, nameIdentifier,
@@ -32,3 +32,16 @@ class Incorrect(val property: String, withGetter: Double, withSetter: Int, diffe
data class Data(name: String) { data class Data(name: String) {
val name = name val name = name
} }
// Case from KT-12876: also should not work, property lives in different class here
interface Foo {
val x : String
}
class Bar(x : String) {
init {
object : Foo {
override val x = x // Property is assigned to parameter, can be declared in ctor
}
}
}