diff options
Diffstat (limited to 'tests/ui/single-use-lifetime')
28 files changed, 719 insertions, 0 deletions
diff --git a/tests/ui/single-use-lifetime/derive-eq.rs b/tests/ui/single-use-lifetime/derive-eq.rs new file mode 100644 index 000000000..e5bdfc55d --- /dev/null +++ b/tests/ui/single-use-lifetime/derive-eq.rs @@ -0,0 +1,11 @@ +// check-pass + +#![deny(single_use_lifetimes)] + +#[derive(PartialEq, Eq)] +struct Foo<'a, T> { + /// a reference to the underlying secret data that will be derefed + pub data: &'a mut T, +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/fn-types.rs b/tests/ui/single-use-lifetime/fn-types.rs new file mode 100644 index 000000000..018535f64 --- /dev/null +++ b/tests/ui/single-use-lifetime/fn-types.rs @@ -0,0 +1,16 @@ +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +// Test that we DO warn when lifetime name is used only +// once in a fn argument. + +struct Foo { + a: for<'a> fn(&'a u32), //~ ERROR `'a` only used once + b: for<'a> fn(&'a u32, &'a u32), // OK, used twice. + c: for<'a> fn(&'a u32) -> &'a u32, // OK, used twice. + d: for<'a> fn() -> &'a u32, // OK, used only in return type. + //~^ ERROR return type references lifetime `'a`, which is not constrained by the fn input types +} + +fn main() { } diff --git a/tests/ui/single-use-lifetime/fn-types.stderr b/tests/ui/single-use-lifetime/fn-types.stderr new file mode 100644 index 000000000..55959def4 --- /dev/null +++ b/tests/ui/single-use-lifetime/fn-types.stderr @@ -0,0 +1,28 @@ +error: lifetime parameter `'a` only used once + --> $DIR/fn-types.rs:9:10 + | +LL | a: for<'a> fn(&'a u32), + | ^^ -- ...is used only here + | | + | this lifetime... + | +note: the lint level is defined here + --> $DIR/fn-types.rs:1:9 + | +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 + | +LL | d: for<'a> fn() -> &'a u32, // OK, used only in return type. + | ^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0581`. diff --git a/tests/ui/single-use-lifetime/issue-104440.rs b/tests/ui/single-use-lifetime/issue-104440.rs new file mode 100644 index 000000000..0795e9530 --- /dev/null +++ b/tests/ui/single-use-lifetime/issue-104440.rs @@ -0,0 +1,100 @@ +#![feature(decl_macro, rustc_attrs)] +#![deny(single_use_lifetimes)] + +mod type_params { + macro m($T:ident) { + fn f<$T: Clone, T: PartialEq>(t1: $T, t2: T) -> ($T, bool) { + (t1.clone(), t2 == t2) + } + } + + #[rustc_macro_transparency = "semitransparent"] + macro n($T:ident) { + fn g<$T: Clone>(t1: $T, t2: T) -> (T, $T) { + (t1.clone(), t2.clone()) + } + fn h<T: Clone>(t1: $T, t2: T) -> (T, $T) { + (t1.clone(), t2.clone()) + } + } + + #[rustc_macro_transparency = "transparent"] + macro p($T:ident) { + fn j<$T: Clone>(t1: $T, t2: T) -> (T, $T) { + (t1.clone(), t2.clone()) + } + fn k<T: Clone>(t1: $T, t2: T) -> (T, $T) { + (t1.clone(), t2.clone()) + } + } + + m!(T); + n!(T); + p!(T); +} + +mod lifetime_params { + macro m($a:lifetime) { + fn f<'b, 'c, $a: 'b, 'a: 'c>(t1: &$a(), t2: &'a ()) -> (&'b (), &'c ()) { //~ ERROR lifetime parameter `'a` only used once + (t1, t2) + } + } + + #[rustc_macro_transparency = "semitransparent"] + macro n($a:lifetime) { + fn g<$a>(t1: &$a(), t2: &'a ()) -> (&'a (), &$a ()) { + (t1, t2) + } + fn h<'a>(t1: &$a(), t2: &'a ()) -> (&'a (), &$a ()) { + (t1, t2) + } + } + + #[rustc_macro_transparency = "transparent"] + macro p($a:lifetime) { + fn j<$a>(t1: &$a(), t2: &'a ()) -> (&'a (), &$a ()) { + (t1, t2) + } + fn k<'a>(t1: &$a(), t2: &'a ()) -> (&'a (), &$a ()) { + (t1, t2) + } + } + + m!('a); //~ ERROR lifetime parameter `'a` only used once + n!('a); + p!('a); +} + +mod const_params { + macro m($C:ident) { + fn f<const $C: usize, const C: usize>(t1: [(); $C], t2: [(); C]) -> ([(); $C], [(); C]) { + (t1, t2) + } + } + + #[rustc_macro_transparency = "semitransparent"] + macro n($C:ident) { + fn g<const $C: usize>(t1: [(); $C], t2: [(); C]) -> ([(); C], [(); $C]) { + (t1, t2) + } + fn h<const C: usize>(t1: [(); $C], t2: [(); C]) -> ([(); C], [(); $C]) { + (t1, t2) + } + } + + #[rustc_macro_transparency = "transparent"] + macro p($C:ident) { + fn j<const $C: usize>(t1: [(); $C], t2: [(); C]) -> ([(); C], [(); $C]) { + (t1, t2) + } + fn k<const C: usize>(t1: [(); $C], t2: [(); C]) -> ([(); C], [(); $C]) { + (t1, t2) + } + } + + m!(C); + n!(C); + p!(C); +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/issue-104440.stderr b/tests/ui/single-use-lifetime/issue-104440.stderr new file mode 100644 index 000000000..54ded31dc --- /dev/null +++ b/tests/ui/single-use-lifetime/issue-104440.stderr @@ -0,0 +1,28 @@ +error: lifetime parameter `'a` only used once + --> $DIR/issue-104440.rs:63:8 + | +LL | m!('a); + | ^^ + | | + | this lifetime... + | ...is used only here + | +note: the lint level is defined here + --> $DIR/issue-104440.rs:2:9 + | +LL | #![deny(single_use_lifetimes)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: lifetime parameter `'a` only used once + --> $DIR/issue-104440.rs:38:30 + | +LL | fn f<'b, 'c, $a: 'b, 'a: 'c>(t1: &$a(), t2: &'a ()) -> (&'b (), &'c ()) { + | ^^ this lifetime... -- ...is used only here +... +LL | m!('a); + | ------ in this macro invocation + | + = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 2 previous errors + diff --git a/tests/ui/single-use-lifetime/one-use-in-fn-argument.rs b/tests/ui/single-use-lifetime/one-use-in-fn-argument.rs new file mode 100644 index 000000000..7919ef820 --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-fn-argument.rs @@ -0,0 +1,33 @@ +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +// Test that we DO warn when lifetime name is used only +// once in a fn argument. + +fn a<'a>(x: &'a u32) { //~ ERROR `'a` only used once + //~^ HELP elide the single-use lifetime +} + +struct Single<'a> { x: &'a u32 } +struct Double<'a, 'b> { f: &'a &'b u32 } + +fn center<'m>(_: Single<'m>) {} //~ ERROR `'m` only used once +//~^ HELP elide the single-use lifetime +fn left<'x, 'y>(foo: Double<'x, 'y>) -> &'x u32 { foo.f } //~ ERROR `'y` only used once +//~^ HELP elide the single-use lifetime +fn right<'x, 'y>(foo: Double<'x, 'y>) -> &'y u32 { foo.f } //~ ERROR `'x` only used once +//~^ HELP elide the single-use lifetime + +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() {} diff --git a/tests/ui/single-use-lifetime/one-use-in-fn-argument.stderr b/tests/ui/single-use-lifetime/one-use-in-fn-argument.stderr new file mode 100644 index 000000000..93f16f5ba --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-fn-argument.stderr @@ -0,0 +1,59 @@ +error: lifetime parameter `'a` only used once + --> $DIR/one-use-in-fn-argument.rs:8:6 + | +LL | fn a<'a>(x: &'a u32) { + | ^^ -- ...is used only here + | | + | this lifetime... + | +note: the lint level is defined here + --> $DIR/one-use-in-fn-argument.rs:1:9 + | +LL | #![deny(single_use_lifetimes)] + | ^^^^^^^^^^^^^^^^^^^^ +help: elide the single-use lifetime + | +LL - fn a<'a>(x: &'a u32) { +LL + fn a(x: &u32) { + | + +error: lifetime parameter `'m` only used once + --> $DIR/one-use-in-fn-argument.rs:15:11 + | +LL | fn center<'m>(_: Single<'m>) {} + | ^^ -- ...is used only here + | | + | this lifetime... + | +help: elide the single-use lifetime + | +LL - fn center<'m>(_: Single<'m>) {} +LL + fn center(_: Single<'_>) {} + | + +error: lifetime parameter `'y` only used once + --> $DIR/one-use-in-fn-argument.rs:17:13 + | +LL | fn left<'x, 'y>(foo: Double<'x, 'y>) -> &'x u32 { foo.f } + | ^^ this lifetime... -- ...is used only here + | +help: elide the single-use lifetime + | +LL - fn left<'x, 'y>(foo: Double<'x, 'y>) -> &'x u32 { foo.f } +LL + fn left<'x>(foo: Double<'x, '_>) -> &'x u32 { foo.f } + | + +error: lifetime parameter `'x` only used once + --> $DIR/one-use-in-fn-argument.rs:19:10 + | +LL | fn right<'x, 'y>(foo: Double<'x, 'y>) -> &'y u32 { foo.f } + | ^^ this lifetime... -- ...is used only here + | +help: elide the single-use lifetime + | +LL - fn right<'x, 'y>(foo: Double<'x, 'y>) -> &'y u32 { foo.f } +LL + fn right<'y>(foo: Double<'_, 'y>) -> &'y u32 { foo.f } + | + +error: aborting due to 4 previous errors + diff --git a/tests/ui/single-use-lifetime/one-use-in-fn-return.rs b/tests/ui/single-use-lifetime/one-use-in-fn-return.rs new file mode 100644 index 000000000..1ade01eed --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-fn-return.rs @@ -0,0 +1,23 @@ +// Test that we DO NOT warn when lifetime name is used only +// once in a fn return type -- using `'_` is not legal there, +// as it must refer back to an argument. +// +// (Normally, using `'static` would be preferred, but there are +// times when that is not what you want.) + +// check-pass + +#![deny(single_use_lifetimes)] + +// OK: used only in return type +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() {} diff --git a/tests/ui/single-use-lifetime/one-use-in-inherent-impl-header.rs b/tests/ui/single-use-lifetime/one-use-in-inherent-impl-header.rs new file mode 100644 index 000000000..64f1c9118 --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-inherent-impl-header.rs @@ -0,0 +1,22 @@ +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +// Test that we DO warn for a lifetime used only once in an impl, and that we +// don't warn for the anonymous lifetime. + +struct Foo<'f> { + data: &'f u32 +} + +impl<'f> Foo<'f> { //~ ERROR `'f` only used once + fn inherent_a(&self) { + } +} + +impl Foo<'_> { + fn inherent_b(&self) {} +} + + +fn main() { } diff --git a/tests/ui/single-use-lifetime/one-use-in-inherent-impl-header.stderr b/tests/ui/single-use-lifetime/one-use-in-inherent-impl-header.stderr new file mode 100644 index 000000000..94129560f --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-inherent-impl-header.stderr @@ -0,0 +1,21 @@ +error: lifetime parameter `'f` only used once + --> $DIR/one-use-in-inherent-impl-header.rs:12:6 + | +LL | impl<'f> Foo<'f> { + | ^^ -- ...is used only here + | | + | this lifetime... + | +note: the lint level is defined here + --> $DIR/one-use-in-inherent-impl-header.rs:1:9 + | +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 + diff --git a/tests/ui/single-use-lifetime/one-use-in-inherent-method-argument.rs b/tests/ui/single-use-lifetime/one-use-in-inherent-method-argument.rs new file mode 100644 index 000000000..eecd715ef --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-inherent-method-argument.rs @@ -0,0 +1,18 @@ +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +// Test that we DO warn for a lifetime used only once in an inherent method. + +struct Foo<'f> { + data: &'f u32 +} + +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 + } +} + +fn main() { } diff --git a/tests/ui/single-use-lifetime/one-use-in-inherent-method-argument.stderr b/tests/ui/single-use-lifetime/one-use-in-inherent-method-argument.stderr new file mode 100644 index 000000000..39507785b --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-inherent-method-argument.stderr @@ -0,0 +1,35 @@ +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... + | +note: the lint level is defined here + --> $DIR/one-use-in-inherent-method-argument.rs:1:9 + | +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: aborting due to 2 previous errors + diff --git a/tests/ui/single-use-lifetime/one-use-in-inherent-method-return.rs b/tests/ui/single-use-lifetime/one-use-in-inherent-method-return.rs new file mode 100644 index 000000000..c5938f4a1 --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-inherent-method-return.rs @@ -0,0 +1,18 @@ +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +// Test that we DO NOT warn for a lifetime used just once in a return type, +// where that return type is in an inherent method. + +struct Foo<'f> { + data: &'f u32 +} + +impl<'f> Foo<'f> { //~ ERROR `'f` only used once + fn inherent_a<'a>(&self) -> &'a u32 { // OK for 'a + &22 + } +} + +fn main() { } diff --git a/tests/ui/single-use-lifetime/one-use-in-inherent-method-return.stderr b/tests/ui/single-use-lifetime/one-use-in-inherent-method-return.stderr new file mode 100644 index 000000000..69578fe2f --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-inherent-method-return.stderr @@ -0,0 +1,21 @@ +error: lifetime parameter `'f` only used once + --> $DIR/one-use-in-inherent-method-return.rs:12:6 + | +LL | impl<'f> Foo<'f> { + | ^^ -- ...is used only here + | | + | this lifetime... + | +note: the lint level is defined here + --> $DIR/one-use-in-inherent-method-return.rs:1:9 + | +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 + diff --git a/tests/ui/single-use-lifetime/one-use-in-struct.rs b/tests/ui/single-use-lifetime/one-use-in-struct.rs new file mode 100644 index 000000000..9cad942e7 --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-struct.rs @@ -0,0 +1,28 @@ +// Test that we do not warn for named lifetimes in structs, +// even when they are only used once (since to not use a named +// lifetime is illegal!) +// +// check-pass + +// Use forbid to verify that `automatically_derived` is handled correctly. +#![forbid(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +struct Foo<'f> { + data: &'f u32, +} + +enum Bar<'f> { + Data(&'f u32), +} + +trait Baz<'f> {} + +// `Derive`d impls shouldn't trigger a warning, either (Issue #53738). +#[derive(Debug)] +struct Quux<'a> { + priors: &'a u32, +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/one-use-in-trait-method-argument.rs b/tests/ui/single-use-lifetime/one-use-in-trait-method-argument.rs new file mode 100644 index 000000000..1848fc91c --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-trait-method-argument.rs @@ -0,0 +1,26 @@ +// Test that we DO warn for a lifetime on an impl used only in `&self` +// in a trait method. + +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +struct Foo<'f> { + data: &'f u32 +} + +impl<'f> Iterator for Foo<'f> { + type Item = &'f u32; + + fn next<'g>(&'g mut self) -> Option<Self::Item> { //~ ERROR `'g` only used once + //~^ HELP elide the single-use lifetime + None + } +} + +trait Bar<'a> { + // But we should not warn here. + fn bar(x: Foo<'a>); +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/one-use-in-trait-method-argument.stderr b/tests/ui/single-use-lifetime/one-use-in-trait-method-argument.stderr new file mode 100644 index 000000000..1a6e8310d --- /dev/null +++ b/tests/ui/single-use-lifetime/one-use-in-trait-method-argument.stderr @@ -0,0 +1,21 @@ +error: lifetime parameter `'g` only used once + --> $DIR/one-use-in-trait-method-argument.rs:15:13 + | +LL | fn next<'g>(&'g mut self) -> Option<Self::Item> { + | ^^ -- ...is used only here + | | + | this lifetime... + | +note: the lint level is defined here + --> $DIR/one-use-in-trait-method-argument.rs:4:9 + | +LL | #![deny(single_use_lifetimes)] + | ^^^^^^^^^^^^^^^^^^^^ +help: elide the single-use lifetime + | +LL - fn next<'g>(&'g mut self) -> Option<Self::Item> { +LL + fn next(&mut self) -> Option<Self::Item> { + | + +error: aborting due to previous error + diff --git a/tests/ui/single-use-lifetime/two-uses-in-fn-argument-and-return.rs b/tests/ui/single-use-lifetime/two-uses-in-fn-argument-and-return.rs new file mode 100644 index 000000000..f80f3f63c --- /dev/null +++ b/tests/ui/single-use-lifetime/two-uses-in-fn-argument-and-return.rs @@ -0,0 +1,15 @@ +// Test that we DO NOT warn when lifetime name is used in +// both the argument and return. +// +// check-pass + +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +// OK: used twice +fn c<'a>(x: &'a u32) -> &'a u32 { + &22 +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/two-uses-in-fn-arguments.rs b/tests/ui/single-use-lifetime/two-uses-in-fn-arguments.rs new file mode 100644 index 000000000..51724ebf8 --- /dev/null +++ b/tests/ui/single-use-lifetime/two-uses-in-fn-arguments.rs @@ -0,0 +1,16 @@ +// Test that we DO NOT warn when lifetime name is used multiple +// arguments, or more than once in a single argument. +// +// check-pass + +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +// OK: used twice +fn c<'a>(x: &'a u32, y: &'a u32) {} + +// OK: used twice +fn d<'a>(x: (&'a u32, &'a u32)) {} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/two-uses-in-inherent-impl-header.rs b/tests/ui/single-use-lifetime/two-uses-in-inherent-impl-header.rs new file mode 100644 index 000000000..125a395db --- /dev/null +++ b/tests/ui/single-use-lifetime/two-uses-in-inherent-impl-header.rs @@ -0,0 +1,17 @@ +// Test that we DO NOT warn for a lifetime used twice in an impl. +// +// check-pass + +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +struct Foo<'f> { + data: &'f u32, +} + +impl<'f> Foo<'f> { + fn inherent_a(&self, data: &'f u32) {} +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/two-uses-in-inherent-method-argument-and-return.rs b/tests/ui/single-use-lifetime/two-uses-in-inherent-method-argument-and-return.rs new file mode 100644 index 000000000..06ab6cdbf --- /dev/null +++ b/tests/ui/single-use-lifetime/two-uses-in-inherent-method-argument-and-return.rs @@ -0,0 +1,18 @@ +// Test that we DO NOT warn for a lifetime used twice in an impl method and +// header. + +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +struct Foo<'f> { + data: &'f u32 +} + +impl<'f> Foo<'f> { //~ ERROR `'f` only used once + fn inherent_a<'a>(&self, data: &'a u32) -> &'a u32{ + data + } +} + +fn main() { } diff --git a/tests/ui/single-use-lifetime/two-uses-in-inherent-method-argument-and-return.stderr b/tests/ui/single-use-lifetime/two-uses-in-inherent-method-argument-and-return.stderr new file mode 100644 index 000000000..4794566ea --- /dev/null +++ b/tests/ui/single-use-lifetime/two-uses-in-inherent-method-argument-and-return.stderr @@ -0,0 +1,21 @@ +error: lifetime parameter `'f` only used once + --> $DIR/two-uses-in-inherent-method-argument-and-return.rs:12:6 + | +LL | impl<'f> Foo<'f> { + | ^^ -- ...is used only here + | | + | this lifetime... + | +note: the lint level is defined here + --> $DIR/two-uses-in-inherent-method-argument-and-return.rs:4:9 + | +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 + diff --git a/tests/ui/single-use-lifetime/two-uses-in-trait-impl.rs b/tests/ui/single-use-lifetime/two-uses-in-trait-impl.rs new file mode 100644 index 000000000..16431a39f --- /dev/null +++ b/tests/ui/single-use-lifetime/two-uses-in-trait-impl.rs @@ -0,0 +1,22 @@ +// Test that we DO NOT warn for a lifetime on an impl used in both +// header and in an associated type. +// +// check-pass + +#![deny(single_use_lifetimes)] +#![allow(dead_code)] +#![allow(unused_variables)] + +struct Foo<'f> { + data: &'f u32, +} + +impl<'f> Iterator for Foo<'f> { + type Item = &'f u32; + + fn next(&mut self) -> Option<Self::Item> { + None + } +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/zero-uses-in-fn.fixed b/tests/ui/single-use-lifetime/zero-uses-in-fn.fixed new file mode 100644 index 000000000..0f26a975a --- /dev/null +++ b/tests/ui/single-use-lifetime/zero-uses-in-fn.fixed @@ -0,0 +1,24 @@ +// run-rustfix + +// Test that we DO warn when lifetime name is not used at all. + +#![deny(unused_lifetimes)] +#![allow(dead_code, unused_variables)] + +fn september() {} +//~^ ERROR lifetime parameter `'a` never used +//~| HELP elide the unused lifetime + +fn october<'b, T>(s: &'b T) -> &'b T { + //~^ ERROR lifetime parameter `'a` never used + //~| HELP elide the unused lifetime + s +} + +fn november<'a>(s: &'a str) -> &'a str { + //~^ ERROR lifetime parameter `'b` never used + //~| HELP elide the unused lifetime + s +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/zero-uses-in-fn.rs b/tests/ui/single-use-lifetime/zero-uses-in-fn.rs new file mode 100644 index 000000000..7f9504fe5 --- /dev/null +++ b/tests/ui/single-use-lifetime/zero-uses-in-fn.rs @@ -0,0 +1,24 @@ +// run-rustfix + +// Test that we DO warn when lifetime name is not used at all. + +#![deny(unused_lifetimes)] +#![allow(dead_code, unused_variables)] + +fn september<'a>() {} +//~^ ERROR lifetime parameter `'a` never used +//~| HELP elide the unused lifetime + +fn october<'a, 'b, T>(s: &'b T) -> &'b T { + //~^ ERROR lifetime parameter `'a` never used + //~| HELP elide the unused lifetime + s +} + +fn november<'a, 'b>(s: &'a str) -> &'a str { + //~^ ERROR lifetime parameter `'b` never used + //~| HELP elide the unused lifetime + s +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/zero-uses-in-fn.stderr b/tests/ui/single-use-lifetime/zero-uses-in-fn.stderr new file mode 100644 index 000000000..59c0164e3 --- /dev/null +++ b/tests/ui/single-use-lifetime/zero-uses-in-fn.stderr @@ -0,0 +1,30 @@ +error: lifetime parameter `'a` never used + --> $DIR/zero-uses-in-fn.rs:8:14 + | +LL | fn september<'a>() {} + | -^^- help: elide the unused lifetime + | +note: the lint level is defined here + --> $DIR/zero-uses-in-fn.rs:5:9 + | +LL | #![deny(unused_lifetimes)] + | ^^^^^^^^^^^^^^^^ + +error: lifetime parameter `'a` never used + --> $DIR/zero-uses-in-fn.rs:12:12 + | +LL | fn october<'a, 'b, T>(s: &'b T) -> &'b T { + | ^^-- + | | + | help: elide the unused lifetime + +error: lifetime parameter `'b` never used + --> $DIR/zero-uses-in-fn.rs:18:17 + | +LL | fn november<'a, 'b>(s: &'a str) -> &'a str { + | --^^ + | | + | help: elide the unused lifetime + +error: aborting due to 3 previous errors + diff --git a/tests/ui/single-use-lifetime/zero-uses-in-impl.rs b/tests/ui/single-use-lifetime/zero-uses-in-impl.rs new file mode 100644 index 000000000..54803e1d2 --- /dev/null +++ b/tests/ui/single-use-lifetime/zero-uses-in-impl.rs @@ -0,0 +1,10 @@ +// Test that we DO warn when lifetime name is not used at all. + +#![deny(unused_lifetimes)] +#![allow(dead_code, unused_variables)] + +struct Foo {} + +impl<'a> Foo {} //~ ERROR `'a` never used + +fn main() {} diff --git a/tests/ui/single-use-lifetime/zero-uses-in-impl.stderr b/tests/ui/single-use-lifetime/zero-uses-in-impl.stderr new file mode 100644 index 000000000..b6e42d3e7 --- /dev/null +++ b/tests/ui/single-use-lifetime/zero-uses-in-impl.stderr @@ -0,0 +1,14 @@ +error: lifetime parameter `'a` never used + --> $DIR/zero-uses-in-impl.rs:8:6 + | +LL | impl<'a> Foo {} + | -^^- help: elide the unused lifetime + | +note: the lint level is defined here + --> $DIR/zero-uses-in-impl.rs:3:9 + | +LL | #![deny(unused_lifetimes)] + | ^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + |