Skip to content

Fixes lots of issues in needless_lifetimes #13141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion tests/ui/needless_lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,81 @@ mod issue5787 {
}
}

fn main() {}
/// In this test; clippy failed to correctly elide
/// lifetimes within the iterator definition
mod issue11291 {
use std::collections::HashMap;

pub struct MyContainer(HashMap<u8, u32>);

impl MyContainer {
pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a u8, &'a u32)> + 'a {
self.0.iter()
}
}
}

/// this issue suggests eliding lifetimes in
/// impl blocks.
mod issue12424 {
mod should_lint {
struct SampleType<'a> {
foo:& 'a i32
}

impl<'a> SampleType<'a> {}
}

mod should_not_lint {
struct SampleType<'a> {
foo:& 'a i32
}

impl<'a> SampleType<'a> {
fn foo(bar: &'a i32) {
todo!()
}
}
}
}

/// For this issue, we want to config clippy as to whether
/// it should lint when having multiple lifetimes where one
/// could be elided.
mod issue12495 {
mod should_lint {
struct Test<'a, 'b> {
alpha: &'a i32,
beta: &'b i32,
}

fn test<'a, 'b>(test: Test<'a, 'b>) -> &'a i32 {
test.alpha
}
}

mod should_not_lint {
struct Test<'a, 'b> {
alpha: &'a i32,
beta: &'b i32,
}
fn test<'a, 'b>(test: Test<'a, 'b>) -> &'a i32 {
test.alpha
}
}
}

/// in this case clippy removed lifetimes that were actually
/// required
fn issue12908<'a>(s: &'a str) {
let _ = || {
let _: &'a str;
};
}

/// in this case, clippy should have elided 's,
/// but instead it produced something that was unparsable
/// fn test'_>(&self) -> impl Iterator<Item = i32> + 's;
pub trait Issue12789 {
fn test<'s>(&'s self) -> impl Iterator<Item = i32> + 's;
}
62 changes: 61 additions & 1 deletion tests/ui/needless_lifetimes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -553,5 +553,65 @@ 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: the following explicit lifetimes could be elided: 'a
--> tests/ui/needless_lifetimes.rs:554:21
|
LL | pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a u8, &'a u32)> + 'a {
| ^^ ^^ ^^
|
help: elide the lifetimes
|
LL - pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a u8, &'a u32)> + 'a {
LL + pub fn iter(&self) -> impl Iterator<Item = (&'_ u8, &'a u32)> + 'a {
|

error: the following explicit lifetimes could be elided: 'b
--> tests/ui/needless_lifetimes.rs:594:21
|
LL | fn test<'a, 'b>(test: Test<'a, 'b>) -> &'a i32 {
| ^^ ^^
|
help: elide the lifetimes
|
LL - fn test<'a, 'b>(test: Test<'a, 'b>) -> &'a i32 {
LL + fn test<'a>(test: Test<'a, '_>) -> &'a i32 {
|

error: the following explicit lifetimes could be elided: 'b
--> tests/ui/needless_lifetimes.rs:604:21
|
LL | fn test<'a, 'b>(test: Test<'a, 'b>) -> &'a i32 {
| ^^ ^^
|
help: elide the lifetimes
|
LL - fn test<'a, 'b>(test: Test<'a, 'b>) -> &'a i32 {
LL + fn test<'a>(test: Test<'a, '_>) -> &'a i32 {
|

error: the following explicit lifetimes could be elided: 'a
--> tests/ui/needless_lifetimes.rs:612:15
|
LL | fn issue12908<'a>(s: &'a str) {
| ^^ ^^
|
help: elide the lifetimes
|
LL - fn issue12908<'a>(s: &'a str) {
LL + fn issue12908(s: &str) {
|

error: the following explicit lifetimes could be elided: 's
--> tests/ui/needless_lifetimes.rs:622:13
|
LL | fn test<'s>(&'s self) -> impl Iterator<Item = i32> + 's;
| ^^ ^^
|
help: elide the lifetimes
|
LL - fn test<'s>(&'s self) -> impl Iterator<Item = i32> + 's;
LL + fn test'_>(&self) -> impl Iterator<Item = i32> + 's;
|

error: aborting due to 51 previous errors

Loading