Skip to content

Commit

Permalink
Do not warn on unused self in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Oct 17, 2024
1 parent ae87d28 commit ee16b02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ impl<'context> Elaborator<'context> {
// so we need to reintroduce the same IDs into scope here.
for parameter in &func_meta.parameter_idents {
let name = self.interner.definition_name(parameter.id).to_owned();
self.add_existing_variable_to_scope(name, parameter.clone(), true);
let warn_if_unused = !(func_meta.trait_impl.is_some() && name == "self");
self.add_existing_variable_to_scope(name, parameter.clone(), warn_if_unused);
}

self.add_trait_constraints_to_scope(&func_meta);
Expand Down
20 changes: 20 additions & 0 deletions compiler/noirc_frontend/src/tests/unused_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,23 @@ fn no_warning_on_indirect_struct_if_it_has_an_abi_attribute() {
"#;
assert_no_errors(src);
}

#[test]
fn no_warning_on_self_in_trait_impl() {
let src = r#"
struct Bar {}
trait Foo {
fn foo(self, a: u64);
}
impl Foo for Bar {
fn foo(self, _a: u64) {}
}
fn main() {
let _ = Bar {};
}
"#;
assert_no_errors(src);
}

0 comments on commit ee16b02

Please sign in to comment.