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
! {
11 /// **What it does:** Checks for usage of dbg!() macro.
13 /// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It
14 /// should not be in version control.
16 /// **Known problems:** None.
28 "`dbg!` macro is intended as a debugging tool"
31 declare_lint_pass
!(DbgMacro
=> [DBG_MACRO
]);
33 impl EarlyLintPass
for DbgMacro
{
34 fn check_mac(&mut self, cx
: &EarlyContext
<'_
>, mac
: &ast
::MacCall
) {
35 if mac
.path
== sym
!(dbg
) {
36 if let Some(sugg
) = tts_span(mac
.args
.inner_tokens()).and_then(|span
| snippet_opt(cx
, span
)) {
41 "`dbg!` macro is intended as a debugging tool",
42 "ensure to avoid having uses of it in version control",
44 Applicability
::MaybeIncorrect
,
51 "`dbg!` macro is intended as a debugging tool",
53 "ensure to avoid having uses of it in version control",
60 // Get span enclosing entire the token stream.
61 fn tts_span(tts
: TokenStream
) -> Option
<Span
> {
62 let mut cursor
= tts
.into_trees();
63 let first
= cursor
.next()?
.span();
64 let span
= cursor
.last().map_or(first
, |tree
| first
.to(tree
.span()));