Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

missing_mut_constraint

What it does

Detects when an account is mutated in the instruction body but not declared with #[account(mut)] in the Anchor accounts struct.

Why is this bad?

Mutating an account without the mut constraint can cause the runtime to reject the transaction or behave unexpectedly, because the account was not marked as writable.

Example

Bad: account mutated without #[account(mut)]

#[derive(Accounts)]
pub struct Update<'info> {
    pub vault: Account<'info, Vault>,  // missing #[account(mut)]
}

pub fn update(ctx: Context<Update>) -> Result<()> {
    ctx.accounts.vault.amount += 1;  // mutation
    Ok(())
}

Good: account has #[account(mut)] when mutated

#[derive(Accounts)]
pub struct Update<'info> {
    #[account(mut)]
    pub vault: Account<'info, Vault>,
}

pub fn update(ctx: Context<Update>) -> Result<()> {
    ctx.accounts.vault.amount += 1;
    Ok(())
}