Skip to content

Commit

Permalink
Rollup merge of #120148 - trevyn:issue-117965, r=cjgillot
Browse files Browse the repository at this point in the history
`single_use_lifetimes`: Don't suggest deleting lifetimes with bounds

Closes #117965

```
9 |     pub fn get<'b: 'a>(&'b self) -> &'a str {
  |                ^^       -- ...is used only here
  |                |
  |                this lifetime...
```

In this example, I think the `&'b self` can be replaced with the bound itself, yielding `&'a self`, but this would require a deeper refactor. Happy to do as a follow-on PR if desired.
  • Loading branch information
matthiaskrgr authored Jan 20, 2024
2 parents 409949b + de2575f commit b7c2ba7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/single-use-lifetime/issue-117965.rs
Original file line number Diff line number Diff line change
@@ -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() {}
16 changes: 16 additions & 0 deletions tests/ui/single-use-lifetime/issue-117965.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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)]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

0 comments on commit b7c2ba7

Please sign in to comment.