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

Misc mutable parameter fixes #49

Merged
merged 5 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MutableParametersDetector : ComposableFunctionDetector(), SourceCodeScanne
"""
Using mutable objects as state in Compose will cause your users to see incorrect or stale data in your app.
Mutable objects that are not observable, such as `ArrayList<T>` or a mutable data class, cannot be observed by Compose to trigger recomposition when they change.
See https://slackhq.github.io/compose-lints/rules/#when-should-i-expose-modifier-parameters for more information.
See https://slackhq.github.io/compose-lints/rules/#do-not-use-inherently-mutable-types-as-parameters for more information.
""",
category = Category.PRODUCTIVITY,
priority = Priorities.NORMAL,
Expand All @@ -36,11 +36,11 @@ class MutableParametersDetector : ComposableFunctionDetector(), SourceCodeScanne
override fun visitComposable(context: JavaContext, function: KtFunction) {
function.valueParameters
.filter { it.isTypeMutable }
.forEach {
.forEach { parameter ->
context.report(
ISSUE,
function,
context.getNameLocation(function),
parameter.typeReference,
context.getLocation(parameter.typeReference),
ISSUE.getExplanation(TextFormat.TEXT)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ val KnownMutableCommonTypesRegex =
"Hashtable<.*>\\??",
// Compose
"MutableState<.*>\\??",
"SnapshotStateList<.*>\\??",
// Flow
"MutableStateFlow<.*>\\??",
"MutableSharedFlow<.*>\\??",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,24 @@ class MutableParametersDetectorTest : BaseSlackLintTest() {
"""
src/test.kt:2: Error: Using mutable objects as state in Compose will cause your users to see incorrect or stale data in your app.
Mutable objects that are not observable, such as ArrayList<T> or a mutable data class, cannot be observed by Compose to trigger recomposition when they change.
See https://slackhq.github.io/compose-lints/rules/#when-should-i-expose-modifier-parameters for more information. [ComposeMutableParameters]
See https://slackhq.github.io/compose-lints/rules/#do-not-use-inherently-mutable-types-as-parameters for more information. [ComposeMutableParameters]
fun Something(a: MutableState<String>) {}
~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~
src/test.kt:4: Error: Using mutable objects as state in Compose will cause your users to see incorrect or stale data in your app.
Mutable objects that are not observable, such as ArrayList<T> or a mutable data class, cannot be observed by Compose to trigger recomposition when they change.
See https://slackhq.github.io/compose-lints/rules/#when-should-i-expose-modifier-parameters for more information. [ComposeMutableParameters]
See https://slackhq.github.io/compose-lints/rules/#do-not-use-inherently-mutable-types-as-parameters for more information. [ComposeMutableParameters]
fun Something(a: ArrayList<String>) {}
~~~~~~~~~
~~~~~~~~~~~~~~~~~
src/test.kt:6: Error: Using mutable objects as state in Compose will cause your users to see incorrect or stale data in your app.
Mutable objects that are not observable, such as ArrayList<T> or a mutable data class, cannot be observed by Compose to trigger recomposition when they change.
See https://slackhq.github.io/compose-lints/rules/#when-should-i-expose-modifier-parameters for more information. [ComposeMutableParameters]
See https://slackhq.github.io/compose-lints/rules/#do-not-use-inherently-mutable-types-as-parameters for more information. [ComposeMutableParameters]
fun Something(a: HashSet<String>) {}
~~~~~~~~~
~~~~~~~~~~~~~~~
src/test.kt:8: Error: Using mutable objects as state in Compose will cause your users to see incorrect or stale data in your app.
Mutable objects that are not observable, such as ArrayList<T> or a mutable data class, cannot be observed by Compose to trigger recomposition when they change.
See https://slackhq.github.io/compose-lints/rules/#when-should-i-expose-modifier-parameters for more information. [ComposeMutableParameters]
See https://slackhq.github.io/compose-lints/rules/#do-not-use-inherently-mutable-types-as-parameters for more information. [ComposeMutableParameters]
fun Something(a: MutableMap<String, String>) {}
~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~
4 errors, 0 warnings
"""
.trimIndent()
Expand Down