Skip to content

Commit 75e2624

Browse files
committed
track current_item in Deprecated lint pass
1 parent 5ef1e7e commit 75e2624

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/librustc_lint/builtin.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,21 @@ declare_lint! {
567567
}
568568

569569
/// Checks for use of items with `#[deprecated]` or `#[rustc_deprecated]` attributes
570-
#[derive(Copy, Clone)]
571-
pub struct Deprecated;
570+
#[derive(Clone)]
571+
pub struct Deprecated {
572+
/// Tracks the `NodeId` of the current item.
573+
///
574+
/// This is required since not all node ids are present in the hir map.
575+
current_item: ast::NodeId,
576+
}
572577

573578
impl Deprecated {
579+
pub fn new() -> Deprecated {
580+
Deprecated {
581+
current_item: ast::CRATE_NODE_ID,
582+
}
583+
}
584+
574585
fn lint(&self, cx: &LateContext, _id: DefId, span: Span,
575586
stability: &Option<&attr::Stability>, deprecation: &Option<attr::Deprecation>) {
576587
// Deprecated attributes apply in-crate and cross-crate.
@@ -591,6 +602,19 @@ impl Deprecated {
591602
cx.span_lint(lint, span, &msg);
592603
}
593604
}
605+
606+
fn push_item(&mut self, item_id: ast::NodeId) {
607+
self.current_item = item_id;
608+
}
609+
610+
fn item_post(&mut self, cx: &LateContext, item_id: ast::NodeId) {
611+
assert_eq!(self.current_item, item_id);
612+
self.current_item = cx.tcx.map.get_parent(item_id);
613+
}
614+
615+
fn parent_def(&self, cx: &LateContext) -> DefId {
616+
cx.tcx.map.local_def_id(self.current_item)
617+
}
594618
}
595619

596620
impl LintPass for Deprecated {
@@ -601,11 +625,16 @@ impl LintPass for Deprecated {
601625

602626
impl LateLintPass for Deprecated {
603627
fn check_item(&mut self, cx: &LateContext, item: &hir::Item) {
628+
self.push_item(item.id);
604629
stability::check_item(cx.tcx, item, false,
605630
&mut |id, sp, stab, depr|
606631
self.lint(cx, id, sp, &stab, &depr));
607632
}
608633

634+
fn check_item_post(&mut self, cx: &LateContext, item: &hir::Item) {
635+
self.item_post(cx, item.id);
636+
}
637+
609638
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
610639
stability::check_expr(cx.tcx, e,
611640
&mut |id, sp, stab, depr|
@@ -629,6 +658,30 @@ impl LateLintPass for Deprecated {
629658
&mut |id, sp, stab, depr|
630659
self.lint(cx, id, sp, &stab, &depr));
631660
}
661+
662+
fn check_impl_item(&mut self, _: &LateContext, item: &hir::ImplItem) {
663+
self.push_item(item.id);
664+
}
665+
666+
fn check_impl_item_post(&mut self, cx: &LateContext, item: &hir::ImplItem) {
667+
self.item_post(cx, item.id);
668+
}
669+
670+
fn check_trait_item(&mut self, _: &LateContext, item: &hir::TraitItem) {
671+
self.push_item(item.id);
672+
}
673+
674+
fn check_trait_item_post(&mut self, cx: &LateContext, item: &hir::TraitItem) {
675+
self.item_post(cx, item.id);
676+
}
677+
678+
fn check_foreign_item(&mut self, _: &LateContext, item: &hir::ForeignItem) {
679+
self.push_item(item.id);
680+
}
681+
682+
fn check_foreign_item_post(&mut self, cx: &LateContext, item: &hir::ForeignItem) {
683+
self.item_post(cx, item.id);
684+
}
632685
}
633686

634687
declare_lint! {

src/librustc_lint/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
124124
UnusedAllocation,
125125
MissingCopyImplementations,
126126
UnstableFeatures,
127-
Deprecated,
128127
UnconditionalRecursion,
129128
InvalidNoMangleItems,
130129
PluginAsLibrary,
@@ -133,6 +132,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
133132
);
134133

135134
add_builtin_with_new!(sess,
135+
Deprecated,
136136
TypeLimits,
137137
MissingDoc,
138138
MissingDebugImplementations,

0 commit comments

Comments
 (0)