rust-lang / rust-lang/rust-clippy

Suggestion: Make `needless_lifetimes` ignore functions that are, or contain, `unsafe`.

Open
#9,694 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description

I've found myself somewhat frequently disabling the needless_lifetimes lint for functions that are, or contain, unsafe, primarily because I felt like it was better to be explicit in those case. (One example: more limited wrappers around pointer casts / transmutes).

IMO, given that unsafe can be used to circumvent the borrow checker, eliding the lifetimes increases complexity, since it's one more thing that authors and reviewers have to pay attention to (because they can't rely on the compiler to catch incorrect lifetimes / elision).

I don't know if such an exception would be too broad, but I believe that if a function is, or contains, unsafe, there's a high likelihood of explicit lifetimes being intentional.

And, maybe more importantly, getting the elision wrong can have more serious consequences, see e.g. https://github.com/rust-lang/rust-clippy/issues/9360.

(Nothing prevents authors from eliding the lifetimes on their own (i.e. I'm not advocating for an "use explicit lifetimes with unsafe" lint), but I don't think Clippy should suggest removing explicit lifetimes from these functions if they are present).

Version

No response

Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the needless_lifetimes lint and its existing tests. Clarify how the proposed exception should apply to functions that are unsafe or contain unsafe, then add coverage so those cases no longer receive the suggestion while unaffected cases retain current behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.