]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/dbg_macro.rs
add `tcx` to `fn walk`
[rust.git] / clippy_lints / src / dbg_macro.rs
1 use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2 use clippy_utils::source::snippet_opt;
3 use rustc_ast::ast;
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;
9
10 declare_clippy_lint! {
11     /// ### What it does
12     /// Checks for usage of dbg!() macro.
13     ///
14     /// ### Why is this bad?
15     /// `dbg!` macro is intended as a debugging tool. It
16     /// should not be in version control.
17     ///
18     /// ### Example
19     /// ```rust,ignore
20     /// // Bad
21     /// dbg!(true)
22     ///
23     /// // Good
24     /// true
25     /// ```
26     pub DBG_MACRO,
27     restriction,
28     "`dbg!` macro is intended as a debugging tool"
29 }
30
31 declare_lint_pass!(DbgMacro => [DBG_MACRO]);
32
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)) {
37                 span_lint_and_sugg(
38                     cx,
39                     DBG_MACRO,
40                     mac.span(),
41                     "`dbg!` macro is intended as a debugging tool",
42                     "ensure to avoid having uses of it in version control",
43                     sugg,
44                     Applicability::MaybeIncorrect,
45                 );
46             } else {
47                 span_lint_and_help(
48                     cx,
49                     DBG_MACRO,
50                     mac.span(),
51                     "`dbg!` macro is intended as a debugging tool",
52                     None,
53                     "ensure to avoid having uses of it in version control",
54                 );
55             }
56         }
57     }
58 }
59
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()));
65     Some(span)
66 }