-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
In-band lifetimes: Lint against single-use lifetime names #44752
Copy link
Copy link
Open
Labels
A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.Blocker: Approved by a merged RFC but not yet implemented.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCE-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.L-single_use_lifetimesLint: single_use_lifetimesLint: single_use_lifetimesS-tracking-design-concernsStatus: There are blocking design concerns.Status: There are blocking design concerns.S-tracking-impl-incompleteStatus: The implementation is incomplete.Status: The implementation is incomplete.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team
Metadata
Metadata
Assignees
Labels
A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.Blocker: Approved by a merged RFC but not yet implemented.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCE-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.L-single_use_lifetimesLint: single_use_lifetimesLint: single_use_lifetimesS-tracking-design-concernsStatus: There are blocking design concerns.Status: There are blocking design concerns.S-tracking-impl-incompleteStatus: The implementation is incomplete.Status: The implementation is incomplete.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team
Type
Fields
Give feedbackNo fields configured for issues without a type.
Once support for
'_lands in #44691, the next step for #44524 is to implement a lint that warns against "single use" lifetime names.Current status
The lint is partially implemented but needs to be completed. Here is a checklist:
unused_lifetimes'aappearing in&'a T, suggest&T'aappearing in any other place, suggest'_Older Background
The idea is that an explicit name like
'ashould only be used (at least in a function or impl) to link together two things. Otherwise, you should just use'_to indicate that the lifetime is not linked to anything.Until #15872 is closed, we should only lint for single-use lifetime names that are bound in functions. Once #15872 is closed, we can also lint against those found in impl headers.
We can detect cases where a lint is valid by modifying the
resolve_lifetimescode:with()is used to push new scopes on the stack. It is also given a closure which will execute with the new name bindings in scope.with()gets called for impls and other kinds of items from here; for methods and functions in particular it is called fromvisit_early_late).resolve_lifetime_ref()is called to resolve an actual reference to a named lifetime.Scope, e.g. counting how many times a particular lifetime was referenced.with()returns, we could scan the lifetimes and check for those that were only referenced 1 time (or 0 times...) and issue a lint warning.(There are some directions for how to add a lint under the header "Issuing future compatibility warnings" in the rustc-bug-fix-procedure page on forge -- we can skip the "future compatibility" parts here.)