fix: warn on never-mutated mut function parameters - #13391
Merged
Conversation
`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.
asterite
approved these changes
Jul 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Problem
VariableDoesNotNeedToBeMutablenever fired for function parameters:fn foo(mut x: Field)withxnever mutated produced no warning, even though the same binding as aletor a lambda parameter would warn.Summary
The cause was a one-line inconsistency:
define_function_metaelaborates parameter patterns withwarn_if_not_mutated: true, but whenelaborate_functionreintroduces the stored parameter idents into the body's scope, the flag was hard-coded tofalse— so the body-scope check that produces the warning never considered parameters. This PR passes the flag through.Safety of the change:
mutare unaffected.&mut selfmark the receiver as mutated (check_can_mutate), somutparameters that exist to enable such calls do not warn — verified againsttrait_method_mut_selfand the genericimpl SomeTraitpatterns._-prefixed names are skipped, same as forletbindings.mut selfwarns (like Rust'sunused_mut);&mut selfdoes not (the binding itself is immutable).Fallout the new warning surfaced
BoundedVec::from_partskeptmuton its array parameter from a since-removed zeroing loop (the deprecation note onfrom_parts_uncheckedrecords that removal). Themutis now dropped.mutparams; one dropped themut, the other (rejects_mutable_tuple_pattern_in_main_param) now annotates the two new warnings.execution_failureprograms (regression_8231,dyn_index_fail_nested_array,brillig_mem_layout_regression) keep their unnecessarymuts — 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 unnecessarymuts 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 themutis removed (the test's comment says exactly this).Test coverage
Five new frontend tests: warns for a never-mutated
mutparameter and formut 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 entirenargo_cliexecute suite (9114).