Skip to content

Commit

Permalink
unused-lifetimes: don't warn about lifetimes originating from expande…
Browse files Browse the repository at this point in the history
…d code

previously, we would warn like this:

````
warning: lifetime parameter `'s` never used
 --> /tmp/unusedlif/code.rs:6:62
  |
5 | #[derive(Clone)]
  |          - help: elide the unused lifetime
6 | struct ShimMethod4<T: Trait2 + 'static>(pub &'static dyn for<'s> Fn(&'s mut T::As));
  |                                                              ^^
  |
  = note: requested on the command line with `-W unused-lifetimes`
````

Fixes #104432
  • Loading branch information
matthiaskrgr committed Feb 3, 2023
1 parent 9545094 commit a363703
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
28 changes: 16 additions & 12 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2244,19 +2244,23 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}
None => {
debug!(?param.ident, ?param.ident.span);

let deletion_span = deletion_span();
self.r.lint_buffer.buffer_lint_with_diagnostic(
lint::builtin::UNUSED_LIFETIMES,
param.id,
param.ident.span,
&format!("lifetime parameter `{}` never used", param.ident),
lint::BuiltinLintDiagnostics::SingleUseLifetime {
param_span: param.ident.span,
use_span: None,
deletion_span,
},
);
// the give lifetime originates from expanded code so we won't be able to remove it #104432
let lifetime_only_in_expanded_code =
deletion_span.map(|sp| sp.in_derive_expansion()).unwrap_or(true);
if !lifetime_only_in_expanded_code {
self.r.lint_buffer.buffer_lint_with_diagnostic(
lint::builtin::UNUSED_LIFETIMES,
param.id,
param.ident.span,
&format!("lifetime parameter `{}` never used", param.ident),
lint::BuiltinLintDiagnostics::SingleUseLifetime {
param_span: param.ident.span,
use_span: None,
deletion_span,
},
);
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/lifetimes/issue-104432-unused-lifetimes-in-expansion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// check-pass

#![deny(unused_lifetimes)]
trait Trait2 {
type As;
}

// we should not warn about an unused lifetime about code generated from this proc macro here
#[derive(Clone)]
struct ShimMethod4<T: Trait2 + 'static>(pub &'static dyn for<'s> Fn(&'s mut T::As));

pub fn main() {}

0 comments on commit a363703

Please sign in to comment.