diff --git a/tests/ui/needless_lifetimes.rs b/tests/ui/needless_lifetimes.rs index 03d6f2013586..0f31fb4a6dbf 100644 --- a/tests/ui/needless_lifetimes.rs +++ b/tests/ui/needless_lifetimes.rs @@ -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); + + impl MyContainer { + pub fn iter<'a>(&'a self) -> impl Iterator + '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 + 's; +pub trait Issue12789 { + fn test<'s>(&'s self) -> impl Iterator + 's; +} diff --git a/tests/ui/needless_lifetimes.stderr b/tests/ui/needless_lifetimes.stderr index f325d27b8c75..e0ac8ed5018a 100644 --- a/tests/ui/needless_lifetimes.stderr +++ b/tests/ui/needless_lifetimes.stderr @@ -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 + 'a { + | ^^ ^^ ^^ + | +help: elide the lifetimes + | +LL - pub fn iter<'a>(&'a self) -> impl Iterator + 'a { +LL + pub fn iter(&self) -> impl Iterator + '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 + 's; + | ^^ ^^ + | +help: elide the lifetimes + | +LL - fn test<'s>(&'s self) -> impl Iterator + 's; +LL + fn test'_>(&self) -> impl Iterator + 's; + | + +error: aborting due to 51 previous errors