From 0943a6b188e11339d217d43f39322f51d2d8187d Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:22:52 +0400 Subject: [PATCH 1/2] add test issue-117965 --- tests/ui/single-use-lifetime/issue-117965.rs | 18 ++++++++++++++++ .../single-use-lifetime/issue-117965.stderr | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/ui/single-use-lifetime/issue-117965.rs create mode 100644 tests/ui/single-use-lifetime/issue-117965.stderr diff --git a/tests/ui/single-use-lifetime/issue-117965.rs b/tests/ui/single-use-lifetime/issue-117965.rs new file mode 100644 index 00000000000..5eb2a03e13d --- /dev/null +++ b/tests/ui/single-use-lifetime/issue-117965.rs @@ -0,0 +1,18 @@ +#![deny(single_use_lifetimes)] + +pub enum Data<'a> { + Borrowed(&'a str), + Owned(String), +} + +impl<'a> Data<'a> { + pub fn get<'b: 'a>(&'b self) -> &'a str { + //~^ ERROR lifetime parameter `'b` only used once + match &self { + Self::Borrowed(val) => val, + Self::Owned(val) => &val, + } + } +} + +fn main() {} diff --git a/tests/ui/single-use-lifetime/issue-117965.stderr b/tests/ui/single-use-lifetime/issue-117965.stderr new file mode 100644 index 00000000000..e789e183546 --- /dev/null +++ b/tests/ui/single-use-lifetime/issue-117965.stderr @@ -0,0 +1,21 @@ +error: lifetime parameter `'b` only used once + --> $DIR/issue-117965.rs:9:16 + | +LL | pub fn get<'b: 'a>(&'b self) -> &'a str { + | ^^ -- ...is used only here + | | + | this lifetime... + | +note: the lint level is defined here + --> $DIR/issue-117965.rs:1:9 + | +LL | #![deny(single_use_lifetimes)] + | ^^^^^^^^^^^^^^^^^^^^ +help: elide the single-use lifetime + | +LL - pub fn get<'b: 'a>(&'b self) -> &'a str { +LL + pub fn get(&self) -> &'a str { + | + +error: aborting due to 1 previous error + From de2575f35d46dfa2b24eb205ef133a679bff1b31 Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Sat, 20 Jan 2024 02:30:58 +0400 Subject: [PATCH 2/2] Don't delete any lifetimes with bounds --- compiler/rustc_resolve/src/late/diagnostics.rs | 3 ++- tests/ui/single-use-lifetime/issue-117965.stderr | 5 ----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 5a95f2083f6..abb0a7a465a 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2567,8 +2567,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { debug!(?param.ident, ?param.ident.span, ?use_span); let elidable = matches!(use_ctxt, LifetimeCtxt::Ref); + let deletion_span = + if param.bounds.is_empty() { deletion_span() } else { None }; - let deletion_span = deletion_span(); self.r.lint_buffer.buffer_lint_with_diagnostic( lint::builtin::SINGLE_USE_LIFETIMES, param.id, diff --git a/tests/ui/single-use-lifetime/issue-117965.stderr b/tests/ui/single-use-lifetime/issue-117965.stderr index e789e183546..ed14ab92f4d 100644 --- a/tests/ui/single-use-lifetime/issue-117965.stderr +++ b/tests/ui/single-use-lifetime/issue-117965.stderr @@ -11,11 +11,6 @@ note: the lint level is defined here | LL | #![deny(single_use_lifetimes)] | ^^^^^^^^^^^^^^^^^^^^ -help: elide the single-use lifetime - | -LL - pub fn get<'b: 'a>(&'b self) -> &'a str { -LL + pub fn get(&self) -> &'a str { - | error: aborting due to 1 previous error