1 use clippy_utils::get_attr;
3 use rustc_lint::{LateContext, LateLintPass, LintContext};
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 /// It formats the attached node with `{:#?}` and writes the result to the
9 /// standard output. This is intended for debugging.
17 /// fn foo(input: u32) -> u64 {
23 "helper to dump info about code"
26 declare_lint_pass!(DumpHir => [DUMP_HIR]);
28 impl<'tcx> LateLintPass<'tcx> for DumpHir {
29 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
30 if has_attr(cx, item.hir_id()) {
31 println!("{item:#?}");
35 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
36 if has_attr(cx, expr.hir_id) {
37 println!("{expr:#?}");
41 fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx hir::Stmt<'_>) {
43 hir::StmtKind::Expr(e) | hir::StmtKind::Semi(e) if has_attr(cx, e.hir_id) => return,
46 if has_attr(cx, stmt.hir_id) {
47 println!("{stmt:#?}");
52 fn has_attr(cx: &LateContext<'_>, hir_id: hir::HirId) -> bool {
53 let attrs = cx.tcx.hir().attrs(hir_id);
54 get_attr(cx.sess(), attrs, "dump").count() > 0