Skip to content

fix: warn on never-mutated mut function parameters - #13391

Merged
asterite merged 1 commit into
masterfrom
tf/warn-unnecessary-mut-parameters
Jul 22, 2026
Merged

fix: warn on never-mutated mut function parameters#13391
asterite merged 1 commit into
masterfrom
tf/warn-unnecessary-mut-parameters

Conversation

@TomAFrench

Copy link
Copy Markdown
Member

Description

Problem

VariableDoesNotNeedToBeMutable never fired for function parameters: fn foo(mut x: Field) with x never mutated produced no warning, even though the same binding as a let or a lambda parameter would warn.

Summary

The cause was a one-line inconsistency: define_function_meta elaborates parameter patterns with warn_if_not_mutated: true, but when elaborate_function reintroduces the stored parameter idents into the body's scope, the flag was hard-coded to false — so the body-scope check that produces the warning never considered parameters. This PR passes the flag through.

Safety of the change:

  • The unnecessary-mut check only considers definitions that are actually mutable, so parameters without mut are unaffected.
  • Method calls that auto-take &mut self mark the receiver as mutated (check_can_mutate), so mut parameters that exist to enable such calls do not warn — verified against trait_method_mut_self and the generic impl SomeTrait patterns.
  • _-prefixed names are skipped, same as for let bindings.
  • mut self warns (like Rust's unused_mut); &mut self does not (the binding itself is immutable).

Fallout the new warning surfaced

  • A genuine stdlib leftover: BoundedVec::from_parts kept mut on its array parameter from a since-removed zeroing loop (the deprecation note on from_parts_unchecked records that removal). The mut is now dropped.
  • Two frontend test fixtures had incidental never-mutated mut params; one dropped the mut, the other (rejects_mutable_tuple_pattern_in_main_param) now annotates the two new warnings.
  • Three fuzzer-derived execution_failure programs (regression_8231, dyn_index_fail_nested_array, brillig_mem_layout_regression) keep their unnecessary muts — removing them would change SSA allocations, which is what those repros exercise — so their stderr snapshots now include the warnings.

Interaction with #13390

nargo check --fix (#13390) removes unnecessary muts and its pattern-visitor already handles parameters; that branch carries a characterization test asserting parameters produce no warning. Whichever PR lands second should flip that test to assert the mut is removed (the test's comment says exactly this).

Test coverage

Five new frontend tests: warns for a never-mutated mut parameter and for mut self; no warning when the parameter is mutated, _-prefixed, or &mut self. Full suites pass: noirc_frontend (2218), noir_lsp (350), stdlib tests, and the entire nargo_cli execute suite (9114).

`define_function_meta` elaborates parameter patterns with
`warn_if_not_mutated: true`, but when the stored parameter idents were
reintroduced into the body's scope the flag was hard-coded to `false`,
so `VariableDoesNotNeedToBeMutable` never fired for parameters — unlike
`let` bindings and lambda parameters, which already warn. Passing the
flag through closes the gap; the unnecessary-mut check only considers
definitions that are actually mutable, so parameters without `mut` are
unaffected, and method calls that auto-take `&mut self` already mark
the receiver as mutated, so `mut` parameters used that way don't warn.

The new warning caught a genuine leftover in the stdlib:
`BoundedVec::from_parts` kept a `mut` on its array parameter from a
since-removed zeroing loop. Three fuzzer-derived `execution_failure`
programs keep their unnecessary `mut`s (removing them would change SSA
allocations, which those repros exercise), so their stderr snapshots
now include the warnings.
@TomAFrench
TomAFrench requested a review from asterite July 22, 2026 16:26
@asterite
asterite added this pull request to the merge queue Jul 22, 2026
Merged via the queue into master with commit 072e80e Jul 22, 2026
138 checks passed
@asterite
asterite deleted the tf/warn-unnecessary-mut-parameters branch July 22, 2026 21:30
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 this pull request may close these issues.

2 participants