use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
-use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::sym;
declare_clippy_lint! {
/// ### What it does
- /// Checks for usage of dbg!() macro.
+ /// Checks for usage of the [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html) macro.
///
/// ### Why is this bad?
- /// `dbg!` macro is intended as a debugging tool. It
- /// should not be in version control.
+ /// The `dbg!` macro is intended as a debugging tool. It should not be present in released
+ /// software or committed to a version control system.
///
/// ### Example
/// ```rust,ignore
- /// // Bad
/// dbg!(true)
+ /// ```
///
- /// // Good
+ /// Use instead:
+ /// ```rust,ignore
/// true
/// ```
#[clippy::version = "1.34.0"]
"`dbg!` macro is intended as a debugging tool"
}
-declare_lint_pass!(DbgMacro => [DBG_MACRO]);
+#[derive(Copy, Clone)]
+pub struct DbgMacro {
+ allow_dbg_in_tests: bool,
+}
+
+impl_lint_pass!(DbgMacro => [DBG_MACRO]);
+
+impl DbgMacro {
+ pub fn new(allow_dbg_in_tests: bool) -> Self {
+ DbgMacro { allow_dbg_in_tests }
+ }
+}
impl LateLintPass<'_> for DbgMacro {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
if cx.tcx.is_diagnostic_item(sym::dbg_macro, macro_call.def_id) {
- // we make an exception for test code
- if is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id) {
+ // allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml
+ if self.allow_dbg_in_tests
+ && (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id))
+ {
return;
}
let mut applicability = Applicability::MachineApplicable;
cx,
DBG_MACRO,
macro_call.span,
- "`dbg!` macro is intended as a debugging tool",
- "ensure to avoid having uses of it in version control",
+ "the `dbg!` macro is intended as a debugging tool",
+ "remove the invocation before committing it to a version control system",
suggestion,
applicability,
);