-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Move elided_named_lifetimes
into a separate pass
#130150
Open
GrigorenkoPV
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
GrigorenkoPV:elided-named-lifetimes-pass
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−344
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use rustc_hir::{ | ||
ImplItem, ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, TraitItem, TraitItemKind, | ||
}; | ||
use rustc_middle::ty::layout::HasTyCtxt; | ||
use rustc_session::{declare_lint, impl_lint_pass}; | ||
use rustc_span::symbol::kw; | ||
|
||
use crate::lints::{ElidedNamedLifetime, ElidedNamedLifetimeSuggestion}; | ||
use crate::{LateContext, LateLintPass, LintContext}; | ||
|
||
declare_lint! { | ||
/// The `elided_named_lifetimes` lint detects when an elided | ||
/// lifetime ends up being a named lifetime, such as `'static` | ||
/// or some lifetime parameter `'a`. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,compile_fail | ||
/// #![deny(elided_named_lifetimes)] | ||
/// struct Foo; | ||
/// impl Foo { | ||
/// pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 { | ||
/// unsafe { &mut *(x as *mut _) } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// Lifetime elision is quite useful, because it frees you from having | ||
/// to give each lifetime its own name, but sometimes it can produce | ||
/// somewhat surprising resolutions. In safe code, it is mostly okay, | ||
/// because the borrow checker prevents any unsoundness, so the worst | ||
/// case scenario is you get a confusing error message in some other place. | ||
/// But with `unsafe` code, such unexpected resolutions may lead to unsound code. | ||
pub ELIDED_NAMED_LIFETIMES, | ||
Warn, | ||
"detects when an elided lifetime gets resolved to be `'static` or some named parameter" | ||
} | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub(crate) struct ElidedNamedLifetimes { | ||
allow_static: bool, | ||
} | ||
|
||
impl_lint_pass!(ElidedNamedLifetimes => [ELIDED_NAMED_LIFETIMES]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ElidedNamedLifetimes { | ||
fn check_trait_item(&mut self, _: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) { | ||
if let TraitItemKind::Const(..) = item.kind { | ||
self.allow_static = true; | ||
} | ||
} | ||
fn check_trait_item_post(&mut self, _: &LateContext<'tcx>, _: &'tcx TraitItem<'tcx>) { | ||
self.allow_static = false; | ||
} | ||
|
||
fn check_impl_item(&mut self, _: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) { | ||
if let ImplItemKind::Const(..) = item.kind { | ||
self.allow_static = true; | ||
} | ||
} | ||
fn check_impl_item_post(&mut self, _: &LateContext<'tcx>, _: &'tcx ImplItem<'tcx>) { | ||
self.allow_static = false; | ||
} | ||
|
||
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { | ||
if let ItemKind::Const(..) | ItemKind::Static(..) = item.kind { | ||
self.allow_static = true; | ||
} | ||
} | ||
fn check_item_post(&mut self, _: &LateContext<'tcx>, _: &'tcx Item<'tcx>) { | ||
self.allow_static = false; | ||
} | ||
|
||
fn check_lifetime(&mut self, cx: &LateContext<'tcx>, lifetime: &'tcx Lifetime) { | ||
// `.is_elided()` should probably be called `.resolves_to_elided()`, | ||
// and `.is_anonymous()` is actually the thing that we need here. | ||
if !lifetime.is_anonymous() { | ||
return; | ||
} | ||
let (name, declaration) = match lifetime.res { | ||
LifetimeName::Param(param) => { | ||
let name = cx.tcx().item_name(param.into()); | ||
if name == kw::UnderscoreLifetime { | ||
return; | ||
} | ||
let span = cx.tcx().def_span(param); | ||
(name, Some(span)) | ||
} | ||
LifetimeName::Static => { | ||
if self.allow_static { | ||
return; | ||
} | ||
(kw::StaticLifetime, None) | ||
} | ||
LifetimeName::ImplicitObjectLifetimeDefault | ||
| LifetimeName::Error | ||
| LifetimeName::Infer => return, | ||
}; | ||
cx.emit_lint( | ||
ELIDED_NAMED_LIFETIMES, | ||
ElidedNamedLifetime { | ||
span: lifetime.ident.span, | ||
sugg: { | ||
let (span, code) = lifetime.suggestion(name.as_str()); | ||
ElidedNamedLifetimeSuggestion { span, code } | ||
}, | ||
name, | ||
declaration, | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced this handles cases like:
You may need an actual stack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just storing the id of the outermost item would be enough.
However, I am reluctant to move any further with this PR as I suspect some lifetimes, which are present in AST, are not present in HIR and thus are unlintable with this approach.