Skip to content

Commit c1f51b6

Browse files
authored
Rollup merge of rust-lang#64603 - gilescope:unused-lifetime-warning, r=matthewjasper
Reducing spurious unused lifetime warnings. Fixes rust-lang#61115, fixes rust-lang#64493.
2 parents 237d54f + d82c1c5 commit c1f51b6

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/librustc/middle/resolve_lifetime.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -708,15 +708,22 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
708708
match param.kind {
709709
GenericParamKind::Lifetime { .. } => {
710710
let (name, reg) = Region::early(&self.tcx.hir(), &mut index, &param);
711+
let def_id = if let Region::EarlyBound(_ ,def_id , _) = reg {
712+
def_id
713+
} else {
714+
bug!();
715+
};
711716
if let hir::ParamName::Plain(param_name) = name {
712717
if param_name.name == kw::UnderscoreLifetime {
713718
// Pick the elided lifetime "definition" if one exists
714719
// and use it to make an elision scope.
720+
self.lifetime_uses.insert(def_id.clone(), LifetimeUseSet::Many);
715721
elision = Some(reg);
716722
} else {
717723
lifetimes.insert(name, reg);
718724
}
719725
} else {
726+
self.lifetime_uses.insert(def_id.clone(), LifetimeUseSet::Many);
720727
lifetimes.insert(name, reg);
721728
}
722729
}
@@ -1615,7 +1622,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
16151622
_ => None,
16161623
} {
16171624
debug!("id = {:?} span = {:?} name = {:?}", id, span, name);
1618-
16191625
if name.name == kw::UnderscoreLifetime {
16201626
continue;
16211627
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// edition:2018
2+
3+
// Avoid spurious warnings of unused lifetime. The below async functions
4+
// are desugered to have an unused lifetime
5+
// but we don't want to warn about that as there's nothing they can do about it.
6+
7+
#![deny(unused_lifetimes)]
8+
#![allow(dead_code)]
9+
10+
pub async fn october(s: &str) {
11+
println!("{}", s);
12+
}
13+
14+
pub async fn async_fn(&mut ref s: &mut[i32]) {
15+
println!("{:?}", s);
16+
}
17+
18+
macro_rules! foo_macro {
19+
() => {
20+
pub async fn async_fn_in_macro(&mut ref _s: &mut[i32]) {}
21+
};
22+
}
23+
24+
foo_macro!();
25+
26+
pub async fn func_with_unused_lifetime<'a>(s: &'a str) {
27+
//~^ ERROR lifetime parameter `'a` never used
28+
println!("{}", s);
29+
}
30+
31+
pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
32+
//~^ ERROR lifetime parameter `'a` never used
33+
//~^^ ERROR lifetime parameter `'b` never used
34+
println!("{}", s);
35+
}
36+
37+
pub async fn func_with_unused_lifetime_in_two_params<'c>(s: &'c str, t: &'c str) {
38+
//~^ ERROR lifetime parameter `'c` never used
39+
println!("{}", s);
40+
}
41+
42+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: lifetime parameter `'a` never used
2+
--> $DIR/unused-lifetime.rs:26:40
3+
|
4+
LL | pub async fn func_with_unused_lifetime<'a>(s: &'a str) {
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/unused-lifetime.rs:7:9
9+
|
10+
LL | #![deny(unused_lifetimes)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: lifetime parameter `'a` never used
14+
--> $DIR/unused-lifetime.rs:31:44
15+
|
16+
LL | pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
17+
| ^^
18+
19+
error: lifetime parameter `'b` never used
20+
--> $DIR/unused-lifetime.rs:31:48
21+
|
22+
LL | pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
23+
| ^^
24+
25+
error: lifetime parameter `'c` never used
26+
--> $DIR/unused-lifetime.rs:37:54
27+
|
28+
LL | pub async fn func_with_unused_lifetime_in_two_params<'c>(s: &'c str, t: &'c str) {
29+
| ^^
30+
31+
error: aborting due to 4 previous errors
32+

0 commit comments

Comments
 (0)