1
Fork 0

unused-lifetimes: don't warn about lifetimes originating from expanded 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
This commit is contained in:
Matthias Krüger 2023-02-03 20:48:25 +01:00
parent 9545094994
commit a3637032db
2 changed files with 28 additions and 12 deletions

View file

@ -2244,8 +2244,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
} }
None => { None => {
debug!(?param.ident, ?param.ident.span); debug!(?param.ident, ?param.ident.span);
let deletion_span = deletion_span(); let deletion_span = 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( self.r.lint_buffer.buffer_lint_with_diagnostic(
lint::builtin::UNUSED_LIFETIMES, lint::builtin::UNUSED_LIFETIMES,
param.id, param.id,
@ -2261,6 +2264,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
} }
} }
} }
}
pub(crate) fn emit_undeclared_lifetime_error( pub(crate) fn emit_undeclared_lifetime_error(
&self, &self,

View file

@ -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() {}