]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/write.rs
Run rustfmt on clippy_lints
[rust.git] / clippy_lints / src / write.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
11 use crate::rustc::{declare_tool_lint, lint_array};
12 use crate::rustc_errors::Applicability;
13 use crate::syntax::ast::*;
14 use crate::syntax::parse::{parser, token};
15 use crate::syntax::tokenstream::{ThinTokenStream, TokenStream};
16 use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg};
17 use std::borrow::Cow;
18
19 /// **What it does:** This lint warns when you use `println!("")` to
20 /// print a newline.
21 ///
22 /// **Why is this bad?** You should use `println!()`, which is simpler.
23 ///
24 /// **Known problems:** None.
25 ///
26 /// **Example:**
27 /// ```rust
28 /// println!("");
29 /// ```
30 declare_clippy_lint! {
31     pub PRINTLN_EMPTY_STRING,
32     style,
33     "using `println!(\"\")` with an empty string"
34 }
35
36 /// **What it does:** This lint warns when you use `print!()` with a format
37 /// string that
38 /// ends in a newline.
39 ///
40 /// **Why is this bad?** You should use `println!()` instead, which appends the
41 /// newline.
42 ///
43 /// **Known problems:** None.
44 ///
45 /// **Example:**
46 /// ```rust
47 /// print!("Hello {}!\n", name);
48 /// ```
49 /// use println!() instead
50 /// ```rust
51 /// println!("Hello {}!", name);
52 /// ```
53 declare_clippy_lint! {
54     pub PRINT_WITH_NEWLINE,
55     style,
56     "using `print!()` with a format string that ends in a single newline"
57 }
58
59 /// **What it does:** Checks for printing on *stdout*. The purpose of this lint
60 /// is to catch debugging remnants.
61 ///
62 /// **Why is this bad?** People often print on *stdout* while debugging an
63 /// application and might forget to remove those prints afterward.
64 ///
65 /// **Known problems:** Only catches `print!` and `println!` calls.
66 ///
67 /// **Example:**
68 /// ```rust
69 /// println!("Hello world!");
70 /// ```
71 declare_clippy_lint! {
72     pub PRINT_STDOUT,
73     restriction,
74     "printing on stdout"
75 }
76
77 /// **What it does:** Checks for use of `Debug` formatting. The purpose of this
78 /// lint is to catch debugging remnants.
79 ///
80 /// **Why is this bad?** The purpose of the `Debug` trait is to facilitate
81 /// debugging Rust code. It should not be used in in user-facing output.
82 ///
83 /// **Example:**
84 /// ```rust
85 /// println!("{:?}", foo);
86 /// ```
87 declare_clippy_lint! {
88     pub USE_DEBUG,
89     restriction,
90     "use of `Debug`-based formatting"
91 }
92
93 /// **What it does:** This lint warns about the use of literals as `print!`/`println!` args.
94 ///
95 /// **Why is this bad?** Using literals as `println!` args is inefficient
96 /// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
97 /// (i.e., just put the literal in the format string)
98 ///
99 /// **Known problems:** Will also warn with macro calls as arguments that expand to literals
100 /// -- e.g., `println!("{}", env!("FOO"))`.
101 ///
102 /// **Example:**
103 /// ```rust
104 /// println!("{}", "foo");
105 /// ```
106 /// use the literal without formatting:
107 /// ```rust
108 /// println!("foo");
109 /// ```
110 declare_clippy_lint! {
111     pub PRINT_LITERAL,
112     style,
113     "printing a literal with a format string"
114 }
115
116 /// **What it does:** This lint warns when you use `writeln!(buf, "")` to
117 /// print a newline.
118 ///
119 /// **Why is this bad?** You should use `writeln!(buf)`, which is simpler.
120 ///
121 /// **Known problems:** None.
122 ///
123 /// **Example:**
124 /// ```rust
125 /// writeln!("");
126 /// ```
127 declare_clippy_lint! {
128     pub WRITELN_EMPTY_STRING,
129     style,
130     "using `writeln!(\"\")` with an empty string"
131 }
132
133 /// **What it does:** This lint warns when you use `write!()` with a format
134 /// string that
135 /// ends in a newline.
136 ///
137 /// **Why is this bad?** You should use `writeln!()` instead, which appends the
138 /// newline.
139 ///
140 /// **Known problems:** None.
141 ///
142 /// **Example:**
143 /// ```rust
144 /// write!(buf, "Hello {}!\n", name);
145 /// ```
146 declare_clippy_lint! {
147     pub WRITE_WITH_NEWLINE,
148     style,
149     "using `write!()` with a format string that ends in a single newline"
150 }
151
152 /// **What it does:** This lint warns about the use of literals as `write!`/`writeln!` args.
153 ///
154 /// **Why is this bad?** Using literals as `writeln!` args is inefficient
155 /// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
156 /// (i.e., just put the literal in the format string)
157 ///
158 /// **Known problems:** Will also warn with macro calls as arguments that expand to literals
159 /// -- e.g., `writeln!(buf, "{}", env!("FOO"))`.
160 ///
161 /// **Example:**
162 /// ```rust
163 /// writeln!(buf, "{}", "foo");
164 /// ```
165 declare_clippy_lint! {
166     pub WRITE_LITERAL,
167     style,
168     "writing a literal with a format string"
169 }
170
171 #[derive(Copy, Clone, Debug)]
172 pub struct Pass;
173
174 impl LintPass for Pass {
175     fn get_lints(&self) -> LintArray {
176         lint_array!(
177             PRINT_WITH_NEWLINE,
178             PRINTLN_EMPTY_STRING,
179             PRINT_STDOUT,
180             USE_DEBUG,
181             PRINT_LITERAL,
182             WRITE_WITH_NEWLINE,
183             WRITELN_EMPTY_STRING,
184             WRITE_LITERAL
185         )
186     }
187 }
188
189 impl EarlyLintPass for Pass {
190     fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
191         if mac.node.path == "println" {
192             span_lint(cx, PRINT_STDOUT, mac.span, "use of `println!`");
193             if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
194                 if fmtstr == "" {
195                     span_lint_and_sugg(
196                         cx,
197                         PRINTLN_EMPTY_STRING,
198                         mac.span,
199                         "using `println!(\"\")`",
200                         "replace it with",
201                         "println!()".to_string(),
202                         Applicability::MachineApplicable,
203                     );
204                 }
205             }
206         } else if mac.node.path == "print" {
207             span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
208             if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
209                 if fmtstr.ends_with("\\n") &&
210                    // don't warn about strings with several `\n`s (#3126)
211                    fmtstr.matches("\\n").count() == 1
212                 {
213                     span_lint(
214                         cx,
215                         PRINT_WITH_NEWLINE,
216                         mac.span,
217                         "using `print!()` with a format string that ends in a \
218                          single newline, consider using `println!()` instead",
219                     );
220                 }
221             }
222         } else if mac.node.path == "write" {
223             if let Some(fmtstr) = check_tts(cx, &mac.node.tts, true).0 {
224                 if fmtstr.ends_with("\\n") &&
225                    // don't warn about strings with several `\n`s (#3126)
226                    fmtstr.matches("\\n").count() == 1
227                 {
228                     span_lint(
229                         cx,
230                         WRITE_WITH_NEWLINE,
231                         mac.span,
232                         "using `write!()` with a format string that ends in a \
233                          single newline, consider using `writeln!()` instead",
234                     );
235                 }
236             }
237         } else if mac.node.path == "writeln" {
238             let check_tts = check_tts(cx, &mac.node.tts, true);
239             if let Some(fmtstr) = check_tts.0 {
240                 if fmtstr == "" {
241                     let mut applicability = Applicability::MachineApplicable;
242                     let suggestion = check_tts.1.map_or_else(
243                         move || {
244                             applicability = Applicability::HasPlaceholders;
245                             Cow::Borrowed("v")
246                         },
247                         move |expr| snippet_with_applicability(cx, expr.span, "v", &mut applicability),
248                     );
249
250                     span_lint_and_sugg(
251                         cx,
252                         WRITELN_EMPTY_STRING,
253                         mac.span,
254                         format!("using `writeln!({}, \"\")`", suggestion).as_str(),
255                         "replace it with",
256                         format!("writeln!({})", suggestion),
257                         applicability,
258                     );
259                 }
260             }
261         }
262     }
263 }
264
265 /// Checks the arguments of `print[ln]!` and `write[ln]!` calls. It will return a tuple of two
266 /// options. The first part of the tuple is `format_str` of the macros. The secund part of the tuple
267 /// is in the `write[ln]!` case the expression the `format_str` should be written to.
268 ///
269 /// Example:
270 ///
271 /// Calling this function on
272 /// ```rust,ignore
273 /// writeln!(buf, "string to write: {}", something)
274 /// ```
275 /// will return
276 /// ```rust,ignore
277 /// (Some("string to write: {}"), Some(buf))
278 /// ```
279 fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -> (Option<String>, Option<Expr>) {
280     use crate::fmt_macros::*;
281     let tts = TokenStream::from(tts.clone());
282     let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false);
283     let mut expr: Option<Expr> = None;
284     if is_write {
285         expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
286             Ok(p) => Some(p.into_inner()),
287             Err(_) => return (None, None),
288         };
289         // might be `writeln!(foo)`
290         if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() {
291             return (None, expr);
292         }
293     }
294
295     let fmtstr = match parser.parse_str().map_err(|mut err| err.cancel()) {
296         Ok(token) => token.0.to_string(),
297         Err(_) => return (None, expr),
298     };
299     let tmp = fmtstr.clone();
300     let mut args = vec![];
301     let mut fmt_parser = Parser::new(&tmp, None);
302     while let Some(piece) = fmt_parser.next() {
303         if !fmt_parser.errors.is_empty() {
304             return (None, expr);
305         }
306         if let Piece::NextArgument(arg) = piece {
307             if arg.format.ty == "?" {
308                 // FIXME: modify rustc's fmt string parser to give us the current span
309                 span_lint(cx, USE_DEBUG, parser.prev_span, "use of `Debug`-based formatting");
310             }
311             args.push(arg);
312         }
313     }
314     let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL };
315     let mut idx = 0;
316     loop {
317         const SIMPLE: FormatSpec<'_> = FormatSpec {
318             fill: None,
319             align: AlignUnknown,
320             flags: 0,
321             precision: CountImplied,
322             width: CountImplied,
323             ty: "",
324         };
325         if !parser.eat(&token::Comma) {
326             return (Some(fmtstr), expr);
327         }
328         let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
329             Ok(expr) => expr,
330             Err(_) => return (Some(fmtstr), None),
331         };
332         match &token_expr.node {
333             ExprKind::Lit(_) => {
334                 let mut all_simple = true;
335                 let mut seen = false;
336                 for arg in &args {
337                     match arg.position {
338                         ArgumentImplicitlyIs(n) | ArgumentIs(n) => {
339                             if n == idx {
340                                 all_simple &= arg.format == SIMPLE;
341                                 seen = true;
342                             }
343                         },
344                         ArgumentNamed(_) => {},
345                     }
346                 }
347                 if all_simple && seen {
348                     span_lint(cx, lint, token_expr.span, "literal with an empty format string");
349                 }
350                 idx += 1;
351             },
352             ExprKind::Assign(lhs, rhs) => {
353                 if let ExprKind::Lit(_) = rhs.node {
354                     if let ExprKind::Path(_, p) = &lhs.node {
355                         let mut all_simple = true;
356                         let mut seen = false;
357                         for arg in &args {
358                             match arg.position {
359                                 ArgumentImplicitlyIs(_) | ArgumentIs(_) => {},
360                                 ArgumentNamed(name) => {
361                                     if *p == name {
362                                         seen = true;
363                                         all_simple &= arg.format == SIMPLE;
364                                     }
365                                 },
366                             }
367                         }
368                         if all_simple && seen {
369                             span_lint(cx, lint, rhs.span, "literal with an empty format string");
370                         }
371                     }
372                 }
373             },
374             _ => idx += 1,
375         }
376     }
377 }