Skip to content
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

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

Open
TimNN opened this issue Oct 22, 2022 · 2 comments

Comments

@TimNN
Copy link
Contributor

TimNN commented Oct 22, 2022

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. #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

@kraktus
Copy link
Contributor

kraktus commented Oct 23, 2022

Could be implemented as a lint configuration setting

@lengyijun
Copy link
Contributor

I think lint should ignore the generic parameter, for example,

1078 -     pub fn iter<'a>(&'a self) -> Iter<'a, K, V> {
1078 +     pub fn iter(&self) -> Iter<'_, K, V> {

The explicit lifetime is much easier to read

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants