Skip to content

Commit ccedc64

Browse files
committed
Add #[clippy::print_hir] attribute for debugging
1 parent b6645d0 commit ccedc64

File tree

6 files changed

+72
-558
lines changed

6 files changed

+72
-558
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
501501
{
502502
store.register_early_pass(|| Box::new(utils::internal_lints::ClippyLintsInternal));
503503
store.register_early_pass(|| Box::new(utils::internal_lints::ProduceIce));
504-
store.register_late_pass(|| Box::new(utils::inspector::DeepCodeInspector));
505504
store.register_late_pass(|| Box::new(utils::internal_lints::CollapsibleCalls));
506505
store.register_late_pass(|| Box::new(utils::internal_lints::CompilerLintFunctions::new()));
507506
store.register_late_pass(|| Box::new(utils::internal_lints::IfChainStyle));
@@ -513,6 +512,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
513512
store.register_late_pass(|| Box::new(utils::internal_lints::MsrvAttrImpl));
514513
}
515514

515+
store.register_late_pass(|| Box::new(utils::dump_hir::DumpHir));
516516
store.register_late_pass(|| Box::new(utils::author::Author));
517517
store.register_late_pass(|| Box::new(await_holding_invalid::AwaitHolding));
518518
store.register_late_pass(|| Box::new(serde_api::SerdeApi));

clippy_lints/src/utils/dump_hir.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

0 commit comments

Comments
 (0)