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}
;
7 use rustc_lint
::{LateContext, LateLintPass}
;
8 use rustc_session
::{declare_tool_lint, impl_lint_pass}
;
11 declare_clippy_lint
! {
13 /// Checks for usage of dbg!() macro.
15 /// ### Why is this bad?
16 /// `dbg!` macro is intended as a debugging tool. It
17 /// should not be in version control.
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 { return }
;
50 if cx
.tcx
.is_diagnostic_item(sym
::dbg_macro
, macro_call
.def_id
) {
51 // allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml
52 if self.allow_dbg_in_tests
53 && (is_in_test_function(cx
.tcx
, expr
.hir_id
) || is_in_cfg_test(cx
.tcx
, expr
.hir_id
))
57 let mut applicability
= Applicability
::MachineApplicable
;
58 let suggestion
= match expr
.peel_drop_temps().kind
{
60 ExprKind
::Block(_
, _
) => String
::new(),
62 ExprKind
::Match(val
, ..) => {
63 snippet_with_applicability(cx
, val
.span
.source_callsite(), "..", &mut applicability
).to_string()
69 kind
: ExprKind
::Match(first
, ..),
74 kind
: ExprKind
::Match(last
, ..),
79 let snippet
= snippet_with_applicability(
81 first
.span
.source_callsite().to(last
.span
.source_callsite()),
85 format
!("({snippet})")
94 "`dbg!` macro is intended as a debugging tool",
95 "ensure to avoid having uses of it in version control",