1 use crate::utils::{snippet_opt, span_lint_and_help, span_lint_and_sugg};
3 use rustc_ast::tokenstream::TokenStream;
4 use rustc_errors::Applicability;
5 use rustc_lint::{EarlyContext, EarlyLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::source_map::Span;
10 /// **What it does:** Checks for usage of dbg!() macro.
12 /// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It
13 /// should not be in version control.
15 /// **Known problems:** None.
27 "`dbg!` macro is intended as a debugging tool"
30 declare_lint_pass!(DbgMacro => [DBG_MACRO]);
32 impl EarlyLintPass for DbgMacro {
33 fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::MacCall) {
34 if mac.path == sym!(dbg) {
35 if let Some(sugg) = tts_span(mac.args.inner_tokens()).and_then(|span| snippet_opt(cx, span)) {
40 "`dbg!` macro is intended as a debugging tool",
41 "ensure to avoid having uses of it in version control",
43 Applicability::MaybeIncorrect,
50 "`dbg!` macro is intended as a debugging tool",
52 "ensure to avoid having uses of it in version control",
59 // Get span enclosing entire the token stream.
60 fn tts_span(tts: TokenStream) -> Option<Span> {
61 let mut cursor = tts.into_trees();
62 let first = cursor.next()?.span();
63 let span = cursor.last().map_or(first, |tree| first.to(tree.span()));