]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/dbg_macro.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / dbg_macro.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::macros::root_macro_call_first_node;
3 use clippy_utils::source::snippet_with_applicability;
4 use clippy_utils::{is_in_cfg_test, is_in_test_function};
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, ExprKind, Node};
7 use rustc_lint::{LateContext, LateLintPass, LintContext};
8 use rustc_session::impl_lint_pass;
9 use rustc_span::sym;
10
11 declare_clippy_lint! {
12 /// ### What it does
13 /// Checks for usage of the [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html) macro.
14 ///
15 /// ### Why is this bad?
16 /// The `dbg!` macro is intended as a debugging tool. It should not be present in released
17 /// software or committed to a version control system.
18 ///
19 /// ### Example
20 /// ```rust,ignore
21 /// dbg!(true)
22 /// ```
23 ///
24 /// Use instead:
25 /// ```rust,ignore
26 /// true
27 /// ```
28 #[clippy::version = "1.34.0"]
29 pub DBG_MACRO,
30 restriction,
31 "`dbg!` macro is intended as a debugging tool"
32 }
33
34 #[derive(Copy, Clone)]
35 pub struct DbgMacro {
36 allow_dbg_in_tests: bool,
37 }
38
39 impl_lint_pass!(DbgMacro => [DBG_MACRO]);
40
41 impl DbgMacro {
42 pub fn new(allow_dbg_in_tests: bool) -> Self {
43 DbgMacro { allow_dbg_in_tests }
44 }
45 }
46
47 impl LateLintPass<'_> for DbgMacro {
48 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
49 let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
50 return;
51 };
52 if cx.tcx.is_diagnostic_item(sym::dbg_macro, macro_call.def_id) {
53 // allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml
54 if self.allow_dbg_in_tests
55 && (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id))
56 {
57 return;
58 }
59 let mut applicability = Applicability::MachineApplicable;
60
61 let (sugg_span, suggestion) = match expr.peel_drop_temps().kind {
62 // dbg!()
63 ExprKind::Block(..) => {
64 // If the `dbg!` macro is a "free" statement and not contained within other expressions,
65 // remove the whole statement.
66 if let Some(Node::Stmt(_)) = cx.tcx.hir().find_parent(expr.hir_id)
67 && let Some(semi_span) = cx.sess().source_map().mac_call_stmt_semi_span(macro_call.span)
68 {
69 (macro_call.span.to(semi_span), String::new())
70 } else {
71 (macro_call.span, String::from("()"))
72 }
73 },
74 // dbg!(1)
75 ExprKind::Match(val, ..) => (
76 macro_call.span,
77 snippet_with_applicability(cx, val.span.source_callsite(), "..", &mut applicability).to_string(),
78 ),
79 // dbg!(2, 3)
80 ExprKind::Tup(
81 [
82 Expr {
83 kind: ExprKind::Match(first, ..),
84 ..
85 },
86 ..,
87 Expr {
88 kind: ExprKind::Match(last, ..),
89 ..
90 },
91 ],
92 ) => {
93 let snippet = snippet_with_applicability(
94 cx,
95 first.span.source_callsite().to(last.span.source_callsite()),
96 "..",
97 &mut applicability,
98 );
99 (macro_call.span, format!("({snippet})"))
100 },
101 _ => return,
102 };
103
104 span_lint_and_sugg(
105 cx,
106 DBG_MACRO,
107 sugg_span,
108 "the `dbg!` macro is intended as a debugging tool",
109 "remove the invocation before committing it to a version control system",
110 suggestion,
111 applicability,
112 );
113 }
114 }
115 }