Skip to content

Commit

Permalink
Auto merge of #45611 - Manishearth:lint-generics, r=petrochenkov
Browse files Browse the repository at this point in the history
Add generics to LateContext

Fixes clippy breakage from #44766 as discussed in rust-lang/rust-clippy#2140 (comment)

r? @nikomatsakis
  • Loading branch information
bors committed Oct 29, 2017
2 parents 7d475a2 + 84f1fc9 commit 690ff04
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,19 @@ impl Item_ {
_ => None,
}
}

pub fn generics(&self) -> Option<&Generics> {
Some(match *self {
ItemFn(_, _, _, _, ref generics, _) |
ItemTy(_, ref generics) |
ItemEnum(_, ref generics) |
ItemStruct(_, ref generics) |
ItemUnion(_, ref generics) |
ItemTrait(_, ref generics, _, _) |
ItemImpl(_, _, _, ref generics, _, _, _)=> generics,
_ => return None
})
}
}

/// A reference from an trait to one of its associated items. This
Expand Down
15 changes: 14 additions & 1 deletion src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ pub struct LateContext<'a, 'tcx: 'a> {
lint_sess: LintSession<'tcx, LateLintPassObject>,

last_ast_node_with_lint_attrs: ast::NodeId,

/// Generic type parameters in scope for the item we are in.
pub generics: Option<&'tcx hir::Generics>,
}

/// Context for lint checking of the AST, after expansion, before lowering to
Expand Down Expand Up @@ -646,13 +649,16 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
}

fn visit_item(&mut self, it: &'tcx hir::Item) {
let generics = self.generics.take();
self.generics = it.node.generics();
self.with_lint_attrs(it.id, &it.attrs, |cx| {
cx.with_param_env(it.id, |cx| {
run_lints!(cx, check_item, late_passes, it);
hir_visit::walk_item(cx, it);
run_lints!(cx, check_item_post, late_passes, it);
});
})
});
self.generics = generics;
}

fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem) {
Expand Down Expand Up @@ -774,23 +780,29 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
}

fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
let generics = self.generics.take();
self.generics = Some(&trait_item.generics);
self.with_lint_attrs(trait_item.id, &trait_item.attrs, |cx| {
cx.with_param_env(trait_item.id, |cx| {
run_lints!(cx, check_trait_item, late_passes, trait_item);
hir_visit::walk_trait_item(cx, trait_item);
run_lints!(cx, check_trait_item_post, late_passes, trait_item);
});
});
self.generics = generics;
}

fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
let generics = self.generics.take();
self.generics = Some(&impl_item.generics);
self.with_lint_attrs(impl_item.id, &impl_item.attrs, |cx| {
cx.with_param_env(impl_item.id, |cx| {
run_lints!(cx, check_impl_item, late_passes, impl_item);
hir_visit::walk_impl_item(cx, impl_item);
run_lints!(cx, check_impl_item_post, late_passes, impl_item);
});
});
self.generics = generics;
}

fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
Expand Down Expand Up @@ -991,6 +1003,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
access_levels,
lint_sess: LintSession::new(&tcx.sess.lint_store),
last_ast_node_with_lint_attrs: ast::CRATE_NODE_ID,
generics: None,
};

// Visit the whole crate.
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy
2 changes: 1 addition & 1 deletion src/tools/toolstate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
miri = "Broken"

# ping @Manishearth @llogiq @mcarton @oli-obk
clippy = "Broken"
clippy = "Compiling"

# ping @nrc
rls = "Broken"
Expand Down

0 comments on commit 690ff04

Please sign in to comment.