Skip to content

Improve suggestion for [needless_lifetimes] #10947

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

Merged
merged 1 commit into from
Jun 14, 2023
Merged
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
15 changes: 14 additions & 1 deletion clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::trait_ref_of_method;
use itertools::Itertools;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability;
use rustc_hir::intravisit::nested_filter::{self as hir_nested_filter, NestedFilter};
Expand Down Expand Up @@ -201,7 +202,19 @@ fn check_fn_inner<'tcx>(
span_lint_and_then(
cx,
NEEDLESS_LIFETIMES,
span.with_hi(sig.decl.output.span().hi()),
elidable_lts
.iter()
.map(|&lt| cx.tcx.def_span(lt))
.chain(usages.iter().filter_map(|usage| {
if let LifetimeName::Param(def_id) = usage.res
&& elidable_lts.contains(&def_id)
{
return Some(usage.ident.span);
}

None
}))
.collect_vec(),
&format!("the following explicit lifetimes could be elided: {lts}"),
|diag| {
if sig.header.is_async() {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/crashes/ice-2774.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: the following explicit lifetimes could be elided: 'a
--> $DIR/ice-2774.rs:15:1
--> $DIR/ice-2774.rs:15:28
|
LL | pub fn add_barfoos_to_foos<'a>(bars: &HashSet<&'a Bar>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^ ^^
|
= note: `-D clippy::needless-lifetimes` implied by `-D warnings`
help: elide the lifetimes
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/crashes/needless_lifetimes_impl_trait.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes_impl_trait.rs:15:5
--> $DIR/needless_lifetimes_impl_trait.rs:15:12
|
LL | fn baz<'a>(&'a self) -> impl Foo + 'a {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^ ^^ ^^
|
note: the lint level is defined here
--> $DIR/needless_lifetimes_impl_trait.rs:1:9
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/issue_4266.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error: the following explicit lifetimes could be elided: 'a
--> $DIR/issue_4266.rs:4:1
--> $DIR/issue_4266.rs:4:16
|
LL | async fn sink1<'a>(_: &'a str) {} // lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^ ^^
|
= note: `-D clippy::needless-lifetimes` implied by `-D warnings`

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

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