/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs

https://gitlab.com/rust-lang/rust · Rust · 57 lines · 50 code · 7 blank · 0 comment · 3 complexity · eea11df61cf21864c1b4aef07740ff92 MD5 · raw file

  1. #![feature(plugin, rustc_private)]
  2. #![crate_type = "dylib"]
  3. extern crate rustc_ast_pretty;
  4. extern crate rustc_driver;
  5. extern crate rustc_hir;
  6. extern crate rustc_lint;
  7. #[macro_use]
  8. extern crate rustc_session;
  9. extern crate rustc_ast;
  10. extern crate rustc_span;
  11. use rustc_ast_pretty::pprust;
  12. use rustc_driver::plugin::Registry;
  13. use rustc_hir as hir;
  14. use rustc_hir::intravisit;
  15. use rustc_hir::Node;
  16. use rustc_lint::{LateContext, LateLintPass, LintContext};
  17. use rustc_span::source_map;
  18. #[no_mangle]
  19. fn __rustc_plugin_registrar(reg: &mut Registry) {
  20. reg.lint_store.register_lints(&[&MISSING_ALLOWED_ATTR]);
  21. reg.lint_store.register_late_pass(|| Box::new(MissingAllowedAttrPass));
  22. }
  23. declare_lint! {
  24. MISSING_ALLOWED_ATTR,
  25. Deny,
  26. "Checks for missing `allowed_attr` attribute"
  27. }
  28. declare_lint_pass!(MissingAllowedAttrPass => [MISSING_ALLOWED_ATTR]);
  29. impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass {
  30. fn check_fn(
  31. &mut self,
  32. cx: &LateContext<'tcx>,
  33. _: intravisit::FnKind<'tcx>,
  34. _: &'tcx hir::FnDecl,
  35. _: &'tcx hir::Body,
  36. span: source_map::Span,
  37. id: hir::HirId,
  38. ) {
  39. let item = match cx.tcx.hir().get(id) {
  40. Node::Item(item) => item,
  41. _ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id)),
  42. };
  43. let allowed = |attr| pprust::attribute_to_string(attr).contains("allowed_attr");
  44. if !cx.tcx.hir().attrs(item.hir_id()).iter().any(allowed) {
  45. cx.lint(MISSING_ALLOWED_ATTR, |lint| {
  46. lint.build("Missing 'allowed_attr' attribute").set_span(span).emit();
  47. });
  48. }
  49. }
  50. }