1 use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2 use clippy_utils::source::snippet_opt;
4 use rustc_ast::tokenstream::TokenStream;
5 use rustc_errors::Applicability;
6 use rustc_lint::{EarlyContext, EarlyLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::source_map::Span;
10 declare_clippy_lint! {
12 /// Checks for usage of dbg!() macro.
14 /// ### Why is this bad?
15 /// `dbg!` macro is intended as a debugging tool. It
16 /// should not be in version control.
18 /// ### Known problems
19 /// * The lint level is unaffected by crate attributes. The level can still
20 /// be set for functions, modules and other items. To change the level for
21 /// the entire crate, please use command line flags. More information and a
22 /// configuration example can be found in [clippy#6610].
24 /// [clippy#6610]: https://github.com/rust-lang/rust-clippy/issues/6610#issuecomment-977120558
34 #[clippy::version = "1.34.0"]
37 "`dbg!` macro is intended as a debugging tool"
40 declare_lint_pass!(DbgMacro => [DBG_MACRO]);
42 impl EarlyLintPass for DbgMacro {
43 fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::MacCall) {
44 if mac.path == sym!(dbg) {
45 if let Some(sugg) = tts_span(mac.args.inner_tokens()).and_then(|span| snippet_opt(cx, span)) {
50 "`dbg!` macro is intended as a debugging tool",
51 "ensure to avoid having uses of it in version control",
53 Applicability::MaybeIncorrect,
60 "`dbg!` macro is intended as a debugging tool",
62 "ensure to avoid having uses of it in version control",
69 // Get span enclosing entire the token stream.
70 fn tts_span(tts: TokenStream) -> Option<Span> {
71 let mut cursor = tts.into_trees();
72 let first = cursor.next()?.span();
73 let span = cursor.last().map_or(first, |tree| first.to(tree.span()));