diff --git a/CHANGELOG.md b/CHANGELOG.md index ceb08eeac3ff..f3e717889904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5548,6 +5548,7 @@ Released 2018-09-13 [`duplicated_attributes`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicated_attributes [`duration_subsec`]: https://rust-lang.github.io/rust-clippy/master/index.html#duration_subsec [`eager_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#eager_transmute +[`elidable_lifetime_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#elidable_lifetime_names [`else_if_without_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#else_if_without_else [`empty_docs`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_docs [`empty_drop`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_drop diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 5e6cfd94283c..cecccfa425c5 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -271,6 +271,7 @@ pub static LINTS: &[&crate::LintInfo] = &[ crate::let_underscore::LET_UNDERSCORE_MUST_USE_INFO, crate::let_underscore::LET_UNDERSCORE_UNTYPED_INFO, crate::let_with_type_underscore::LET_WITH_TYPE_UNDERSCORE_INFO, + crate::lifetimes::ELIDABLE_LIFETIME_NAMES_INFO, crate::lifetimes::EXTRA_UNUSED_LIFETIMES_INFO, crate::lifetimes::NEEDLESS_LIFETIMES_INFO, crate::lines_filter_map_ok::LINES_FILTER_MAP_OK_INFO, diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index 540d8b26f8e5..85d4a04ae60d 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -63,6 +63,40 @@ declare_clippy_lint! { would allow omitting them" } +declare_clippy_lint! { + /// ### What it does + /// Checks for lifetime annotations which can be replaced with anonymous lifetimes (`'_`). + /// + /// ### Why is this bad? + /// The additional lifetimes can make the code look more complicated. + /// + /// ### Known problems + /// - We bail out if the function has a `where` clause where lifetimes + /// are mentioned due to potential false positives. + /// + /// ### Example + /// ```no_run + /// use std::str::Chars; + /// + /// fn f<'a>(x: &'a str) -> Chars<'a> { + /// x.chars() + /// } + /// ``` + /// + /// Use instead: + /// ```no_run + /// use std::str::Chars; + /// + /// fn f(x: &str) -> Chars<'_> { + /// x.chars() + /// } + /// ``` + #[clippy::version = "1.84.0"] + pub ELIDABLE_LIFETIME_NAMES, + pedantic, + "lifetime name that can be replaced with the anonymous lifetime" +} + declare_clippy_lint! { /// ### What it does /// Checks for lifetimes in generics that are never used @@ -105,7 +139,11 @@ impl Lifetimes { } } -impl_lint_pass!(Lifetimes => [NEEDLESS_LIFETIMES, EXTRA_UNUSED_LIFETIMES]); +impl_lint_pass!(Lifetimes => [ + NEEDLESS_LIFETIMES, + ELIDABLE_LIFETIME_NAMES, + EXTRA_UNUSED_LIFETIMES, +]); impl<'tcx> LateLintPass<'tcx> for Lifetimes { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { @@ -747,6 +785,15 @@ fn report_elidable_impl_lifetimes<'tcx>( report_elidable_lifetimes(cx, impl_.generics, &elidable_lts, &usages, true); } +#[derive(Copy, Clone)] +enum ElidableUsage { + /// Used in a ref (`&'a T`), can be removed + Ref(Span), + /// Used as a generic param (`T<'a>`) or an impl lifetime (`impl T + 'a`), can be replaced + /// with `'_` + Other(Span), +} + /// Generate diagnostic messages for elidable lifetimes. fn report_elidable_lifetimes( cx: &LateContext<'_>, @@ -764,9 +811,29 @@ fn report_elidable_lifetimes( .collect::>() .join(", "); + let elidable_usages: Vec = usages + .iter() + .filter(|usage| named_lifetime(usage).is_some_and(|id| elidable_lts.contains(&id))) + .map(|usage| match cx.tcx.parent_hir_node(usage.hir_id) { + Node::Ty(Ty { + kind: TyKind::Ref(..), .. + }) => ElidableUsage::Ref(usage.ident.span), + _ => ElidableUsage::Other(usage.ident.span), + }) + .collect(); + + let lint = if elidable_usages + .iter() + .any(|usage| matches!(usage, ElidableUsage::Other(_))) + { + ELIDABLE_LIFETIME_NAMES + } else { + NEEDLESS_LIFETIMES + }; + span_lint_and_then( cx, - NEEDLESS_LIFETIMES, + lint, elidable_lts .iter() .map(|<| cx.tcx.def_span(lt)) @@ -786,7 +853,7 @@ fn report_elidable_lifetimes( return; }; - if let Some(suggestions) = elision_suggestions(cx, generics, elidable_lts, usages) { + if let Some(suggestions) = elision_suggestions(cx, generics, elidable_lts, &elidable_usages) { diag.multipart_suggestion("elide the lifetimes", suggestions, Applicability::MachineApplicable); } }, @@ -797,7 +864,7 @@ fn elision_suggestions( cx: &LateContext<'_>, generics: &Generics<'_>, elidable_lts: &[LocalDefId], - usages: &[Lifetime], + usages: &[ElidableUsage], ) -> Option> { let explicit_params = generics .params @@ -837,26 +904,21 @@ fn elision_suggestions( .collect::>>()? }; - suggestions.extend( - usages - .iter() - .filter(|usage| named_lifetime(usage).is_some_and(|id| elidable_lts.contains(&id))) - .map(|usage| { - match cx.tcx.parent_hir_node(usage.hir_id) { - Node::Ty(Ty { - kind: TyKind::Ref(..), .. - }) => { - // expand `&'a T` to `&'a T` - // ^^ ^^^ - let span = cx.sess().source_map().span_extend_while_whitespace(usage.ident.span); - - (span, String::new()) - }, - // `T<'a>` and `impl Foo + 'a` should be replaced by `'_` - _ => (usage.ident.span, String::from("'_")), - } - }), - ); + suggestions.extend(usages.iter().map(|&usage| { + match usage { + ElidableUsage::Ref(span) => { + // expand `&'a T` to `&'a T` + // ^^ ^^^ + let span = cx.sess().source_map().span_extend_while_whitespace(span); + + (span, String::new()) + }, + ElidableUsage::Other(span) => { + // `T<'a>` and `impl Foo + 'a` should be replaced by `'_` + (span, String::from("'_")) + }, + } + })); Some(suggestions) } diff --git a/tests/ui/crashes/needless_lifetimes_impl_trait.fixed b/tests/ui/crashes/elidable_lifetime_names_impl_trait.fixed similarity index 86% rename from tests/ui/crashes/needless_lifetimes_impl_trait.fixed rename to tests/ui/crashes/elidable_lifetime_names_impl_trait.fixed index 837069cae6d1..dd065b1e7e71 100644 --- a/tests/ui/crashes/needless_lifetimes_impl_trait.fixed +++ b/tests/ui/crashes/elidable_lifetime_names_impl_trait.fixed @@ -1,4 +1,4 @@ -#![deny(clippy::needless_lifetimes)] +#![deny(clippy::elidable_lifetime_names)] #![allow(dead_code)] trait Foo {} diff --git a/tests/ui/crashes/needless_lifetimes_impl_trait.rs b/tests/ui/crashes/elidable_lifetime_names_impl_trait.rs similarity index 87% rename from tests/ui/crashes/needless_lifetimes_impl_trait.rs rename to tests/ui/crashes/elidable_lifetime_names_impl_trait.rs index 06947e3a3513..197fcbbe08a3 100644 --- a/tests/ui/crashes/needless_lifetimes_impl_trait.rs +++ b/tests/ui/crashes/elidable_lifetime_names_impl_trait.rs @@ -1,4 +1,4 @@ -#![deny(clippy::needless_lifetimes)] +#![deny(clippy::elidable_lifetime_names)] #![allow(dead_code)] trait Foo {} diff --git a/tests/ui/crashes/needless_lifetimes_impl_trait.stderr b/tests/ui/crashes/elidable_lifetime_names_impl_trait.stderr similarity index 67% rename from tests/ui/crashes/needless_lifetimes_impl_trait.stderr rename to tests/ui/crashes/elidable_lifetime_names_impl_trait.stderr index bed6aab25c4d..dd4b01c86649 100644 --- a/tests/ui/crashes/needless_lifetimes_impl_trait.stderr +++ b/tests/ui/crashes/elidable_lifetime_names_impl_trait.stderr @@ -1,14 +1,14 @@ error: the following explicit lifetimes could be elided: 'a - --> tests/ui/crashes/needless_lifetimes_impl_trait.rs:12:6 + --> tests/ui/crashes/elidable_lifetime_names_impl_trait.rs:12:6 | LL | impl<'a> Foo for Baz<'a> {} | ^^ ^^ | note: the lint level is defined here - --> tests/ui/crashes/needless_lifetimes_impl_trait.rs:1:9 + --> tests/ui/crashes/elidable_lifetime_names_impl_trait.rs:1:9 | -LL | #![deny(clippy::needless_lifetimes)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | #![deny(clippy::elidable_lifetime_names)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: elide the lifetimes | LL - impl<'a> Foo for Baz<'a> {} @@ -16,7 +16,7 @@ LL + impl Foo for Baz<'_> {} | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/crashes/needless_lifetimes_impl_trait.rs:15:12 + --> tests/ui/crashes/elidable_lifetime_names_impl_trait.rs:15:12 | LL | fn baz<'a>(&'a self) -> impl Foo + 'a { | ^^ ^^ ^^ diff --git a/tests/ui/elidable_lifetime_names.fixed b/tests/ui/elidable_lifetime_names.fixed new file mode 100644 index 000000000000..171f3d228895 --- /dev/null +++ b/tests/ui/elidable_lifetime_names.fixed @@ -0,0 +1,189 @@ +#![warn(clippy::needless_lifetimes, clippy::elidable_lifetime_names)] + +type Ref<'r> = &'r u8; + +// No error; same lifetime on two params. +fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {} + +fn lifetime_param_2(_x: Ref<'_>, _y: &u8) {} + +// No error; bounded lifetime. +fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {} + +// No error; bounded lifetime. +fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8) +where + 'b: 'a, +{ +} + +struct Lt<'a, I: 'static> { + x: &'a I, +} + +// No error; fn bound references `'a`. +fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> +where + F: Fn(Lt<'a, I>) -> Lt<'a, I>, +{ + unreachable!() +} + +fn fn_bound_2(_m: Lt<'_, I>, _f: F) -> Lt<'_, I> +where + for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>, +{ + unreachable!() +} + +struct Foo<'a>(&'a u8); + +fn struct_with_lt(_foo: Foo<'_>) -> &str { + unimplemented!() +} + +// No warning; two input lifetimes (named on the reference, anonymous on `Foo`). +fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str { + unimplemented!() +} + +// No warning; two input lifetimes (anonymous on the reference, named on `Foo`). +fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str { + unimplemented!() +} + +// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is +// valid: +// fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str +// ^^ +fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str { + unimplemented!() +} + +type FooAlias<'a> = Foo<'a>; + +fn alias_with_lt(_foo: FooAlias<'_>) -> &str { + unimplemented!() +} + +// No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`). +fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str { + unimplemented!() +} + +// No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`). +fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str { + unimplemented!() +} + +// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is +// valid: +// fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str +// ^^ +fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str { + unimplemented!() +} + +// Issue #3284: give hint regarding lifetime in return type. +struct Cow<'a> { + x: &'a str, +} +fn out_return_type_lts(e: &str) -> Cow<'_> { + unimplemented!() +} + +mod issue2944 { + trait Foo {} + struct Bar; + struct Baz<'a> { + bar: &'a Bar, + } + + impl Foo for Baz<'_> {} + impl Bar { + fn baz(&self) -> impl Foo + '_ { + Baz { bar: self } + } + } +} + +mod issue13923 { + struct Py<'py> { + data: &'py str, + } + + enum Content<'t, 'py> { + Py(Py<'py>), + T1(&'t str), + T2(&'t str), + } + + enum ContentString<'t> { + T1(&'t str), + T2(&'t str), + } + + impl<'t, 'py> ContentString<'t> { + // `'py` cannot be elided + fn map_content1(self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { + match self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(content) => Content::T2(f(content)), + } + } + } + + impl<'t> ContentString<'t> { + // `'py` can be elided because of `&self` + fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> { + match self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(content) => Content::T2(f(content)), + } + } + } + + impl<'t> ContentString<'t> { + // `'py` can be elided because of `&'_ self` + fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> { + match self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(content) => Content::T2(f(content)), + } + } + } + + impl<'t, 'py> ContentString<'t> { + // `'py` should not be elided as the default lifetime, even if working, could be named as `'t` + fn map_content4(self, f: impl FnOnce(&'t str) -> &'t str, o: &'t str) -> Content<'t, 'py> { + match self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(_) => Content::T2(o), + } + } + } + + impl<'t> ContentString<'t> { + // `'py` can be elided because of `&Self` + fn map_content5( + self: std::pin::Pin<&Self>, + f: impl FnOnce(&'t str) -> &'t str, + o: &'t str, + ) -> Content<'t, '_> { + match *self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(_) => Content::T2(o), + } + } + } + + struct Cx<'a, 'b> { + a: &'a u32, + b: &'b u32, + } + + // `'c` cannot be elided because we have several input lifetimes + fn one_explicit<'b>(x: Cx<'_, 'b>) -> &'b u32 { + x.b + } +} diff --git a/tests/ui/elidable_lifetime_names.rs b/tests/ui/elidable_lifetime_names.rs new file mode 100644 index 000000000000..0416633c9604 --- /dev/null +++ b/tests/ui/elidable_lifetime_names.rs @@ -0,0 +1,189 @@ +#![warn(clippy::needless_lifetimes, clippy::elidable_lifetime_names)] + +type Ref<'r> = &'r u8; + +// No error; same lifetime on two params. +fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {} + +fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {} + +// No error; bounded lifetime. +fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {} + +// No error; bounded lifetime. +fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8) +where + 'b: 'a, +{ +} + +struct Lt<'a, I: 'static> { + x: &'a I, +} + +// No error; fn bound references `'a`. +fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> +where + F: Fn(Lt<'a, I>) -> Lt<'a, I>, +{ + unreachable!() +} + +fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> +where + for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>, +{ + unreachable!() +} + +struct Foo<'a>(&'a u8); + +fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { + unimplemented!() +} + +// No warning; two input lifetimes (named on the reference, anonymous on `Foo`). +fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str { + unimplemented!() +} + +// No warning; two input lifetimes (anonymous on the reference, named on `Foo`). +fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str { + unimplemented!() +} + +// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is +// valid: +// fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str +// ^^ +fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { + unimplemented!() +} + +type FooAlias<'a> = Foo<'a>; + +fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { + unimplemented!() +} + +// No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`). +fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str { + unimplemented!() +} + +// No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`). +fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str { + unimplemented!() +} + +// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is +// valid: +// fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str +// ^^ +fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { + unimplemented!() +} + +// Issue #3284: give hint regarding lifetime in return type. +struct Cow<'a> { + x: &'a str, +} +fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { + unimplemented!() +} + +mod issue2944 { + trait Foo {} + struct Bar; + struct Baz<'a> { + bar: &'a Bar, + } + + impl<'a> Foo for Baz<'a> {} + impl Bar { + fn baz<'a>(&'a self) -> impl Foo + 'a { + Baz { bar: self } + } + } +} + +mod issue13923 { + struct Py<'py> { + data: &'py str, + } + + enum Content<'t, 'py> { + Py(Py<'py>), + T1(&'t str), + T2(&'t str), + } + + enum ContentString<'t> { + T1(&'t str), + T2(&'t str), + } + + impl<'t, 'py> ContentString<'t> { + // `'py` cannot be elided + fn map_content1(self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { + match self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(content) => Content::T2(f(content)), + } + } + } + + impl<'t, 'py> ContentString<'t> { + // `'py` can be elided because of `&self` + fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { + match self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(content) => Content::T2(f(content)), + } + } + } + + impl<'t, 'py> ContentString<'t> { + // `'py` can be elided because of `&'_ self` + fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { + match self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(content) => Content::T2(f(content)), + } + } + } + + impl<'t, 'py> ContentString<'t> { + // `'py` should not be elided as the default lifetime, even if working, could be named as `'t` + fn map_content4(self, f: impl FnOnce(&'t str) -> &'t str, o: &'t str) -> Content<'t, 'py> { + match self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(_) => Content::T2(o), + } + } + } + + impl<'t, 'py> ContentString<'t> { + // `'py` can be elided because of `&Self` + fn map_content5( + self: std::pin::Pin<&Self>, + f: impl FnOnce(&'t str) -> &'t str, + o: &'t str, + ) -> Content<'t, 'py> { + match *self { + Self::T1(content) => Content::T1(f(content)), + Self::T2(_) => Content::T2(o), + } + } + } + + struct Cx<'a, 'b> { + a: &'a u32, + b: &'b u32, + } + + // `'c` cannot be elided because we have several input lifetimes + fn one_explicit<'b>(x: Cx<'_, 'b>) -> &'b u32 { + x.b + } +} diff --git a/tests/ui/elidable_lifetime_names.stderr b/tests/ui/elidable_lifetime_names.stderr new file mode 100644 index 000000000000..2cb52058ff0a --- /dev/null +++ b/tests/ui/elidable_lifetime_names.stderr @@ -0,0 +1,162 @@ +error: the following explicit lifetimes could be elided: 'a, 'b + --> tests/ui/elidable_lifetime_names.rs:8:21 + | +LL | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {} + | ^^ ^^ ^^ ^^ + | + = note: `-D clippy::elidable-lifetime-names` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::elidable_lifetime_names)]` +help: elide the lifetimes + | +LL - fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {} +LL + fn lifetime_param_2(_x: Ref<'_>, _y: &u8) {} + | + +error: the following explicit lifetimes could be elided: 'a + --> tests/ui/elidable_lifetime_names.rs:32:15 + | +LL | fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> + | ^^ ^^ ^^ + | +help: elide the lifetimes + | +LL - fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> +LL + fn fn_bound_2(_m: Lt<'_, I>, _f: F) -> Lt<'_, I> + | + +error: the following explicit lifetimes could be elided: 'a + --> tests/ui/elidable_lifetime_names.rs:41:19 + | +LL | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { + | ^^ ^^ ^^ + | +help: elide the lifetimes + | +LL - fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { +LL + fn struct_with_lt(_foo: Foo<'_>) -> &str { + | + +error: the following explicit lifetimes could be elided: 'b + --> tests/ui/elidable_lifetime_names.rs:59:25 + | +LL | fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { + | ^^ ^^ + | +help: elide the lifetimes + | +LL - fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { +LL + fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str { + | + +error: the following explicit lifetimes could be elided: 'a + --> tests/ui/elidable_lifetime_names.rs:65:18 + | +LL | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { + | ^^ ^^ ^^ + | +help: elide the lifetimes + | +LL - fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { +LL + fn alias_with_lt(_foo: FooAlias<'_>) -> &str { + | + +error: the following explicit lifetimes could be elided: 'b + --> tests/ui/elidable_lifetime_names.rs:83:24 + | +LL | fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { + | ^^ ^^ + | +help: elide the lifetimes + | +LL - fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { +LL + fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str { + | + +error: the following explicit lifetimes could be elided: 'a + --> tests/ui/elidable_lifetime_names.rs:91:24 + | +LL | fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { + | ^^ ^^ ^^ + | +help: elide the lifetimes + | +LL - fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { +LL + fn out_return_type_lts(e: &str) -> Cow<'_> { + | + +error: the following explicit lifetimes could be elided: 'a + --> tests/ui/elidable_lifetime_names.rs:102:10 + | +LL | impl<'a> Foo for Baz<'a> {} + | ^^ ^^ + | +help: elide the lifetimes + | +LL - impl<'a> Foo for Baz<'a> {} +LL + impl Foo for Baz<'_> {} + | + +error: the following explicit lifetimes could be elided: 'a + --> tests/ui/elidable_lifetime_names.rs:104:16 + | +LL | fn baz<'a>(&'a self) -> impl Foo + 'a { + | ^^ ^^ ^^ + | +help: elide the lifetimes + | +LL - fn baz<'a>(&'a self) -> impl Foo + 'a { +LL + fn baz(&self) -> impl Foo + '_ { + | + +error: the following explicit lifetimes could be elided: 'py + --> tests/ui/elidable_lifetime_names.rs:136:14 + | +LL | impl<'t, 'py> ContentString<'t> { + | ^^^ +LL | // `'py` can be elided because of `&self` +LL | fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { + | ^^^ + | +help: elide the lifetimes + | +LL ~ impl<'t> ContentString<'t> { +LL | // `'py` can be elided because of `&self` +LL ~ fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> { + | + +error: the following explicit lifetimes could be elided: 'py + --> tests/ui/elidable_lifetime_names.rs:146:14 + | +LL | impl<'t, 'py> ContentString<'t> { + | ^^^ +LL | // `'py` can be elided because of `&'_ self` +LL | fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { + | ^^^ + | +help: elide the lifetimes + | +LL ~ impl<'t> ContentString<'t> { +LL | // `'py` can be elided because of `&'_ self` +LL ~ fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> { + | + +error: the following explicit lifetimes could be elided: 'py + --> tests/ui/elidable_lifetime_names.rs:166:14 + | +LL | impl<'t, 'py> ContentString<'t> { + | ^^^ +... +LL | ) -> Content<'t, 'py> { + | ^^^ + | +help: elide the lifetimes + | +LL ~ impl<'t> ContentString<'t> { +LL | // `'py` can be elided because of `&Self` +... +LL | o: &'t str, +LL ~ ) -> Content<'t, '_> { + | + +error: aborting due to 12 previous errors + diff --git a/tests/ui/methods.rs b/tests/ui/methods.rs index cb1f695c651c..38e8e0269384 100644 --- a/tests/ui/methods.rs +++ b/tests/ui/methods.rs @@ -11,6 +11,7 @@ clippy::new_without_default, clippy::needless_pass_by_value, clippy::needless_lifetimes, + clippy::elidable_lifetime_names, clippy::print_stdout, clippy::must_use_candidate, clippy::use_self, diff --git a/tests/ui/methods.stderr b/tests/ui/methods.stderr index 2e2bd3d874f5..26a2e823a539 100644 --- a/tests/ui/methods.stderr +++ b/tests/ui/methods.stderr @@ -1,5 +1,5 @@ error: methods called `new` usually return `Self` - --> tests/ui/methods.rs:103:5 + --> tests/ui/methods.rs:104:5 | LL | / fn new() -> i32 { LL | | 0 @@ -10,7 +10,7 @@ LL | | } = help: to override `-D warnings` add `#[allow(clippy::new_ret_no_self)]` error: called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(..)` instead - --> tests/ui/methods.rs:124:13 + --> tests/ui/methods.rs:125:13 | LL | let _ = v.iter().filter(|&x| { | _____________^ diff --git a/tests/ui/needless_lifetimes.fixed b/tests/ui/needless_lifetimes.fixed index 86cf9a9cdb63..6ab68976ab3a 100644 --- a/tests/ui/needless_lifetimes.fixed +++ b/tests/ui/needless_lifetimes.fixed @@ -1,6 +1,6 @@ //@aux-build:proc_macros.rs -#![warn(clippy::needless_lifetimes)] +#![warn(clippy::needless_lifetimes, clippy::elidable_lifetime_names)] #![allow( unused, clippy::boxed_local, @@ -91,42 +91,6 @@ where Ok(x) } -type Ref<'r> = &'r u8; - -// No error; same lifetime on two params. -fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {} - -fn lifetime_param_2(_x: Ref<'_>, _y: &u8) {} - -// No error; bounded lifetime. -fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {} - -// No error; bounded lifetime. -fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8) -where - 'b: 'a, -{ -} - -struct Lt<'a, I: 'static> { - x: &'a I, -} - -// No error; fn bound references `'a`. -fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> -where - F: Fn(Lt<'a, I>) -> Lt<'a, I>, -{ - unreachable!() -} - -fn fn_bound_2(_m: Lt<'_, I>, _f: F) -> Lt<'_, I> -where - for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>, -{ - unreachable!() -} - // No error; see below. fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) { f(x); @@ -187,28 +151,6 @@ fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 { unimplemented!() } -fn struct_with_lt(_foo: Foo<'_>) -> &str { - unimplemented!() -} - -// No warning; two input lifetimes (named on the reference, anonymous on `Foo`). -fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str { - unimplemented!() -} - -// No warning; two input lifetimes (anonymous on the reference, named on `Foo`). -fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str { - unimplemented!() -} - -// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is -// valid: -// fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str -// ^^ -fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str { - unimplemented!() -} - // Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is // valid: // fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str @@ -234,28 +176,6 @@ fn trait_obj_elided2(_arg: &dyn Drop) -> &str { type FooAlias<'a> = Foo<'a>; -fn alias_with_lt(_foo: FooAlias<'_>) -> &str { - unimplemented!() -} - -// No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`). -fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str { - unimplemented!() -} - -// No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`). -fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str { - unimplemented!() -} - -// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is -// valid: -// fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str -// ^^ -fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str { - unimplemented!() -} - // Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is // valid: // fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str @@ -304,14 +224,6 @@ fn test<'a>(x: &'a [u8]) -> u8 { *y } -// Issue #3284: give hint regarding lifetime in return type. -struct Cow<'a> { - x: &'a str, -} -fn out_return_type_lts(e: &str) -> Cow<'_> { - unimplemented!() -} - // Make sure we still warn on implementations mod issue4291 { trait BadTrait { @@ -323,21 +235,6 @@ mod issue4291 { } } -mod issue2944 { - trait Foo {} - struct Bar; - struct Baz<'a> { - bar: &'a Bar, - } - - impl Foo for Baz<'_> {} - impl Bar { - fn baz(&self) -> impl Foo + '_ { - Baz { bar: self } - } - } -} - mod nested_elision_sites { // issue #issue2944 @@ -594,85 +491,4 @@ mod issue13749bis { impl<'a, T: 'a> Generic {} } -mod issue13923 { - struct Py<'py> { - data: &'py str, - } - - enum Content<'t, 'py> { - Py(Py<'py>), - T1(&'t str), - T2(&'t str), - } - - enum ContentString<'t> { - T1(&'t str), - T2(&'t str), - } - - impl<'t, 'py> ContentString<'t> { - // `'py` cannot be elided - fn map_content1(self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { - match self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(content) => Content::T2(f(content)), - } - } - } - - impl<'t> ContentString<'t> { - // `'py` can be elided because of `&self` - fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> { - match self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(content) => Content::T2(f(content)), - } - } - } - - impl<'t> ContentString<'t> { - // `'py` can be elided because of `&'_ self` - fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> { - match self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(content) => Content::T2(f(content)), - } - } - } - - impl<'t, 'py> ContentString<'t> { - // `'py` should not be elided as the default lifetime, even if working, could be named as `'t` - fn map_content4(self, f: impl FnOnce(&'t str) -> &'t str, o: &'t str) -> Content<'t, 'py> { - match self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(_) => Content::T2(o), - } - } - } - - impl<'t> ContentString<'t> { - // `'py` can be elided because of `&Self` - fn map_content5( - self: std::pin::Pin<&Self>, - f: impl FnOnce(&'t str) -> &'t str, - o: &'t str, - ) -> Content<'t, '_> { - match *self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(_) => Content::T2(o), - } - } - } - - struct Cx<'a, 'b> { - a: &'a u32, - b: &'b u32, - } - - // `'c` cannot be elided because we have several input lifetimes - fn one_explicit<'b>(x: Cx<'_, 'b>) -> &'b u32 { - x.b - } -} - fn main() {} diff --git a/tests/ui/needless_lifetimes.rs b/tests/ui/needless_lifetimes.rs index 1ee0f4c6092a..ff3b17ea2b07 100644 --- a/tests/ui/needless_lifetimes.rs +++ b/tests/ui/needless_lifetimes.rs @@ -1,6 +1,6 @@ //@aux-build:proc_macros.rs -#![warn(clippy::needless_lifetimes)] +#![warn(clippy::needless_lifetimes, clippy::elidable_lifetime_names)] #![allow( unused, clippy::boxed_local, @@ -91,42 +91,6 @@ where Ok(x) } -type Ref<'r> = &'r u8; - -// No error; same lifetime on two params. -fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {} - -fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {} - -// No error; bounded lifetime. -fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {} - -// No error; bounded lifetime. -fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8) -where - 'b: 'a, -{ -} - -struct Lt<'a, I: 'static> { - x: &'a I, -} - -// No error; fn bound references `'a`. -fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> -where - F: Fn(Lt<'a, I>) -> Lt<'a, I>, -{ - unreachable!() -} - -fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> -where - for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>, -{ - unreachable!() -} - // No error; see below. fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) { f(x); @@ -187,28 +151,6 @@ fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 { unimplemented!() } -fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { - unimplemented!() -} - -// No warning; two input lifetimes (named on the reference, anonymous on `Foo`). -fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str { - unimplemented!() -} - -// No warning; two input lifetimes (anonymous on the reference, named on `Foo`). -fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str { - unimplemented!() -} - -// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is -// valid: -// fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str -// ^^ -fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { - unimplemented!() -} - // Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is // valid: // fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str @@ -234,28 +176,6 @@ fn trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str { type FooAlias<'a> = Foo<'a>; -fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { - unimplemented!() -} - -// No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`). -fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str { - unimplemented!() -} - -// No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`). -fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str { - unimplemented!() -} - -// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is -// valid: -// fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str -// ^^ -fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { - unimplemented!() -} - // Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is // valid: // fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str @@ -304,14 +224,6 @@ fn test<'a>(x: &'a [u8]) -> u8 { *y } -// Issue #3284: give hint regarding lifetime in return type. -struct Cow<'a> { - x: &'a str, -} -fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { - unimplemented!() -} - // Make sure we still warn on implementations mod issue4291 { trait BadTrait { @@ -323,21 +235,6 @@ mod issue4291 { } } -mod issue2944 { - trait Foo {} - struct Bar; - struct Baz<'a> { - bar: &'a Bar, - } - - impl<'a> Foo for Baz<'a> {} - impl Bar { - fn baz<'a>(&'a self) -> impl Foo + 'a { - Baz { bar: self } - } - } -} - mod nested_elision_sites { // issue #issue2944 @@ -594,85 +491,4 @@ mod issue13749bis { impl<'a, T: 'a> Generic {} } -mod issue13923 { - struct Py<'py> { - data: &'py str, - } - - enum Content<'t, 'py> { - Py(Py<'py>), - T1(&'t str), - T2(&'t str), - } - - enum ContentString<'t> { - T1(&'t str), - T2(&'t str), - } - - impl<'t, 'py> ContentString<'t> { - // `'py` cannot be elided - fn map_content1(self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { - match self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(content) => Content::T2(f(content)), - } - } - } - - impl<'t, 'py> ContentString<'t> { - // `'py` can be elided because of `&self` - fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { - match self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(content) => Content::T2(f(content)), - } - } - } - - impl<'t, 'py> ContentString<'t> { - // `'py` can be elided because of `&'_ self` - fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { - match self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(content) => Content::T2(f(content)), - } - } - } - - impl<'t, 'py> ContentString<'t> { - // `'py` should not be elided as the default lifetime, even if working, could be named as `'t` - fn map_content4(self, f: impl FnOnce(&'t str) -> &'t str, o: &'t str) -> Content<'t, 'py> { - match self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(_) => Content::T2(o), - } - } - } - - impl<'t, 'py> ContentString<'t> { - // `'py` can be elided because of `&Self` - fn map_content5( - self: std::pin::Pin<&Self>, - f: impl FnOnce(&'t str) -> &'t str, - o: &'t str, - ) -> Content<'t, 'py> { - match *self { - Self::T1(content) => Content::T1(f(content)), - Self::T2(_) => Content::T2(o), - } - } - } - - struct Cx<'a, 'b> { - a: &'a u32, - b: &'b u32, - } - - // `'c` cannot be elided because we have several input lifetimes - fn one_explicit<'b>(x: Cx<'_, 'b>) -> &'b u32 { - &x.b - } -} - fn main() {} diff --git a/tests/ui/needless_lifetimes.stderr b/tests/ui/needless_lifetimes.stderr index 465d529bf16c..61bf4a8ca85c 100644 --- a/tests/ui/needless_lifetimes.stderr +++ b/tests/ui/needless_lifetimes.stderr @@ -1,5 +1,5 @@ error: elided lifetime has a name - --> tests/ui/needless_lifetimes.rs:267:52 + --> tests/ui/needless_lifetimes.rs:187:52 | LL | fn named_input_elided_output<'a>(_arg: &'a str) -> &str { | -- ^ this elided lifetime gets resolved as `'a` @@ -119,32 +119,8 @@ LL - fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> LL + fn where_clause_without_lt(x: &u8, _y: u8) -> Result<&u8, ()> | -error: the following explicit lifetimes could be elided: 'a, 'b - --> tests/ui/needless_lifetimes.rs:99:21 - | -LL | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {} - | ^^ ^^ ^^ ^^ - | -help: elide the lifetimes - | -LL - fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {} -LL + fn lifetime_param_2(_x: Ref<'_>, _y: &u8) {} - | - -error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:123:15 - | -LL | fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> - | ^^ ^^ ^^ - | -help: elide the lifetimes - | -LL - fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> -LL + fn fn_bound_2(_m: Lt<'_, I>, _f: F) -> Lt<'_, I> - | - error: the following explicit lifetimes could be elided: 's - --> tests/ui/needless_lifetimes.rs:153:21 + --> tests/ui/needless_lifetimes.rs:117:21 | LL | fn self_and_out<'s>(&'s self) -> &'s u8 { | ^^ ^^ ^^ @@ -156,7 +132,7 @@ LL + fn self_and_out(&self) -> &u8 { | error: the following explicit lifetimes could be elided: 't - --> tests/ui/needless_lifetimes.rs:160:30 + --> tests/ui/needless_lifetimes.rs:124:30 | LL | fn self_and_in_out_1<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 { | ^^ ^^ @@ -168,7 +144,7 @@ LL + fn self_and_in_out_1<'s>(&'s self, _x: &u8) -> &'s u8 { | error: the following explicit lifetimes could be elided: 's - --> tests/ui/needless_lifetimes.rs:167:26 + --> tests/ui/needless_lifetimes.rs:131:26 | LL | fn self_and_in_out_2<'s, 't>(&'s self, x: &'t u8) -> &'t u8 { | ^^ ^^ @@ -180,7 +156,7 @@ LL + fn self_and_in_out_2<'t>(&self, x: &'t u8) -> &'t u8 { | error: the following explicit lifetimes could be elided: 's, 't - --> tests/ui/needless_lifetimes.rs:171:29 + --> tests/ui/needless_lifetimes.rs:135:29 | LL | fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {} | ^^ ^^ ^^ ^^ @@ -192,31 +168,7 @@ LL + fn distinct_self_and_in(&self, _x: &u8) {} | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:190:19 - | -LL | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { - | ^^ ^^ ^^ - | -help: elide the lifetimes - | -LL - fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { -LL + fn struct_with_lt(_foo: Foo<'_>) -> &str { - | - -error: the following explicit lifetimes could be elided: 'b - --> tests/ui/needless_lifetimes.rs:208:25 - | -LL | fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { - | ^^ ^^ - | -help: elide the lifetimes - | -LL - fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { -LL + fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str { - | - -error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:216:21 + --> tests/ui/needless_lifetimes.rs:158:21 | LL | fn struct_with_lt4b<'a, 'b>(_foo: &'a Foo<'b>) -> &'b str { | ^^ ^^ @@ -228,7 +180,7 @@ LL + fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:231:22 + --> tests/ui/needless_lifetimes.rs:173:22 | LL | fn trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str { | ^^ ^^ ^^ @@ -240,31 +192,7 @@ LL + fn trait_obj_elided2(_arg: &dyn Drop) -> &str { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:237:18 - | -LL | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { - | ^^ ^^ ^^ - | -help: elide the lifetimes - | -LL - fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { -LL + fn alias_with_lt(_foo: FooAlias<'_>) -> &str { - | - -error: the following explicit lifetimes could be elided: 'b - --> tests/ui/needless_lifetimes.rs:255:24 - | -LL | fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { - | ^^ ^^ - | -help: elide the lifetimes - | -LL - fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { -LL + fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str { - | - -error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:263:20 + --> tests/ui/needless_lifetimes.rs:183:20 | LL | fn alias_with_lt4b<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'b str { | ^^ ^^ @@ -276,7 +204,7 @@ LL + fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:267:30 + --> tests/ui/needless_lifetimes.rs:187:30 | LL | fn named_input_elided_output<'a>(_arg: &'a str) -> &str { | ^^ ^^ ^ @@ -288,7 +216,7 @@ LL + fn named_input_elided_output(_arg: &str) -> &str { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:275:19 + --> tests/ui/needless_lifetimes.rs:195:19 | LL | fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) { | ^^ ^^ @@ -300,19 +228,7 @@ LL + fn trait_bound_ok>(_: &u8, _: T) { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:311:24 - | -LL | fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { - | ^^ ^^ ^^ - | -help: elide the lifetimes - | -LL - fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { -LL + fn out_return_type_lts(e: &str) -> Cow<'_> { - | - -error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:318:24 + --> tests/ui/needless_lifetimes.rs:230:24 | LL | fn needless_lt<'a>(x: &'a u8) {} | ^^ ^^ @@ -324,7 +240,7 @@ LL + fn needless_lt(x: &u8) {} | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:322:24 + --> tests/ui/needless_lifetimes.rs:234:24 | LL | fn needless_lt<'a>(_x: &'a u8) {} | ^^ ^^ @@ -336,31 +252,7 @@ LL + fn needless_lt(_x: &u8) {} | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:333:10 - | -LL | impl<'a> Foo for Baz<'a> {} - | ^^ ^^ - | -help: elide the lifetimes - | -LL - impl<'a> Foo for Baz<'a> {} -LL + impl Foo for Baz<'_> {} - | - -error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:335:16 - | -LL | fn baz<'a>(&'a self) -> impl Foo + 'a { - | ^^ ^^ ^^ - | -help: elide the lifetimes - | -LL - fn baz<'a>(&'a self) -> impl Foo + 'a { -LL + fn baz(&self) -> impl Foo + '_ { - | - -error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:367:55 + --> tests/ui/needless_lifetimes.rs:264:55 | LL | fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 { | ^^ ^^ ^^ @@ -372,7 +264,7 @@ LL + fn impl_trait_elidable_nested_anonymous_lifetimes(i: &i32, f: impl Fn(& | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:376:26 + --> tests/ui/needless_lifetimes.rs:273:26 | LL | fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 { | ^^ ^^ ^^ @@ -384,7 +276,7 @@ LL + fn generics_elidable &i32>(i: &i32, f: T) -> &i32 { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:388:30 + --> tests/ui/needless_lifetimes.rs:285:30 | LL | fn where_clause_elidable<'a, T>(i: &'a i32, f: T) -> &'a i32 | ^^ ^^ ^^ @@ -396,7 +288,7 @@ LL + fn where_clause_elidable(i: &i32, f: T) -> &i32 | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:403:28 + --> tests/ui/needless_lifetimes.rs:300:28 | LL | fn pointer_fn_elidable<'a>(i: &'a i32, f: fn(&i32) -> &i32) -> &'a i32 { | ^^ ^^ ^^ @@ -408,7 +300,7 @@ LL + fn pointer_fn_elidable(i: &i32, f: fn(&i32) -> &i32) -> &i32 { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:416:28 + --> tests/ui/needless_lifetimes.rs:313:28 | LL | fn nested_fn_pointer_3<'a>(_: &'a i32) -> fn(fn(&i32) -> &i32) -> i32 { | ^^ ^^ @@ -420,7 +312,7 @@ LL + fn nested_fn_pointer_3(_: &i32) -> fn(fn(&i32) -> &i32) -> i32 { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:419:28 + --> tests/ui/needless_lifetimes.rs:316:28 | LL | fn nested_fn_pointer_4<'a>(_: &'a i32) -> impl Fn(fn(&i32)) { | ^^ ^^ @@ -432,7 +324,7 @@ LL + fn nested_fn_pointer_4(_: &i32) -> impl Fn(fn(&i32)) { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:441:21 + --> tests/ui/needless_lifetimes.rs:338:21 | LL | fn implicit<'a>(&'a self) -> &'a () { | ^^ ^^ ^^ @@ -444,7 +336,7 @@ LL + fn implicit(&self) -> &() { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:444:25 + --> tests/ui/needless_lifetimes.rs:341:25 | LL | fn implicit_mut<'a>(&'a mut self) -> &'a () { | ^^ ^^ ^^ @@ -456,7 +348,7 @@ LL + fn implicit_mut(&mut self) -> &() { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:448:21 + --> tests/ui/needless_lifetimes.rs:345:21 | LL | fn explicit<'a>(self: &'a Arc) -> &'a () { | ^^ ^^ ^^ @@ -468,7 +360,7 @@ LL + fn explicit(self: &Arc) -> &() { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:452:25 + --> tests/ui/needless_lifetimes.rs:349:25 | LL | fn explicit_mut<'a>(self: &'a mut Rc) -> &'a () { | ^^ ^^ ^^ @@ -480,7 +372,7 @@ LL + fn explicit_mut(self: &mut Rc) -> &() { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:464:31 + --> tests/ui/needless_lifetimes.rs:361:31 | LL | fn lifetime_elsewhere<'a>(self: Box, here: &'a ()) -> &'a () { | ^^ ^^ ^^ @@ -492,7 +384,7 @@ LL + fn lifetime_elsewhere(self: Box, here: &()) -> &() { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:470:21 + --> tests/ui/needless_lifetimes.rs:367:21 | LL | fn implicit<'a>(&'a self) -> &'a (); | ^^ ^^ ^^ @@ -504,7 +396,7 @@ LL + fn implicit(&self) -> &(); | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:471:30 + --> tests/ui/needless_lifetimes.rs:368:30 | LL | fn implicit_provided<'a>(&'a self) -> &'a () { | ^^ ^^ ^^ @@ -516,7 +408,7 @@ LL + fn implicit_provided(&self) -> &() { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:476:21 + --> tests/ui/needless_lifetimes.rs:373:21 | LL | fn explicit<'a>(self: &'a Arc) -> &'a (); | ^^ ^^ ^^ @@ -528,7 +420,7 @@ LL + fn explicit(self: &Arc) -> &(); | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:478:30 + --> tests/ui/needless_lifetimes.rs:375:30 | LL | fn explicit_provided<'a>(self: &'a Arc) -> &'a () { | ^^ ^^ ^^ @@ -540,7 +432,7 @@ LL + fn explicit_provided(self: &Arc) -> &() { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:488:31 + --> tests/ui/needless_lifetimes.rs:385:31 | LL | fn lifetime_elsewhere<'a>(self: Box, here: &'a ()) -> &'a (); | ^^ ^^ ^^ @@ -552,7 +444,7 @@ LL + fn lifetime_elsewhere(self: Box, here: &()) -> &(); | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:489:40 + --> tests/ui/needless_lifetimes.rs:386:40 | LL | fn lifetime_elsewhere_provided<'a>(self: Box, here: &'a ()) -> &'a () { | ^^ ^^ ^^ @@ -564,7 +456,7 @@ LL + fn lifetime_elsewhere_provided(self: Box, here: &()) -> &() { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:498:12 + --> tests/ui/needless_lifetimes.rs:395:12 | LL | fn foo<'a>(x: &'a u8, y: &'_ u8) {} | ^^ ^^ @@ -576,7 +468,7 @@ LL + fn foo(x: &u8, y: &'_ u8) {} | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:500:12 + --> tests/ui/needless_lifetimes.rs:397:12 | LL | fn bar<'a>(x: &'a u8, y: &'_ u8, z: &'_ u8) {} | ^^ ^^ @@ -588,7 +480,7 @@ LL + fn bar(x: &u8, y: &'_ u8, z: &'_ u8) {} | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:507:18 + --> tests/ui/needless_lifetimes.rs:404:18 | LL | fn one_input<'a>(x: &'a u8) -> &'a u8 { | ^^ ^^ ^^ @@ -600,7 +492,7 @@ LL + fn one_input(x: &u8) -> &u8 { | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:512:42 + --> tests/ui/needless_lifetimes.rs:409:42 | LL | fn multiple_inputs_output_not_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'b u8 { | ^^ ^^ @@ -612,7 +504,7 @@ LL + fn multiple_inputs_output_not_elided<'b>(x: &u8, y: &'b u8, z: &'b u8) | error: the following explicit lifetimes could be elided: 'a - --> tests/ui/needless_lifetimes.rs:528:22 + --> tests/ui/needless_lifetimes.rs:425:22 | LL | fn one_input<'a>(x: &'a u8) -> &'a u8 { | ^^ ^^ ^^ @@ -624,64 +516,5 @@ LL - fn one_input<'a>(x: &'a u8) -> &'a u8 { LL + fn one_input(x: &u8) -> &u8 { | -error: the following explicit lifetimes could be elided: 'py - --> tests/ui/needless_lifetimes.rs:623:14 - | -LL | impl<'t, 'py> ContentString<'t> { - | ^^^ -LL | // `'py` can be elided because of `&self` -LL | fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { - | ^^^ - | -help: elide the lifetimes - | -LL ~ impl<'t> ContentString<'t> { -LL | // `'py` can be elided because of `&self` -LL ~ fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> { - | - -error: the following explicit lifetimes could be elided: 'py - --> tests/ui/needless_lifetimes.rs:633:14 - | -LL | impl<'t, 'py> ContentString<'t> { - | ^^^ -LL | // `'py` can be elided because of `&'_ self` -LL | fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> { - | ^^^ - | -help: elide the lifetimes - | -LL ~ impl<'t> ContentString<'t> { -LL | // `'py` can be elided because of `&'_ self` -LL ~ fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> { - | - -error: the following explicit lifetimes could be elided: 'py - --> tests/ui/needless_lifetimes.rs:653:14 - | -LL | impl<'t, 'py> ContentString<'t> { - | ^^^ -... -LL | ) -> Content<'t, 'py> { - | ^^^ - | -help: elide the lifetimes - | -LL ~ impl<'t> ContentString<'t> { -LL | // `'py` can be elided because of `&Self` -... -LL | o: &'t str, -LL ~ ) -> Content<'t, '_> { - | - -error: this expression creates a reference which is immediately dereferenced by the compiler - --> tests/ui/needless_lifetimes.rs:674:9 - | -LL | &x.b - | ^^^^ help: change this to: `x.b` - | - = note: `-D clippy::needless-borrow` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::needless_borrow)]` - -error: aborting due to 56 previous errors +error: aborting due to 43 previous errors