-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db63611
commit 9174d9f
Showing
28 changed files
with
247 additions
and
331 deletions.
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,118 @@ | ||
use rustc_hir::def_id::LocalDefId; | ||
use rustc_hir::intravisit::{walk_fn_decl, FnKind, Visitor}; | ||
use rustc_hir::{ | ||
Body, FnDecl, FnSig, ImplItem, ImplItemKind, Lifetime, LifetimeName, TraitItem, TraitItemKind, | ||
}; | ||
use rustc_middle::ty::layout::HasTyCtxt; | ||
use rustc_session::{declare_lint, declare_lint_pass}; | ||
use rustc_span::symbol::kw; | ||
use rustc_span::Span; | ||
|
||
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" | ||
} | ||
|
||
declare_lint_pass!(ElidedNamedLifetimes => [ELIDED_NAMED_LIFETIMES]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ElidedNamedLifetimes { | ||
fn check_fn( | ||
&mut self, | ||
cx: &LateContext<'tcx>, | ||
_: FnKind<'tcx>, | ||
decl: &'tcx FnDecl<'tcx>, | ||
_: &'tcx Body<'tcx>, | ||
_: Span, | ||
_: LocalDefId, | ||
) { | ||
self.check_decl(cx, decl) | ||
} | ||
|
||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) { | ||
if let TraitItemKind::Fn(FnSig { decl, .. }, _) = item.kind { | ||
self.check_decl(cx, decl) | ||
} | ||
} | ||
|
||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) { | ||
if let ImplItemKind::Fn(FnSig { decl, .. }, _) = item.kind { | ||
self.check_decl(cx, decl) | ||
} | ||
} | ||
} | ||
|
||
impl ElidedNamedLifetimes { | ||
fn check_decl<'tcx>(&self, cx: &LateContext<'tcx>, decl: &'tcx FnDecl<'tcx>) { | ||
walk_fn_decl(&mut LifetimeVisitor { cx }, decl) | ||
} | ||
} | ||
struct LifetimeVisitor<'a, 'tcx> { | ||
cx: &'a LateContext<'tcx>, | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for LifetimeVisitor<'_, 'tcx> { | ||
fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) -> Self::Result { | ||
// `.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 tcx = self.cx.tcx(); | ||
let name = tcx.item_name(param.into()); | ||
if name == kw::UnderscoreLifetime { | ||
return; | ||
} | ||
let span = tcx.source_span(param); | ||
(name, Some(span)) | ||
} | ||
LifetimeName::Static => (kw::StaticLifetime, None), | ||
LifetimeName::ImplicitObjectLifetimeDefault | ||
| LifetimeName::Error | ||
| LifetimeName::Infer => return, | ||
}; | ||
self.cx.emit_lint( | ||
ELIDED_NAMED_LIFETIMES, | ||
ElidedNamedLifetime { | ||
span: lifetime.ident.span, | ||
sugg: { | ||
let (span, code) = lifetime.suggestion(); | ||
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
Oops, something went wrong.