Lint single-use-lifetimes on the AST.

This commit is contained in:
Camille GILLOT 2022-05-10 21:15:30 +02:00
parent db8a9274a9
commit 563916d698
19 changed files with 364 additions and 469 deletions

View file

@ -1,19 +1,15 @@
// Check "unused_lifetimes" lint on both async and sync functions
// Both cases should be diagnosed the same way.
// edition:2018
#![deny(unused_lifetimes)]
async fn async_wrong_without_args<'a>() {} //~ ERROR lifetime parameter `'a` never used
// Async part with unused lifetimes
//
// 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>() {}
async fn async_wrong_1_lifetime<'a>(_: &i32) {} //~ ERROR lifetime parameter `'a` never used
async fn async_wrong_1_lifetime<'a>(_: &i32) {}
async fn async_wrong_2_lifetimes<'a, 'b>(_: &'a i32, _: &i32) {}
async fn async_wrong_2_lifetimes<'a, 'b>(_: &'a i32, _: &i32) {} //~ ERROR lifetime parameter `'b` never used
async fn async_right_1_lifetime<'a>(_: &'a i32) {}
@ -24,10 +20,6 @@ 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

View file

@ -1,28 +1,48 @@
error: lifetime parameter `'a` never used
--> $DIR/unused-lifetime.rs:31:23
--> $DIR/unused-lifetime.rs:8:35
|
LL | fn wrong_without_args<'a>() {}
| -^^- help: elide the unused lifetime
LL | async fn async_wrong_without_args<'a>() {}
| -^^- help: elide the unused lifetime
|
note: the lint level is defined here
--> $DIR/unused-lifetime.rs:5:9
--> $DIR/unused-lifetime.rs:6:9
|
LL | #![deny(unused_lifetimes)]
| ^^^^^^^^^^^^^^^^
error: lifetime parameter `'a` never used
--> $DIR/unused-lifetime.rs:33:21
--> $DIR/unused-lifetime.rs:10:33
|
LL | async fn async_wrong_1_lifetime<'a>(_: &i32) {}
| -^^- help: elide the unused lifetime
error: lifetime parameter `'b` never used
--> $DIR/unused-lifetime.rs:12:38
|
LL | async fn async_wrong_2_lifetimes<'a, 'b>(_: &'a i32, _: &i32) {}
| --^^
| |
| help: elide the unused lifetime
error: lifetime parameter `'a` never used
--> $DIR/unused-lifetime.rs:23:23
|
LL | fn wrong_without_args<'a>() {}
| -^^- help: elide the unused lifetime
error: lifetime parameter `'a` never used
--> $DIR/unused-lifetime.rs:25:21
|
LL | fn wrong_1_lifetime<'a>(_: &i32) {}
| -^^- help: elide the unused lifetime
error: lifetime parameter `'b` never used
--> $DIR/unused-lifetime.rs:35:26
--> $DIR/unused-lifetime.rs:27:26
|
LL | fn wrong_2_lifetimes<'a, 'b>(_: &'a i32, _: &i32) {}
| --^^
| |
| help: elide the unused lifetime
error: aborting due to 3 previous errors
error: aborting due to 6 previous errors

View file

@ -11,6 +11,11 @@ note: the lint level is defined here
|
LL | #![deny(single_use_lifetimes)]
| ^^^^^^^^^^^^^^^^^^^^
help: elide the single-use lifetime
|
LL - a: for<'a> fn(&'a u32),
LL + a: fn(&u32),
|
error[E0581]: return type references lifetime `'a`, which is not constrained by the fn input types
--> $DIR/fn-types.rs:12:22

View file

@ -19,4 +19,15 @@ fn left<'x, 'y>(foo: Double<'x, 'y>) -> &'x u32 { foo.f } //~ ERROR `'y` only us
fn right<'x, 'y>(foo: Double<'x, 'y>) -> &'y u32 { foo.f } //~ ERROR `'x` only used once
//~^ HELP elide the single-use lifetime
fn main() { }
pub trait Tfv<'a> {}
// Do NOT lint in an HRTB.
pub fn g<T: for<'a> Tfv<'a>>() {}
// Do NOT lint for trait bounds.
pub fn h<'a, S>(_: S)
where
S: Tfv<'a>,
{}
fn main() {}

View file

@ -14,4 +14,10 @@ fn b<'a>() -> &'a u32 {
&22
}
pub trait Tfv<'a> {}
impl Tfv<'_> for () {}
// Do NOT lint if used in return type.
pub fn i<'a>() -> impl Tfv<'a> {}
fn main() {}

View file

@ -11,6 +11,11 @@ note: the lint level is defined here
|
LL | #![deny(single_use_lifetimes)]
| ^^^^^^^^^^^^^^^^^^^^
help: elide the single-use lifetime
|
LL - impl<'f> Foo<'f> {
LL + impl Foo<'_> {
|
error: aborting due to previous error

View file

@ -9,6 +9,7 @@ struct Foo<'f> {
}
impl<'f> Foo<'f> { //~ ERROR `'f` only used once
//~^ HELP elide the single-use lifetime
fn inherent_a<'a>(&self, data: &'a u32) { //~ ERROR `'a` only used once
//~^ HELP elide the single-use lifetime
}

View file

@ -1,10 +1,10 @@
error: lifetime parameter `'a` only used once
--> $DIR/one-use-in-inherent-method-argument.rs:12:19
error: lifetime parameter `'f` only used once
--> $DIR/one-use-in-inherent-method-argument.rs:11:6
|
LL | fn inherent_a<'a>(&self, data: &'a u32) {
| ^^ -- ...is used only here
| |
| this lifetime...
LL | impl<'f> Foo<'f> {
| ^^ -- ...is used only here
| |
| this lifetime...
|
note: the lint level is defined here
--> $DIR/one-use-in-inherent-method-argument.rs:1:9
@ -13,17 +13,23 @@ LL | #![deny(single_use_lifetimes)]
| ^^^^^^^^^^^^^^^^^^^^
help: elide the single-use lifetime
|
LL - impl<'f> Foo<'f> {
LL + impl Foo<'_> {
|
error: lifetime parameter `'a` only used once
--> $DIR/one-use-in-inherent-method-argument.rs:13:19
|
LL | fn inherent_a<'a>(&self, data: &'a u32) {
| ^^ -- ...is used only here
| |
| this lifetime...
|
help: elide the single-use lifetime
|
LL - fn inherent_a<'a>(&self, data: &'a u32) {
LL + fn inherent_a(&self, data: &u32) {
|
error: lifetime parameter `'f` only used once
--> $DIR/one-use-in-inherent-method-argument.rs:11:6
|
LL | impl<'f> Foo<'f> {
| ^^ -- ...is used only here
| |
| this lifetime...
error: aborting due to 2 previous errors

View file

@ -11,6 +11,11 @@ note: the lint level is defined here
|
LL | #![deny(single_use_lifetimes)]
| ^^^^^^^^^^^^^^^^^^^^
help: elide the single-use lifetime
|
LL - impl<'f> Foo<'f> {
LL + impl Foo<'_> {
|
error: aborting due to previous error

View file

@ -4,7 +4,8 @@
//
// check-pass
#![deny(single_use_lifetimes)]
// Use forbid to verify that `automatically_derived` is handled correctly.
#![forbid(single_use_lifetimes)]
#![allow(dead_code)]
#![allow(unused_variables)]

View file

@ -18,4 +18,9 @@ impl<'f> Iterator for Foo<'f> {
}
}
fn main() { }
trait Bar<'a> {
// But we should not warn here.
fn bar(x: Foo<'a>);
}
fn main() {}

View file

@ -11,6 +11,11 @@ note: the lint level is defined here
|
LL | #![deny(single_use_lifetimes)]
| ^^^^^^^^^^^^^^^^^^^^
help: elide the single-use lifetime
|
LL - impl<'f> Foo<'f> {
LL + impl Foo<'_> {
|
error: aborting due to previous error