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

Fix const_item_mutation warnings #217

Closed
webmaster128 opened this issue Jan 9, 2021 · 0 comments · Fixed by #218
Closed

Fix const_item_mutation warnings #217

webmaster128 opened this issue Jan 9, 2021 · 0 comments · Fixed by #218

Comments

@webmaster128
Copy link
Member

Starting with Rust 1.48.0 there is a new const_item_mutation warning, which is triggered many times in this repo. I think the warning is right, and calling any mut metod of a constant like TOTAL is dangerous. I did not look into this in detail, but if global state is required, const need to change to static. However, maybe update should not take a mutable self reference.

warning: taking a mutable reference to a `const` item
   --> contracts/cw4-stake/src/contract.rs:184:5
    |
184 | /     TOTAL.update(storage, |total| -> StdResult<_> {
185 | |         Ok(total + new.unwrap_or_default() - old.unwrap_or_default())
186 | |     })?;
    | |______^
    |
    = note: `#[warn(const_item_mutation)]` on by default
    = note: each usage of a `const` item creates a new temporary
    = note: the mutable reference will refer to this temporary, not the original `const` item
note: mutable reference created due to call to this method
   --> /projects/CosmWasm/cosmwasm-plus/packages/storage-plus/src/item.rs:64:5
    |
64  | /     pub fn update<A, E>(&mut self, store: &mut dyn Storage, action: A) -> Result<T, E>
65  | |     where
66  | |         A: FnOnce(T) -> Result<T, E>,
67  | |         E: From<StdError>,
...   |
72  | |         Ok(output)
73  | |     }
    | |_____^
note: `const` item defined here
   --> contracts/cw4-stake/src/state.rs:24:1
    |
24  | pub const TOTAL: Item<u64> = Item::new(TOTAL_KEY);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: 1 warning emitted
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant