Rules › Anchor (Rust)
Unchecked arithmetic on amounts
+, - and * on balances or amounts can overflow; prefer checked_add/checked_sub and return an error.
Why it matters
Even with overflow checks on, a panic is a poor failure mode, and with them off the math wraps. Using checked_add, checked_sub and checked_mul makes every amount calculation either succeed or return a clear error, and documents that the edge case was considered.
Vulnerable
vault.balance -= amount;
Fixed
vault.balance = vault.balance.checked_sub(amount).ok_or(VaultError::InsufficientFunds)?;
What other tools call it
| Tool | Detector or reference |
|---|---|
| Clippy | arithmetic_side_effects |
Check your code
Paste a contract into the in-browser checker, or let the Claude Code plugin scan before every deploy. The quick check for this rule is a pattern match: treat a hit as "look here", and a miss as no guarantee.
Related rules
- Unchecked account without a /// CHECK: note
- Authority account that isn't a Signer
- CPI to a program the caller chooses
- Account data deserialized without an owner check
- init_if_needed (reinitialization risk)
- Account closed by draining lamports by hand
Static analysis finds known bug classes; it can't prove code is safe and isn't an audit. Get a professional audit before deploying code that will hold value.