]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/dbg_macro.rs
Rollup merge of #92541 - asquared31415:from-docs, r=m-ou-se
[rust.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 rustc_errors::Applicability;
5 use rustc_hir::{Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::sym;
9
10 declare_clippy_lint! {
11     /// ### What it does
12     /// Checks for usage of dbg!() macro.
13     ///
14     /// ### Why is this bad?
15     /// `dbg!` macro is intended as a debugging tool. It
16     /// should not be in version control.
17     ///
18     /// ### Example
19     /// ```rust,ignore
20     /// // Bad
21     /// dbg!(true)
22     ///
23     /// // Good
24     /// true
25     /// ```
26     #[clippy::version = "1.34.0"]
27     pub DBG_MACRO,
28     restriction,
29     "`dbg!` macro is intended as a debugging tool"
30 }
31
32 declare_lint_pass!(DbgMacro => [DBG_MACRO]);
33
34 impl LateLintPass<'_> for DbgMacro {
35     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
36         let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
37         if cx.tcx.is_diagnostic_item(sym::dbg_macro, macro_call.def_id) {
38             let mut applicability = Applicability::MachineApplicable;
39             let suggestion = match expr.peel_drop_temps().kind {
40                 // dbg!()
41                 ExprKind::Block(_, _) => String::new(),
42                 // dbg!(1)
43                 ExprKind::Match(val, ..) => {
44                     snippet_with_applicability(cx, val.span.source_callsite(), "..", &mut applicability).to_string()
45                 },
46                 // dbg!(2, 3)
47                 ExprKind::Tup(
48                     [
49                         Expr {
50                             kind: ExprKind::Match(first, ..),
51                             ..
52                         },
53                         ..,
54                         Expr {
55                             kind: ExprKind::Match(last, ..),
56                             ..
57                         },
58                     ],
59                 ) => {
60                     let snippet = snippet_with_applicability(
61                         cx,
62                         first.span.source_callsite().to(last.span.source_callsite()),
63                         "..",
64                         &mut applicability,
65                     );
66                     format!("({snippet})")
67                 },
68                 _ => return,
69             };
70
71             span_lint_and_sugg(
72                 cx,
73                 DBG_MACRO,
74                 macro_call.span,
75                 "`dbg!` macro is intended as a debugging tool",
76                 "ensure to avoid having uses of it in version control",
77                 suggestion,
78                 applicability,
79             );
80         }
81     }
82 }