-
Notifications
You must be signed in to change notification settings - Fork 968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Lint checks for Kotlin consumers #432
Conversation
if ("format" == methodName && evaluator.isMemberInClass(method, "java.lang.String")) { | ||
if ("format" == methodName && | ||
(evaluator.isMemberInClass(method, "java.lang.String") || | ||
evaluator.isMemberInClass(method, "kotlin.text.StringsKt__StringsJVMKt")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cuz String.format is an extension function on kotlin.String
@@ -122,8 +125,9 @@ class WrongTimberUsageDetector : Detector(), UastScanner { | |||
} | |||
if (current.isMethodCall()) { | |||
val psiMethod = (current as UCallExpression).resolve() | |||
if (Pattern.matches(TIMBER_TREE_LOG_METHOD_REGEXP, psiMethod!!.name) | |||
&& context.evaluator.isMemberInClass(psiMethod, "timber.log.Timber") | |||
if (psiMethod != null && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cuz arrayOf
is part of the Kotlin stdlib and not resolvable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an intrinsic that resolves to a regular Java array creation. That being said I don't understand the comment in the first place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that Lint/Psi reference resolution is limited to types for which it can find the source. Since we don't own the source of arrayOf (or any method that might wrap in this case), psiMethod will be null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note quite -- it just needs to be on the classpath (which is why the earlier string format comparison resolves to a method in the special class kotlin.text.StringsKt__StringsJVMKt). Lint normally tries to put the android.jar classes into the classpath (if it's an android project), as well as the Kotlin runtime jars (if the unit test contains Kotlin).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dug into this a little more and it looks like arrayOf is on the classpath:
in kotlin/text/StringsJVM.kt
@kotlin.internal.InlineOnly
public inline fun String.Companion.format(format: String, vararg args: Any?): String = java.lang.String.format(format, *args)
in kotlin/Library.kt
public inline fun <reified @PureReifiable T> arrayOf(vararg elements: T): Array<T>
However, that resolution is returning null, because the KotlinUFunctionCallExpression.resolvedCall.resultingDescriptor
(wat!) is a DeserializedCallableMemberDescriptor
whose containingDeclaration
is neither a LazyJavaPackageFragment
or a DeserializedClassDescriptor
, but rather of type org.jetbrains.kotlin.serialization.deserialization.builtins.BuiltInsPackageFragmentImpl
.
Is this a UAST bug, @tnorbye?
fixSource1 += "$methodName(${throwable.asSourceString()}, ${msg.asSourceString()})" | ||
fixSource2 += "$methodName(${throwable.asSourceString()}, ${msg.asSourceString()})" | ||
fixSource1 += "$methodName(${throwable.sourcePsi?.text}, ${msg.asSourceString()})" | ||
fixSource2 += "$methodName(${throwable.sourcePsi?.text}, ${msg.asSourceString()})" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cuz Exception()
source strings rendered as <init>()
. conversation here: https://groups.google.com/g/lint-dev/c/7FYbR7fSl0I
3b41c5d
to
b913a14
Compare
b04d9ef
to
5c002f3
Compare
b913a14
to
c83e3eb
Compare
Merging for now, can address the psiMethod null check discussion in a follow-up. |
Stacked on #431
cc: @tnorbye
Closes #401. Closes #317.