Skip to content

Commit be1789a

Browse files
authored
Rollup merge of #107648 - matthiaskrgr:unused_lifetime_104432_fix, r=cjgillot
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
2 parents 8cca42a + a363703 commit be1789a

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -2244,19 +2244,23 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
22442244
}
22452245
None => {
22462246
debug!(?param.ident, ?param.ident.span);
2247-
22482247
let deletion_span = deletion_span();
2249-
self.r.lint_buffer.buffer_lint_with_diagnostic(
2250-
lint::builtin::UNUSED_LIFETIMES,
2251-
param.id,
2252-
param.ident.span,
2253-
&format!("lifetime parameter `{}` never used", param.ident),
2254-
lint::BuiltinLintDiagnostics::SingleUseLifetime {
2255-
param_span: param.ident.span,
2256-
use_span: None,
2257-
deletion_span,
2258-
},
2259-
);
2248+
// the give lifetime originates from expanded code so we won't be able to remove it #104432
2249+
let lifetime_only_in_expanded_code =
2250+
deletion_span.map(|sp| sp.in_derive_expansion()).unwrap_or(true);
2251+
if !lifetime_only_in_expanded_code {
2252+
self.r.lint_buffer.buffer_lint_with_diagnostic(
2253+
lint::builtin::UNUSED_LIFETIMES,
2254+
param.id,
2255+
param.ident.span,
2256+
&format!("lifetime parameter `{}` never used", param.ident),
2257+
lint::BuiltinLintDiagnostics::SingleUseLifetime {
2258+
param_span: param.ident.span,
2259+
use_span: None,
2260+
deletion_span,
2261+
},
2262+
);
2263+
}
22602264
}
22612265
}
22622266
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
3+
#![deny(unused_lifetimes)]
4+
trait Trait2 {
5+
type As;
6+
}
7+
8+
// we should not warn about an unused lifetime about code generated from this proc macro here
9+
#[derive(Clone)]
10+
struct ShimMethod4<T: Trait2 + 'static>(pub &'static dyn for<'s> Fn(&'s mut T::As));
11+
12+
pub fn main() {}

0 commit comments

Comments
 (0)