Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

henni99
Copy link

@henni99 henni99 commented Mar 7, 2025

try to resolve #373 !

Comment on lines 39 to 40
val property = it.initializer?.text ?: return@filter false
property.contains("Locale.getDefault()")
Copy link
Collaborator

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

Copy link
Author

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 !🚀

@henni99 henni99 requested a review from ZacSweers March 8, 2025 07:24
@henni99
Copy link
Author

henni99 commented Mar 11, 2025

@ZacSweers I would greatly appreciate your review 😊


if (!context.evaluator.isMemberInClass(method, JAVA_UTIL_LOCALE)) return

val parentFunction = node.getParentOfType(UMethod::class.java) ?: return
Copy link
Collaborator

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()
  }
}

Copy link
Author

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!

Copy link
Collaborator

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()
  }
}

Copy link
Author

@henni99 henni99 Mar 13, 2025

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:

  1. 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?

  2. 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.
@henni99 henni99 requested a review from ZacSweers March 12, 2025 16:30
Copy link
Collaborator

@ZacSweers ZacSweers left a 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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposed rule: Don't use Locale.getDefault() in a @Composable context
2 participants