Update tests of "unused_lifetimes" lint for async functions and corresponding source code
This commit is contained in:
parent
5be3f9f10e
commit
64f11b9639
3 changed files with 58 additions and 57 deletions
|
@ -644,19 +644,19 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
|
||||||
} else {
|
} else {
|
||||||
bug!();
|
bug!();
|
||||||
};
|
};
|
||||||
if let hir::ParamName::Plain(param_name) = name {
|
// We cannot predict what lifetimes are unused in opaque type.
|
||||||
if param_name.name == kw::UnderscoreLifetime {
|
self.lifetime_uses.insert(def_id, LifetimeUseSet::Many);
|
||||||
|
if let hir::ParamName::Plain(Ident {
|
||||||
|
name: kw::UnderscoreLifetime,
|
||||||
|
..
|
||||||
|
}) = name
|
||||||
|
{
|
||||||
// Pick the elided lifetime "definition" if one exists
|
// Pick the elided lifetime "definition" if one exists
|
||||||
// and use it to make an elision scope.
|
// and use it to make an elision scope.
|
||||||
self.lifetime_uses.insert(def_id, LifetimeUseSet::Many);
|
|
||||||
elision = Some(reg);
|
elision = Some(reg);
|
||||||
} else {
|
} else {
|
||||||
lifetimes.insert(name, reg);
|
lifetimes.insert(name, reg);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
self.lifetime_uses.insert(def_id, LifetimeUseSet::Many);
|
|
||||||
lifetimes.insert(name, reg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
|
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
|
||||||
non_lifetime_count += 1;
|
non_lifetime_count += 1;
|
||||||
|
|
|
@ -1,42 +1,47 @@
|
||||||
|
// Check "unused_lifetimes" lint on both async and sync functions
|
||||||
|
|
||||||
// edition:2018
|
// edition:2018
|
||||||
|
|
||||||
// Avoid spurious warnings of unused lifetime. The below async functions
|
|
||||||
// are desugered to have an unused lifetime
|
|
||||||
// but we don't want to warn about that as there's nothing they can do about it.
|
|
||||||
|
|
||||||
#![deny(unused_lifetimes)]
|
#![deny(unused_lifetimes)]
|
||||||
#![allow(dead_code)]
|
|
||||||
|
|
||||||
pub async fn october(s: &str) {
|
|
||||||
println!("{}", s);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn async_fn(&mut ref s: &mut[i32]) {
|
// Async part with unused lifetimes
|
||||||
println!("{:?}", s);
|
//
|
||||||
}
|
// Even wrong cases don't cause errors because async functions are desugared with all lifetimes
|
||||||
|
// involved in the signature. So, we cannot predict what lifetimes are unused in async function.
|
||||||
|
async fn async_wrong_without_args<'a>() {}
|
||||||
|
|
||||||
macro_rules! foo_macro {
|
async fn async_wrong_1_lifetime<'a>(_: &i32) {}
|
||||||
() => {
|
|
||||||
pub async fn async_fn_in_macro(&mut ref _s: &mut[i32]) {}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
foo_macro!();
|
async fn async_wrong_2_lifetimes<'a, 'b>(_: &'a i32, _: &i32) {}
|
||||||
|
|
||||||
pub async fn func_with_unused_lifetime<'a>(s: &'a str) {
|
async fn async_right_1_lifetime<'a>(_: &'a i32) {}
|
||||||
//~^ ERROR lifetime parameter `'a` never used
|
|
||||||
println!("{}", s);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
|
async fn async_right_2_lifetimes<'a, 'b>(_: &'a i32, _: &'b i32) {}
|
||||||
//~^ ERROR lifetime parameter `'a` never used
|
|
||||||
//~^^ ERROR lifetime parameter `'b` never used
|
async fn async_right_trait_bound_lifetime<'a, I>(_: I)
|
||||||
println!("{}", s);
|
where
|
||||||
}
|
I: Iterator<Item = &'a i32>
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
// Sync part with unused lifetimes
|
||||||
|
//
|
||||||
|
// These functions are compiled as supposed
|
||||||
|
fn wrong_without_args<'a>() {} //~ ERROR lifetime parameter `'a` never used
|
||||||
|
|
||||||
|
fn wrong_1_lifetime<'a>(_: &i32) {} //~ ERROR lifetime parameter `'a` never used
|
||||||
|
|
||||||
|
fn wrong_2_lifetimes<'a, 'b>(_: &'a i32, _: &i32) {} //~ ERROR lifetime parameter `'b` never used
|
||||||
|
|
||||||
|
fn right_1_lifetime<'a>(_: &'a i32) {}
|
||||||
|
|
||||||
|
fn right_2_lifetimes<'a, 'b>(_: &'a i32, _: &'b i32) {}
|
||||||
|
|
||||||
|
fn right_trait_bound_lifetime<'a, I>(_: I)
|
||||||
|
where
|
||||||
|
I: Iterator<Item = &'a i32>
|
||||||
|
{}
|
||||||
|
|
||||||
pub async fn func_with_unused_lifetime_in_two_params<'c>(s: &'c str, t: &'c str) {
|
|
||||||
//~^ ERROR lifetime parameter `'c` never used
|
|
||||||
println!("{}", s);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
|
@ -1,32 +1,28 @@
|
||||||
error: lifetime parameter `'a` never used
|
error: lifetime parameter `'a` never used
|
||||||
--> $DIR/unused-lifetime.rs:26:40
|
--> $DIR/unused-lifetime.rs:31:23
|
||||||
|
|
|
|
||||||
LL | pub async fn func_with_unused_lifetime<'a>(s: &'a str) {
|
LL | fn wrong_without_args<'a>() {}
|
||||||
| ^^
|
| -^^- help: elide the unused lifetime
|
||||||
|
|
|
|
||||||
note: the lint level is defined here
|
note: the lint level is defined here
|
||||||
--> $DIR/unused-lifetime.rs:7:9
|
--> $DIR/unused-lifetime.rs:5:9
|
||||||
|
|
|
|
||||||
LL | #![deny(unused_lifetimes)]
|
LL | #![deny(unused_lifetimes)]
|
||||||
| ^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: lifetime parameter `'a` never used
|
error: lifetime parameter `'a` never used
|
||||||
--> $DIR/unused-lifetime.rs:31:44
|
--> $DIR/unused-lifetime.rs:33:21
|
||||||
|
|
|
|
||||||
LL | pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
|
LL | fn wrong_1_lifetime<'a>(_: &i32) {}
|
||||||
| ^^
|
| -^^- help: elide the unused lifetime
|
||||||
|
|
||||||
error: lifetime parameter `'b` never used
|
error: lifetime parameter `'b` never used
|
||||||
--> $DIR/unused-lifetime.rs:31:48
|
--> $DIR/unused-lifetime.rs:35:26
|
||||||
|
|
|
|
||||||
LL | pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
|
LL | fn wrong_2_lifetimes<'a, 'b>(_: &'a i32, _: &i32) {}
|
||||||
| ^^
|
| --^^
|
||||||
|
| |
|
||||||
|
| help: elide the unused lifetime
|
||||||
|
|
||||||
error: lifetime parameter `'c` never used
|
error: aborting due to 3 previous errors
|
||||||
--> $DIR/unused-lifetime.rs:37:54
|
|
||||||
|
|
|
||||||
LL | pub async fn func_with_unused_lifetime_in_two_params<'c>(s: &'c str, t: &'c str) {
|
|
||||||
| ^^
|
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue