-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add lint rule to prevent using Locale.getDefault() in Composable functions #469
base: main
Are you sure you want to change the base?
Conversation
val property = it.initializer?.text ?: return@filter false | ||
property.contains("Locale.getDefault()") |
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.
Text-based checks like this are fairly brittle, let's resolve the call expression and strictly match the called function instead
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.
Okay I will try it !🚀
@ZacSweers I would greatly appreciate your review 😊 |
|
||
if (!context.evaluator.isMemberInClass(method, JAVA_UTIL_LOCALE)) return | ||
|
||
val parentFunction = node.getParentOfType(UMethod::class.java) ?: return |
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.
This will search all parents, but this may be in a nested lambda block where it's ok. Let's only warn if it is a direct child in a function body. I.e. let's make this test not throw any warnings
@Composable
fun Example(...) {
SideEffect {
Locale.getDefault()
}
}
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.
@ZacSweers I think it’s enough to add logic to determine if Locale.getDefault() exists within a lambda block!
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.
I don't think so. For example, this would be allowed now
@Composable
fun Example(
body: @Composable () -> Unit = { }
) {
body()
}
@Composable
fun Example2() {
Example {
Locale.getDefault()
}
}
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.
thank you for your thorough review. While incorporating the feedback, I encountered some confusion and would like to clarify a few points.
So far, my understanding is as follows:
- If
Locale.getDefault()
is directly called in the function body →Show warning
- If
Locale.getDefault()
is called inside a lambda block →No warning
However, I have the following questions:
-
Isn't
Locale.getDefault()
in Example's body (@composable () -> Unit = { }) also called inside a lambda? If so, shouldn't it be excluded from the warning since it's called within a lambda? -
Or, should a Composable lambda be considered as if it were directly part of the function body?
The test case I understand.
@Composable
fun Example(...) {
val locale = Locale.getDefault() // Warning
SideEffect {
Locale.getDefault() // No Warning
}
}
@Composable
fun Example(
body: @Composable () -> Unit = { }
) {
body()
}
@Composable
fun Example2() {
Example {
Locale.getDefault() // is it correct to show a warning in this case?
}
}
…ion body calls, ignoring nested lambdas.
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.
as a side note - please don't keep re-requesting reviews. I am already reviewing :)
try to resolve #373 !