From ee16b02d3754fe5b76548122679476d2cea29465 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Thu, 17 Oct 2024 23:31:27 +0100 Subject: [PATCH] Do not warn on unused self in traits --- compiler/noirc_frontend/src/elaborator/mod.rs | 3 ++- .../noirc_frontend/src/tests/unused_items.rs | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index 5067ac05c44..dafafe421eb 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -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); diff --git a/compiler/noirc_frontend/src/tests/unused_items.rs b/compiler/noirc_frontend/src/tests/unused_items.rs index 51bdf785688..86f77fc397a 100644 --- a/compiler/noirc_frontend/src/tests/unused_items.rs +++ b/compiler/noirc_frontend/src/tests/unused_items.rs @@ -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); +}