Skip to content

Fix const_item_mutation warnings #217

Closed
@webmaster128

Description

@webmaster128

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions