Skip to content

Commit

Permalink
elided_named_lifetimes: bless & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigorenkoPV committed Aug 31, 2024
1 parent f7b0b22 commit a9b959a
Show file tree
Hide file tree
Showing 45 changed files with 460 additions and 27 deletions.
13 changes: 12 additions & 1 deletion src/tools/clippy/tests/ui/needless_lifetimes.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
error: elided lifetime has a name
--> tests/ui/needless_lifetimes.rs:266:52
|
LL | fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
| -- ^ this elided lifetime gets resolved as `'a`
| |
| lifetime `'a` declared here
|
= note: `-D elided-named-lifetimes` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(elided_named_lifetimes)]`

error: the following explicit lifetimes could be elided: 'a, 'b
--> tests/ui/needless_lifetimes.rs:17:23
|
Expand Down Expand Up @@ -553,5 +564,5 @@ LL - fn one_input<'a>(x: &'a u8) -> &'a u8 {
LL + fn one_input(x: &u8) -> &u8 {
|

error: aborting due to 46 previous errors
error: aborting due to 47 previous errors

11 changes: 10 additions & 1 deletion src/tools/clippy/tests/ui/ptr_arg.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
error: elided lifetime has a name
--> tests/ui/ptr_arg.rs:295:56
|
LL | fn cow_good_ret_ty<'a>(input: &'a Cow<'a, str>) -> &str {
| -- lifetime `'a` declared here ^ this elided lifetime gets resolved as `'a`
|
= note: `-D elided-named-lifetimes` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(elided_named_lifetimes)]`

error: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
--> tests/ui/ptr_arg.rs:13:14
|
Expand Down Expand Up @@ -212,5 +221,5 @@ error: using a reference to `Cow` is not recommended
LL | fn cow_bad_ret_ty_2<'a, 'b>(input: &'a Cow<'a, str>) -> &'b str {
| ^^^^^^^^^^^^^^^^ help: change this to: `&str`

error: aborting due to 24 previous errors
error: aborting due to 25 previous errors

2 changes: 1 addition & 1 deletion tests/ui/async-await/issues/issue-63388-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait Foo {}
impl Xyz {
async fn do_sth<'a>(
&'a self, foo: &dyn Foo
) -> &dyn Foo
) -> &dyn Foo //~ WARNING elided lifetime has a name
{
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
foo
Expand Down
13 changes: 12 additions & 1 deletion tests/ui/async-await/issues/issue-63388-1.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
warning: elided lifetime has a name
--> $DIR/issue-63388-1.rs:12:10
|
LL | async fn do_sth<'a>(
| -- lifetime `'a` declared here
LL | &'a self, foo: &dyn Foo
LL | ) -> &dyn Foo
| ^ this elided lifetime gets resolved as `'a`
|
= note: `#[warn(elided_named_lifetimes)]` on by default

error[E0621]: explicit lifetime required in the type of `foo`
--> $DIR/issue-63388-1.rs:13:5
|
Expand All @@ -10,6 +21,6 @@ LL | | foo
LL | | }
| |_____^ lifetime `'a` required

error: aborting due to 1 previous error
error: aborting due to 1 previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0621`.
10 changes: 10 additions & 0 deletions tests/ui/const-generics/type-dependent/issue-71348.full.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: elided lifetime has a name
--> $DIR/issue-71348.rs:18:68
|
LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<N>>::Target
| -- lifetime `'a` declared here ^ this elided lifetime gets resolved as `'a`
|
= note: `#[warn(elided_named_lifetimes)]` on by default

warning: 1 warning emitted

10 changes: 9 additions & 1 deletion tests/ui/const-generics/type-dependent/issue-71348.min.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
warning: elided lifetime has a name
--> $DIR/issue-71348.rs:18:68
|
LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<N>>::Target
| -- lifetime `'a` declared here ^ this elided lifetime gets resolved as `'a`
|
= note: `#[warn(elided_named_lifetimes)]` on by default

error: `&'static str` is forbidden as the type of a const generic parameter
--> $DIR/issue-71348.rs:10:24
|
Expand Down Expand Up @@ -30,5 +38,5 @@ help: add `#![feature(unsized_const_params)]` to the crate attributes to enable
LL + #![feature(unsized_const_params)]
|

error: aborting due to 2 previous errors
error: aborting due to 2 previous errors; 1 warning emitted

1 change: 1 addition & 0 deletions tests/ui/const-generics/type-dependent/issue-71348.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ trait Get<'a, const N: &'static str> {
impl Foo {
fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<N>>::Target
//[min]~^ ERROR `&'static str` is forbidden as the type of a const generic parameter
//~^^ WARNING elided lifetime has a name
where
Self: Get<'a, N>,
{
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/min_const_fn/min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl<T> Foo<T> {
impl<'a, T> Foo<T> {
const fn new_lt(t: T) -> Self { Foo(t) }
const fn into_inner_lt(self) -> T { self.0 } //~ destructor of
const fn get_lt(&'a self) -> &T { &self.0 }
const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
const fn get_lt(&'a self) -> &T { &self.0 } //~ WARNING elided lifetime has a name
const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 } //~ WARNING elided lifetime has a name
//~^ mutable references
//~| mutable references
//~| mutable references
Expand Down
22 changes: 21 additions & 1 deletion tests/ui/consts/min_const_fn/min_const_fn.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
warning: elided lifetime has a name
--> $DIR/min_const_fn.rs:47:34
|
LL | impl<'a, T> Foo<T> {
| -- lifetime `'a` declared here
...
LL | const fn get_lt(&'a self) -> &T { &self.0 }
| ^ this elided lifetime gets resolved as `'a`
|
= note: `#[warn(elided_named_lifetimes)]` on by default

warning: elided lifetime has a name
--> $DIR/min_const_fn.rs:48:42
|
LL | impl<'a, T> Foo<T> {
| -- lifetime `'a` declared here
...
LL | const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
| ^ this elided lifetime gets resolved as `'a`

error[E0493]: destructor of `Foo<T>` cannot be evaluated at compile-time
--> $DIR/min_const_fn.rs:37:25
|
Expand Down Expand Up @@ -228,7 +248,7 @@ LL | const fn no_apit(_x: impl std::fmt::Debug) {}
| |
| the destructor for this type cannot be evaluated in constant functions

error: aborting due to 24 previous errors
error: aborting due to 24 previous errors; 2 warnings emitted

Some errors have detailed explanations: E0493, E0658.
For more information about an error, try `rustc --explain E0493`.
2 changes: 1 addition & 1 deletion tests/ui/generics/generic-no-mangle.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ run-rustfix
#![allow(dead_code)]
#![allow(dead_code, elided_named_lifetimes)]
#![deny(no_mangle_generic_items)]

pub fn foo<T>() {} //~ ERROR functions generic over types or consts must be mangled
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/generics/generic-no-mangle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ run-rustfix
#![allow(dead_code)]
#![allow(dead_code, elided_named_lifetimes)]
#![deny(no_mangle_generic_items)]

#[no_mangle]
Expand Down
1 change: 1 addition & 0 deletions tests/ui/impl-trait/impl-fn-hrtb-bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn b() -> impl for<'a> Fn(&'a u8) -> (impl Debug + 'a) {

fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) {
//~^ ERROR `impl Trait` cannot capture higher-ranked lifetime from outer `impl Trait`
//~| WARNING elided lifetime has a name
|x| x
}

Expand Down
12 changes: 10 additions & 2 deletions tests/ui/impl-trait/impl-fn-hrtb-bounds.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0106]: missing lifetime specifier
--> $DIR/impl-fn-hrtb-bounds.rs:19:38
--> $DIR/impl-fn-hrtb-bounds.rs:20:38
|
LL | fn d() -> impl Fn() -> (impl Debug + '_) {
| ^^ expected named lifetime parameter
Expand All @@ -10,6 +10,14 @@ help: consider using the `'static` lifetime, but this is uncommon unless you're
LL | fn d() -> impl Fn() -> (impl Debug + 'static) {
| ~~~~~~~

warning: elided lifetime has a name
--> $DIR/impl-fn-hrtb-bounds.rs:14:52
|
LL | fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) {
| -- lifetime `'a` declared here ^^ this elided lifetime gets resolved as `'a`
|
= note: `#[warn(elided_named_lifetimes)]` on by default

error[E0657]: `impl Trait` cannot capture higher-ranked lifetime from outer `impl Trait`
--> $DIR/impl-fn-hrtb-bounds.rs:4:41
|
Expand Down Expand Up @@ -46,7 +54,7 @@ note: lifetime declared here
LL | fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) {
| ^^

error: aborting due to 4 previous errors
error: aborting due to 4 previous errors; 1 warning emitted

Some errors have detailed explanations: E0106, E0657.
For more information about an error, try `rustc --explain E0106`.
1 change: 1 addition & 0 deletions tests/ui/impl-trait/impl-fn-predefined-lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::Debug;

fn a<'a>() -> impl Fn(&'a u8) -> (impl Debug + '_) {
//~^ ERROR cannot resolve opaque type
//~| WARNING elided lifetime has a name
|x| x
//~^ ERROR expected generic lifetime parameter, found `'_`
}
Expand Down
14 changes: 11 additions & 3 deletions tests/ui/impl-trait/impl-fn-predefined-lifetimes.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
warning: elided lifetime has a name
--> $DIR/impl-fn-predefined-lifetimes.rs:4:48
|
LL | fn a<'a>() -> impl Fn(&'a u8) -> (impl Debug + '_) {
| -- lifetime `'a` declared here ^^ this elided lifetime gets resolved as `'a`
|
= note: `#[warn(elided_named_lifetimes)]` on by default

error[E0792]: expected generic lifetime parameter, found `'_`
--> $DIR/impl-fn-predefined-lifetimes.rs:6:9
--> $DIR/impl-fn-predefined-lifetimes.rs:7:9
|
LL | fn a<'a>() -> impl Fn(&'a u8) -> (impl Debug + '_) {
| -- this generic parameter must be used with a generic lifetime parameter
LL |
...
LL | |x| x
| ^

Expand All @@ -13,7 +21,7 @@ error[E0720]: cannot resolve opaque type
LL | fn a<'a>() -> impl Fn(&'a u8) -> (impl Debug + '_) {
| ^^^^^^^^^^^^^^^ cannot resolve opaque type

error: aborting due to 2 previous errors
error: aborting due to 2 previous errors; 1 warning emitted

Some errors have detailed explanations: E0720, E0792.
For more information about an error, try `rustc --explain E0720`.
1 change: 1 addition & 0 deletions tests/ui/impl-trait/rpit-assoc-pair-with-lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ check-pass

pub fn iter<'a>(v: Vec<(u32, &'a u32)>) -> impl DoubleEndedIterator<Item = (u32, &u32)> {
//~^ WARNING elided lifetime has a name
v.into_iter()
}

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/impl-trait/rpit-assoc-pair-with-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: elided lifetime has a name
--> $DIR/rpit-assoc-pair-with-lifetime.rs:3:82
|
LL | pub fn iter<'a>(v: Vec<(u32, &'a u32)>) -> impl DoubleEndedIterator<Item = (u32, &u32)> {
| -- lifetime `'a` declared here ^ this elided lifetime gets resolved as `'a`
|
= note: `#[warn(elided_named_lifetimes)]` on by default

warning: 1 warning emitted

Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ fn l<'a>(_: &'a str, _: &'a str) -> &str { "" }

// This is ok because both `'a` are for the same parameter.
fn m<'a>(_: &'a Foo<'a>) -> &str { "" }
//~^ WARNING elided lifetime has a name

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ help: consider using the `'a` lifetime
LL | fn l<'a>(_: &'a str, _: &'a str) -> &'a str { "" }
| ++

error: aborting due to 7 previous errors
warning: elided lifetime has a name
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:49:29
|
LL | fn m<'a>(_: &'a Foo<'a>) -> &str { "" }
| -- ^ this elided lifetime gets resolved as `'a`
| |
| lifetime `'a` declared here
|
= note: `#[warn(elided_named_lifetimes)]` on by default

error: aborting due to 7 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0106`.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct Foo {

impl Foo {
fn foo<'a>(&'a self, x: &i32) -> &i32 {
//~^ WARNING elided lifetime has a name

if true { &self.field } else { x } //~ ERROR explicit lifetime

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
warning: elided lifetime has a name
--> $DIR/ex1-return-one-existing-name-if-else-using-impl-3.rs:6:36
|
LL | fn foo<'a>(&'a self, x: &i32) -> &i32 {
| -- ^ this elided lifetime gets resolved as `'a`
| |
| lifetime `'a` declared here
|
= note: `#[warn(elided_named_lifetimes)]` on by default

error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/ex1-return-one-existing-name-if-else-using-impl-3.rs:8:36
--> $DIR/ex1-return-one-existing-name-if-else-using-impl-3.rs:9:36
|
LL | fn foo<'a>(&'a self, x: &i32) -> &i32 {
| ---- help: add explicit lifetime `'a` to the type of `x`: `&'a i32`
LL |
...
LL | if true { &self.field } else { x }
| ^ lifetime `'a` required

error: aborting due to 1 previous error
error: aborting due to 1 previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0621`.
12 changes: 12 additions & 0 deletions tests/ui/lint/elided-named-lifetimes/example-from-issue48686.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![deny(elided_named_lifetimes)]

struct Foo;

impl Foo {
pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
//~^ ERROR elided lifetime has a name
unsafe { &mut *(x as *mut _) }
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: elided lifetime has a name
--> $DIR/example-from-issue48686.rs:6:50
|
LL | pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
| ^ this elided lifetime gets resolved as `'static`
|
note: the lint level is defined here
--> $DIR/example-from-issue48686.rs:1:9
|
LL | #![deny(elided_named_lifetimes)]
| ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

27 changes: 27 additions & 0 deletions tests/ui/lint/elided-named-lifetimes/missing-lifetime-kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![deny(elided_named_lifetimes)]

fn ampersand<'a>(x: &'a u8) -> &u8 {
//~^ ERROR elided lifetime has a name
x
}

struct Brackets<'a>(&'a u8);

fn brackets<'a>(x: &'a u8) -> Brackets {
//~^ ERROR elided lifetime has a name
Brackets(x)
}

struct Comma<'a, T>(&'a T);

fn comma<'a>(x: &'a u8) -> Comma<u8> {
//~^ ERROR elided lifetime has a name
Comma(x)
}

fn underscore<'a>(x: &'a u8) -> &'_ u8 {
//~^ ERROR elided lifetime has a name
x
}

fn main() {}
Loading

0 comments on commit a9b959a

Please sign in to comment.