A public property holds a MutableStateFlow, MutableSharedFlow or MutableLiveData, so any caller can change the state
What it finds
A view model that exposes val state = MutableStateFlow(...) lets every observer write to its state. The usual shape is a private mutable property and a public read-only view, such as val state: StateFlow<T> = _state.asStateFlow().
From the rule's source, exposed_mutable_state.rs.
class ProfileViewModel : ViewModel() { val state = MutableStateFlow(0)tx0 reports: the public state is a MutableStateFlow that any caller can write val events: MutableSharedFlow<String> = MutableSharedFlow()tx0 reports: the public events is a MutableSharedFlow private val _name = MutableLiveData<String>() val name: LiveData<String> = _name fun load() { val local = MutableStateFlow(1) }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:exposed-mutable-state-holder" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/kotlin:exposed-mutable-state-holder] reason="..."tx0 explain tx0/kotlin:exposed-mutable-state-holder shows the same in the terminal. Found a false positive? Report it.