]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/dump_hir.rs
make arc-likes for `mutable-key` configurable
[rust.git] / clippy_lints / src / utils / dump_hir.rs
1 use clippy_utils::get_attr;
2 use rustc_hir as hir;
3 use rustc_lint::{LateContext, LateLintPass, LintContext};
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
5
6 declare_clippy_lint! {
7     /// ### What it does
8     /// It formats the attached node with `{:#?}` and writes the result to the
9     /// standard output. This is intended for debugging.
10     ///
11     /// ### Examples
12     /// ```rs
13     /// #[clippy::dump]
14     /// use std::mem;
15     ///
16     /// #[clippy::dump]
17     /// fn foo(input: u32) -> u64 {
18     ///     input as u64
19     /// }
20     /// ```
21     pub DUMP_HIR,
22     internal_warn,
23     "helper to dump info about code"
24 }
25
26 declare_lint_pass!(DumpHir => [DUMP_HIR]);
27
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:#?}");
32         }
33     }
34
35     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
36         if has_attr(cx, expr.hir_id) {
37             println!("{expr:#?}");
38         }
39     }
40
41     fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx hir::Stmt<'_>) {
42         match stmt.kind {
43             hir::StmtKind::Expr(e) | hir::StmtKind::Semi(e) if has_attr(cx, e.hir_id) => return,
44             _ => {},
45         }
46         if has_attr(cx, stmt.hir_id) {
47             println!("{stmt:#?}");
48         }
49     }
50 }
51
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
55 }