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
;
11 declare_clippy_lint
! {
13 /// Checks for usage of the [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html) macro.
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.
28 #[clippy::version = "1.34.0"]
31 "`dbg!` macro is intended as a debugging tool"
34 #[derive(Copy, Clone)]
36 allow_dbg_in_tests
: bool
,
39 impl_lint_pass
!(DbgMacro
=> [DBG_MACRO
]);
42 pub fn new(allow_dbg_in_tests
: bool
) -> Self {
43 DbgMacro { allow_dbg_in_tests }
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 {
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
))
59 let mut applicability
= Applicability
::MachineApplicable
;
61 let (sugg_span
, suggestion
) = match expr
.peel_drop_temps().kind
{
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
)
69 (macro_call
.span
.to(semi_span
), String
::new())
71 (macro_call
.span
, String
::from("()"))
75 ExprKind
::Match(val
, ..) => (
77 snippet_with_applicability(cx
, val
.span
.source_callsite(), "..", &mut applicability
).to_string(),
83 kind
: ExprKind
::Match(first
, ..),
88 kind
: ExprKind
::Match(last
, ..),
93 let snippet
= snippet_with_applicability(
95 first
.span
.source_callsite().to(last
.span
.source_callsite()),
99 (macro_call
.span
, format
!("({snippet})"))
108 "the `dbg!` macro is intended as a debugging tool",
109 "remove the invocation before committing it to a version control system",