Skip to content

Commit 1b7fb40

Browse files
committed
Auto merge of rust-lang#10947 - Centri3:needless_lifetimes, r=llogiq
Improve suggestion for [`needless_lifetimes`] Fixes rust-lang#10093 changelog: [`needless_lifetimes`]: Suggestion now points at the elidable lifetimes, rather than the entire function declaration
2 parents eefc2a0 + 74a0c9c commit 1b7fb40

File tree

5 files changed

+114
-101
lines changed

5 files changed

+114
-101
lines changed

clippy_lints/src/lifetimes.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
22
use clippy_utils::trait_ref_of_method;
3+
use itertools::Itertools;
34
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
45
use rustc_errors::Applicability;
56
use rustc_hir::intravisit::nested_filter::{self as hir_nested_filter, NestedFilter};
@@ -201,7 +202,19 @@ fn check_fn_inner<'tcx>(
201202
span_lint_and_then(
202203
cx,
203204
NEEDLESS_LIFETIMES,
204-
span.with_hi(sig.decl.output.span().hi()),
205+
elidable_lts
206+
.iter()
207+
.map(|&lt| cx.tcx.def_span(lt))
208+
.chain(usages.iter().filter_map(|usage| {
209+
if let LifetimeName::Param(def_id) = usage.res
210+
&& elidable_lts.contains(&def_id)
211+
{
212+
return Some(usage.ident.span);
213+
}
214+
215+
None
216+
}))
217+
.collect_vec(),
205218
&format!("the following explicit lifetimes could be elided: {lts}"),
206219
|diag| {
207220
if sig.header.is_async() {

tests/ui/crashes/ice-2774.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: the following explicit lifetimes could be elided: 'a
2-
--> $DIR/ice-2774.rs:15:1
2+
--> $DIR/ice-2774.rs:15:28
33
|
44
LL | pub fn add_barfoos_to_foos<'a>(bars: &HashSet<&'a Bar>) {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^ ^^
66
|
77
= note: `-D clippy::needless-lifetimes` implied by `-D warnings`
88
help: elide the lifetimes

tests/ui/crashes/needless_lifetimes_impl_trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: the following explicit lifetimes could be elided: 'a
2-
--> $DIR/needless_lifetimes_impl_trait.rs:15:5
2+
--> $DIR/needless_lifetimes_impl_trait.rs:15:12
33
|
44
LL | fn baz<'a>(&'a self) -> impl Foo + 'a {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^ ^^ ^^
66
|
77
note: the lint level is defined here
88
--> $DIR/needless_lifetimes_impl_trait.rs:1:9

tests/ui/issue_4266.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
error: the following explicit lifetimes could be elided: 'a
2-
--> $DIR/issue_4266.rs:4:1
2+
--> $DIR/issue_4266.rs:4:16
33
|
44
LL | async fn sink1<'a>(_: &'a str) {} // lint
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^ ^^
66
|
77
= note: `-D clippy::needless-lifetimes` implied by `-D warnings`
88

99
error: the following explicit lifetimes could be elided: 'a
10-
--> $DIR/issue_4266.rs:8:1
10+
--> $DIR/issue_4266.rs:8:21
1111
|
1212
LL | async fn one_to_one<'a>(s: &'a str) -> &'a str {
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
| ^^ ^^
1414

1515
error: methods called `new` usually take no `self`
1616
--> $DIR/issue_4266.rs:28:22

0 commit comments

Comments
 (0)