You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/detekt.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -110,7 +110,7 @@ Compose:
110
110
RememberContentMissing:
111
111
active: true
112
112
UnstableCollections:
113
-
active: true
113
+
active: false # Opt-in, disabled by default. Turn on if you want to enforce this (e.g. you have strong skipping disabled)
114
114
ViewModelForwarding:
115
115
active: true
116
116
# -- You can optionally use this rule on things other than types ending in "ViewModel" or "Presenter" (which are the defaults). You can add your own via a regex here:
The `unstable-collections` rule will flag any usage of any unstable collection (e.g. List/Set/Map). This rule is disabled by default, so you'll need to explicitly enable it in your `.editorconfig` file:
177
+
178
+
```editorconfig
179
+
[*.{kt,kts}]
180
+
compose_disallow_unstable_collections = true
181
+
```
182
+
174
183
## Disabling a specific rule
175
184
176
185
To disable a rule you have to follow the [instructions from the ktlint documentation](https://pinterest.github.io/ktlint/0.49.1/faq/#how-do-i-suppress-errors-for-a-lineblockfile), and use the id of the rule you want to disable with the `compose` tag.
@@ -179,5 +188,5 @@ For example, to disable the `naming-check` rule, the tag you'll need to disable
Copy file name to clipboardExpand all lines: docs/rules.md
+37-35Lines changed: 37 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -30,41 +30,6 @@ More info: [Immutable docs](https://developer.android.com/reference/kotlin/andro
30
30
31
31
Related rule: TBD
32
32
33
-
### Avoid using unstable collections
34
-
35
-
Collections are defined as interfaces (e.g. `List<T>`, `Map<T>`, `Set<T>`) in Kotlin, which can't guarantee that they are actually immutable. For example, you could write:
36
-
37
-
```kotlin
38
-
// ❌ The compiler won't be able to infer that the list is immutable
39
-
val list:List<String> = mutableListOf<String>()
40
-
```
41
-
42
-
The variable is constant, its declared type is not mutable but its implementation is still mutable. The Compose compiler cannot be sure of the immutability of this class as it just sees the declared type and as such declares it as unstable.
43
-
44
-
To force the compiler to see a collection as truly 'immutable' you have a couple of options.
45
-
46
-
You can use [Kotlinx Immutable Collections](https://github.com/Kotlin/kotlinx.collections.immutable):
47
-
48
-
```kotlin
49
-
// ✅ The compiler knows that this list is immutable
50
-
val list:ImmutableList<String> = persistentListOf<String>()
51
-
```
52
-
53
-
Alternatively, you can wrap your collection in an annotated stable class to mark it as immutable for the Compose compiler.
54
-
55
-
```kotlin
56
-
// ✅ The compiler knows that this class is immutable
57
-
@Immutable
58
-
data classStringList(valitems:List<String>)
59
-
// ...
60
-
val list:StringList=StringList(yourList)
61
-
```
62
-
> **Note**: It is preferred to use Kotlinx Immutable Collections for this. As you can see, the wrapped case only includes the immutability promise with the annotation, but the underlying List is still mutable.
63
-
64
-
More info: [Jetpack Compose Stability Explained](https://medium.com/androiddevelopers/jetpack-compose-stability-explained-79c10db270c8), [Kotlinx Immutable Collections](https://github.com/Kotlin/kotlinx.collections.immutable)
65
-
66
-
Related rule: [compose:unstable-collections](https://github.com/mrmans0n/compose-rules/blob/main/rules/common/src/main/kotlin/io/nlopez/compose/rules/UnstableCollections.kt)
67
-
68
33
### Use mutableStateOf type-specific variants when possible
69
34
70
35
`mutableIntStateOf`, `mutableLongStateOf`, `mutableDoubleStateOf`, `mutableFloatStateOf` are essentially counterparts to `mutableStateOf`, but with the added advantage of circumventing autoboxing on JVM platforms. This distinction renders them more memory efficient, making them the preferable choice when dealing with primitive types such as double, float, int, and long.
More info: [Migration to Material 3](https://developer.android.com/develop/ui/compose/designsystems/material2-material3)
519
484
520
485
Related rule: [compose:material-two](https://github.com/mrmans0n/compose-rules/blob/main/rules/common/src/main/kotlin/io/nlopez/compose/rules/Material2.kt)
486
+
487
+
### Avoid using unstable collections
488
+
489
+
> **Note**: This rule will become unnecessary from the Compose version where strong skipping is enabled by default.
490
+
491
+
Collections are defined as interfaces (e.g. `List<T>`, `Map<T>`, `Set<T>`) in Kotlin, which can't guarantee that they are actually immutable. For example, you could write:
492
+
493
+
```kotlin
494
+
// ❌ The compiler won't be able to infer that the list is immutable
495
+
val list:List<String> = mutableListOf<String>()
496
+
```
497
+
498
+
The variable is constant, its declared type is not mutable but its implementation is still mutable. The Compose compiler cannot be sure of the immutability of this class as it just sees the declared type and as such declares it as unstable.
499
+
500
+
To force the compiler to see a collection as truly 'immutable' you have a couple of options.
501
+
502
+
You can use [Kotlinx Immutable Collections](https://github.com/Kotlin/kotlinx.collections.immutable):
503
+
504
+
```kotlin
505
+
// ✅ The compiler knows that this list is immutable
506
+
val list:ImmutableList<String> = persistentListOf<String>()
507
+
```
508
+
509
+
Alternatively, you can wrap your collection in an annotated stable class to mark it as immutable for the Compose compiler.
510
+
511
+
```kotlin
512
+
// ✅ The compiler knows that this class is immutable
513
+
@Immutable
514
+
data classStringList(valitems:List<String>)
515
+
// ...
516
+
val list:StringList=StringList(yourList)
517
+
```
518
+
> **Note**: It is preferred to use Kotlinx Immutable Collections for this. As you can see, the wrapped case only includes the immutability promise with the annotation, but the underlying List is still mutable.
519
+
520
+
More info: [Jetpack Compose Stability Explained](https://medium.com/androiddevelopers/jetpack-compose-stability-explained-79c10db270c8), [Kotlinx Immutable Collections](https://github.com/Kotlin/kotlinx.collections.immutable)
521
+
522
+
Related rule: [compose:unstable-collections](https://github.com/mrmans0n/compose-rules/blob/main/rules/common/src/main/kotlin/io/nlopez/compose/rules/UnstableCollections.kt)
0 commit comments