Warning: variable does not need to be mutable, but i needs to be

Viewed 55

The compiler is returning unused mutable warning. Warning is returned on deposit_amount and sender variables.

fn try_deposit(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    mut config: &mut ConfigInfo,
    priority: u8,
    from: Option<String>,
    amount: Option<Uint128>,
) -> StdResult<()> {
    //CHECKING: if the contract is in the correct status to perform this handle function.
    check_status(config.status, priority)?;

    let mut deposit_amount: Uint128 = Uint128::zero(); // A thin wrapper around u128 that is using strings for JSON encoding/decoding. More here:https://github.com/scrtlabs/cosmwasm/blob/secret/packages/std/src/math/uint128.rs
    let mut sender: Addr; // A human-readable address. More here https://github.com/scrtlabs/cosmwasm/blob/secret/packages/std/src/addresses.rs


            
    if from.as_ref().is_some() && amount.as_ref().is_some() {
                deposit_amount = check_if_valid_amount(&info, &config, true, amount)?;
                sender = deps.api.addr_validate(from.as_ref().unwrap().as_str())?;
            } else {
                //CHECKING: Amount of deposit > minimum deposit amount
                deposit_amount = check_if_valid_amount(&info, &config, false, None)?;
                sender = info.sender;
                }

//Using deposit_amount and sender here

Ok(())
}

check_if_validate_amount function

fn check_if_valid_amount(

) -> StdResult<Uint128> {
    let mut deposit_amount = Uint128::zero();

   //Extracting deposit amount 
    deposit_amount = extracted_amount
    return Ok(deposit_amount);
}
1 Answers

If you declare a variable with let variable; or let variable: Type;, and then assign to it just once, that doesn't count as mutation, but as delayed initialization. That's why the compiler tells you that the variable doesn't need to be mut.

To fix it, you just need to remove the unneeded mut. In your case this applies to both sender and deposit_amount (once you remove the unneeded initialization from the latter).

Related