Skip to content

Commit

Permalink
fix: Stop issuing unused variable warnings for variables in trait def…
Browse files Browse the repository at this point in the history
…initions (#3797)

# Description

## Problem\*

Previously we'd issue a warning for each trait function parameter that
it is unused. This seemed to only trigger when the trait was in another
crate.

## Summary\*

Stop checking for unused variables in trait function declarations since
there is no body to use the variable in anyway.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
jfecher and kevaundray committed Dec 13, 2023
1 parent 14f2fff commit 0bb44c3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
5 changes: 2 additions & 3 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ impl<'a> Resolver<'a> {
};

let (hir_func, func_meta) = self.intern_function(NoirFunction { kind, def }, func_id);
let func_scope_tree = self.scopes.end_function();
self.check_for_unused_variables_in_scope_tree(func_scope_tree);

let _ = self.scopes.end_function();
// Don't check the scope tree for unused variables, they can't be used in a declaration anyway.
self.trait_bounds.clear();
(hir_func, func_meta)
}
Expand Down
17 changes: 3 additions & 14 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ mod test {
});
}

/// Many of the tests in this file have odd unused variable warnings which do not occur
/// when running an identical program using `nargo execute`. They're filtered out of the
/// errors returned by `get_errors` for now.
pub(crate) fn remove_unused_variable_warnings(errors: &mut Vec<(CompilationError, FileId)>) {
errors.retain(|(error, _)| {
!matches!(error, CompilationError::ResolverError(ResolverError::UnusedVariable { .. }))
});
}

pub(crate) fn get_program(
src: &str,
) -> (ParsedModule, Context, Vec<(CompilationError, FileId)>) {
Expand Down Expand Up @@ -97,9 +88,7 @@ mod test {
}

pub(crate) fn get_program_errors(src: &str) -> Vec<(CompilationError, FileId)> {
let (_program, _context, mut errors) = get_program(src);
remove_unused_variable_warnings(&mut errors);
errors
get_program(src).2
}

#[test]
Expand Down Expand Up @@ -797,7 +786,7 @@ mod test {
}
"#;

let (_, _, errors) = get_program(src);
let errors = get_program_errors(src);
assert!(errors.len() == 1, "Expected 1 error, got: {:?}", errors);
// It should be regarding the unused variable
match &errors[0].0 {
Expand Down Expand Up @@ -874,7 +863,7 @@ mod test {
}
"#;

let (_, _, errors) = get_program(src);
let errors = get_program_errors(src);
assert!(errors.len() == 3, "Expected 3 errors, got: {:?}", errors);

// Errors are:
Expand Down

0 comments on commit 0bb44c3

Please sign in to comment.