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

Lint check for accessing stateSnapshot in MutateState #134

Open
sockeqwe opened this issue Jun 19, 2021 · 1 comment
Open

Lint check for accessing stateSnapshot in MutateState #134

sockeqwe opened this issue Jun 19, 2021 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@sockeqwe
Copy link
Collaborator

sockeqwe commented Jun 19, 2021

Accessing the stateSnapshot parameter inside a MutateState block should be forbidden by a lint Rule.

fun onActionXDoFoo(action: ActionX, stateSnapshot: State) {
   ....
   return MutateState {
       stateSnapshot.copy( ... ) // Accessing must be forbidden because lambda parameter state must be used instead
   }
}
@sockeqwe sockeqwe added the enhancement New feature or request label Jun 19, 2021
@gabrielittner
Copy link
Member

gabrielittner commented Jun 25, 2022

This might be a bit easier now that the snapshot is a property on State since you can search for usages of that specifically instead of an arbitrary parameter. I think one other issue that this check should cover is that the object returned in the lambda is created inside the lambda, which will also reduce the risk of creating it based on an outdated snapshot.

fun onActionXDoFoo(action: ActionX, stateSnapshot: State) {
   ....
   val newState =  stateSnapshot.copy( ... )
   return MutateState { 
       newState // Returning newState must be forbidden because it was created outside the lambda where we can't check whether the snapshot was used to create it
   }
}

In summary:

  • state.snapshot should never be accessed inside the lambda
  • the returned object has to be created inside the lambda

@gabrielittner gabrielittner added this to the 1.3 milestone Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants