Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/noirc_frontend/src/elaborator/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,9 @@ impl Elaborator<'_> {
for parameter in &func_meta.parameter_idents {
let name = self.interner.definition_name(parameter.id).to_owned();
let warn_if_unused = !(func_meta.trait_impl.is_some() && name == "self");
let warn_if_not_mutated = false;
// The unnecessary-mut check only considers definitions that are actually mutable,
// so this is a no-op for parameters without `mut`.
let warn_if_not_mutated = true;
// We allow shadowing here because there's no outer scope to shadow
// (duplicate parameter names were already checked in `resolve_function_parameters`)
let allow_shadowing = true;
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/tests/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn array_length_overflow_during_monomorphization() {
#[test]
fn constant_index_out_of_bounds() {
let src = r#"
fn main(a: u32, mut c: [u32; 2]) {
fn main(a: u32, c: [u32; 2]) {
if (a == c[0]) {
assert((c[0] == 12));
} else if (a == c[1]) {
Expand Down
89 changes: 89 additions & 0 deletions compiler/noirc_frontend/src/tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@
fn main(mut (a, b): pub (Field, Field)) -> pub Field {
^^^^^^^^^^ Entry point parameter must use a simple identifier pattern
~~~~~~~~~~ Destructuring patterns are not allowed here; bind to a name and destructure inside the body
^ variable does not need to be mutable
^ variable does not need to be mutable
a + b
}
"#;
Expand Down Expand Up @@ -743,3 +745,90 @@
"#;
check_errors(src);
}

#[test]
fn warns_on_unnecessary_mut_function_parameter() {
let src = r#"
fn foo(mut x: Field) -> Field {
^ variable does not need to be mutable
x
}

fn main() {
assert(foo(1) == 1);
}
"#;
check_errors(src);
}

#[test]
fn warns_on_unnecessary_mut_self_parameter() {
let src = r#"
struct Counter {
count: Field,
}

impl Counter {
fn count(mut self) -> Field {
^^^^ variable does not need to be mutable
self.count
}
}

fn main() {
let counter = Counter { count: 1 };
assert(counter.count() == 1);
}
"#;
check_errors(src);
}

#[test]
fn does_not_warn_on_mutated_mut_function_parameter() {
let src = r#"
fn foo(mut x: Field) -> Field {
x = x + 1;
x
}

fn main() {
assert(foo(1) == 2);
}
"#;
assert_no_errors(src);
}

#[test]
fn does_not_warn_on_unnecessary_mut_parameter_with_underscore_name() {
let src = r#"
fn foo(mut _x: Field) -> Field {
1
}

fn main() {
assert(foo(1) == 1);
}
"#;
assert_no_errors(src);
}

#[test]
fn does_not_warn_on_unmutated_mut_reference_self_parameter() {

Check warning on line 816 in compiler/noirc_frontend/src/tests/functions.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unmutated)
let src = r#"
struct Counter {
count: Field,
}

impl Counter {
fn count(&mut self) -> Field {
self.count
}
}

fn main() {
let mut counter = Counter { count: 1 };
assert(counter.count() == 1);
}
"#;
assert_no_errors(src);
}
2 changes: 1 addition & 1 deletion noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl<T, let MaxLen: u32> BoundedVec<T, MaxLen> {
/// let vec: BoundedVec<u32, 4> = BoundedVec::from_parts([1, 2, 3, 0], 3);
/// assert_eq(vec.len(), 3);
/// ```
pub fn from_parts(mut array: [T; MaxLen], len: u32) -> Self {
pub fn from_parts(array: [T; MaxLen], len: u32) -> Self {
assert(len <= MaxLen);
BoundedVec { storage: array, len }
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading