]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | use crate::utils::{snippet_opt, span_lint_and_help, span_lint_and_sugg}; |
2 | use rustc_ast::ast; | |
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; | |
8 | ||
9 | declare_clippy_lint! { | |
10 | /// **What it does:** Checks for usage of dbg!() macro. | |
11 | /// | |
12 | /// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It | |
13 | /// should not be in version control. | |
14 | /// | |
15 | /// **Known problems:** None. | |
16 | /// | |
17 | /// **Example:** | |
18 | /// ```rust,ignore | |
19 | /// // Bad | |
20 | /// dbg!(true) | |
21 | /// | |
22 | /// // Good | |
23 | /// true | |
24 | /// ``` | |
25 | pub DBG_MACRO, | |
26 | restriction, | |
27 | "`dbg!` macro is intended as a debugging tool" | |
28 | } | |
29 | ||
30 | declare_lint_pass!(DbgMacro => [DBG_MACRO]); | |
31 | ||
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)) { | |
36 | span_lint_and_sugg( | |
37 | cx, | |
38 | DBG_MACRO, | |
39 | mac.span(), | |
40 | "`dbg!` macro is intended as a debugging tool", | |
41 | "ensure to avoid having uses of it in version control", | |
42 | sugg, | |
43 | Applicability::MaybeIncorrect, | |
44 | ); | |
45 | } else { | |
46 | span_lint_and_help( | |
47 | cx, | |
48 | DBG_MACRO, | |
49 | mac.span(), | |
50 | "`dbg!` macro is intended as a debugging tool", | |
51 | None, | |
52 | "ensure to avoid having uses of it in version control", | |
53 | ); | |
54 | } | |
55 | } | |
56 | } | |
57 | } | |
58 | ||
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())); | |
64 | Some(span) | |
65 | } |