1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
11 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::rustc_errors::Applicability;
14 use crate::syntax::ast::*;
15 use crate::syntax::parse::{parser, token};
16 use crate::syntax::tokenstream::{ThinTokenStream, TokenStream};
17 use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg};
20 /// **What it does:** This lint warns when you use `println!("")` to
23 /// **Why is this bad?** You should use `println!()`, which is simpler.
25 /// **Known problems:** None.
31 declare_clippy_lint! {
32 pub PRINTLN_EMPTY_STRING,
34 "using `println!(\"\")` with an empty string"
37 /// **What it does:** This lint warns when you use `print!()` with a format
39 /// ends in a newline.
41 /// **Why is this bad?** You should use `println!()` instead, which appends the
44 /// **Known problems:** None.
48 /// print!("Hello {}!\n", name);
50 /// use println!() instead
52 /// println!("Hello {}!", name);
54 declare_clippy_lint! {
55 pub PRINT_WITH_NEWLINE,
57 "using `print!()` with a format string that ends in a single newline"
60 /// **What it does:** Checks for printing on *stdout*. The purpose of this lint
61 /// is to catch debugging remnants.
63 /// **Why is this bad?** People often print on *stdout* while debugging an
64 /// application and might forget to remove those prints afterward.
66 /// **Known problems:** Only catches `print!` and `println!` calls.
70 /// println!("Hello world!");
72 declare_clippy_lint! {
78 /// **What it does:** Checks for use of `Debug` formatting. The purpose of this
79 /// lint is to catch debugging remnants.
81 /// **Why is this bad?** The purpose of the `Debug` trait is to facilitate
82 /// debugging Rust code. It should not be used in in user-facing output.
86 /// println!("{:?}", foo);
88 declare_clippy_lint! {
91 "use of `Debug`-based formatting"
94 /// **What it does:** This lint warns about the use of literals as `print!`/`println!` args.
96 /// **Why is this bad?** Using literals as `println!` args is inefficient
97 /// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
98 /// (i.e., just put the literal in the format string)
100 /// **Known problems:** Will also warn with macro calls as arguments that expand to literals
101 /// -- e.g., `println!("{}", env!("FOO"))`.
105 /// println!("{}", "foo");
107 /// use the literal without formatting:
111 declare_clippy_lint! {
114 "printing a literal with a format string"
117 /// **What it does:** This lint warns when you use `writeln!(buf, "")` to
120 /// **Why is this bad?** You should use `writeln!(buf)`, which is simpler.
122 /// **Known problems:** None.
128 declare_clippy_lint! {
129 pub WRITELN_EMPTY_STRING,
131 "using `writeln!(\"\")` with an empty string"
134 /// **What it does:** This lint warns when you use `write!()` with a format
136 /// ends in a newline.
138 /// **Why is this bad?** You should use `writeln!()` instead, which appends the
141 /// **Known problems:** None.
145 /// write!(buf, "Hello {}!\n", name);
147 declare_clippy_lint! {
148 pub WRITE_WITH_NEWLINE,
150 "using `write!()` with a format string that ends in a single newline"
153 /// **What it does:** This lint warns about the use of literals as `write!`/`writeln!` args.
155 /// **Why is this bad?** Using literals as `writeln!` args is inefficient
156 /// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
157 /// (i.e., just put the literal in the format string)
159 /// **Known problems:** Will also warn with macro calls as arguments that expand to literals
160 /// -- e.g., `writeln!(buf, "{}", env!("FOO"))`.
164 /// writeln!(buf, "{}", "foo");
166 declare_clippy_lint! {
169 "writing a literal with a format string"
172 #[derive(Copy, Clone, Debug)]
175 impl LintPass for Pass {
176 fn get_lints(&self) -> LintArray {
179 PRINTLN_EMPTY_STRING,
184 WRITELN_EMPTY_STRING,
190 impl EarlyLintPass for Pass {
191 fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
192 if mac.node.path == "println" {
193 span_lint(cx, PRINT_STDOUT, mac.span, "use of `println!`");
194 if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
198 PRINTLN_EMPTY_STRING,
200 "using `println!(\"\")`",
202 "println!()".to_string(),
203 Applicability::MachineApplicable,
207 } else if mac.node.path == "print" {
208 span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
209 if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
210 if fmtstr.ends_with("\\n") &&
211 // don't warn about strings with several `\n`s (#3126)
212 fmtstr.matches("\\n").count() == 1
218 "using `print!()` with a format string that ends in a \
219 single newline, consider using `println!()` instead",
223 } else if mac.node.path == "write" {
224 if let Some(fmtstr) = check_tts(cx, &mac.node.tts, true).0 {
225 if fmtstr.ends_with("\\n") &&
226 // don't warn about strings with several `\n`s (#3126)
227 fmtstr.matches("\\n").count() == 1
233 "using `write!()` with a format string that ends in a \
234 single newline, consider using `writeln!()` instead",
238 } else if mac.node.path == "writeln" {
239 let check_tts = check_tts(cx, &mac.node.tts, true);
240 if let Some(fmtstr) = check_tts.0 {
242 let mut applicability = Applicability::MachineApplicable;
243 let suggestion = check_tts.1.map_or_else(
245 applicability = Applicability::HasPlaceholders;
248 move |expr| snippet_with_applicability(cx, expr.span, "v", &mut applicability),
253 WRITELN_EMPTY_STRING,
255 format!("using `writeln!({}, \"\")`", suggestion).as_str(),
257 format!("writeln!({})", suggestion),
266 /// Checks the arguments of `print[ln]!` and `write[ln]!` calls. It will return a tuple of two
267 /// options. The first part of the tuple is `format_str` of the macros. The secund part of the tuple
268 /// is in the `write[ln]!` case the expression the `format_str` should be written to.
272 /// Calling this function on
274 /// writeln!(buf, "string to write: {}", something)
278 /// (Some("string to write: {}"), Some(buf))
280 fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -> (Option<String>, Option<Expr>) {
281 use crate::fmt_macros::*;
282 let tts = TokenStream::from(tts.clone());
283 let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false);
284 let mut expr: Option<Expr> = None;
286 expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
287 Ok(p) => Some(p.into_inner()),
288 Err(_) => return (None, None),
290 // might be `writeln!(foo)`
291 if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() {
296 let fmtstr = match parser.parse_str().map_err(|mut err| err.cancel()) {
297 Ok(token) => token.0.to_string(),
298 Err(_) => return (None, expr),
300 let tmp = fmtstr.clone();
301 let mut args = vec![];
302 let mut fmt_parser = Parser::new(&tmp, None);
303 while let Some(piece) = fmt_parser.next() {
304 if !fmt_parser.errors.is_empty() {
307 if let Piece::NextArgument(arg) = piece {
308 if arg.format.ty == "?" {
309 // FIXME: modify rustc's fmt string parser to give us the current span
310 span_lint(cx, USE_DEBUG, parser.prev_span, "use of `Debug`-based formatting");
315 let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL };
318 const SIMPLE: FormatSpec<'_> = FormatSpec {
322 precision: CountImplied,
326 if !parser.eat(&token::Comma) {
327 return (Some(fmtstr), expr);
329 let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
331 Err(_) => return (Some(fmtstr), None),
333 match &token_expr.node {
334 ExprKind::Lit(_) => {
335 let mut all_simple = true;
336 let mut seen = false;
339 ArgumentImplicitlyIs(n) | ArgumentIs(n) => if n == idx {
340 all_simple &= arg.format == SIMPLE;
343 ArgumentNamed(_) => {},
346 if all_simple && seen {
347 span_lint(cx, lint, token_expr.span, "literal with an empty format string");
351 ExprKind::Assign(lhs, rhs) => {
352 if let ExprKind::Lit(_) = rhs.node {
353 if let ExprKind::Path(_, p) = &lhs.node {
354 let mut all_simple = true;
355 let mut seen = false;
358 ArgumentImplicitlyIs(_) | ArgumentIs(_) => {},
359 ArgumentNamed(name) => if *p == name {
361 all_simple &= arg.format == SIMPLE;
365 if all_simple && seen {
366 span_lint(cx, lint, rhs.span, "literal with an empty format string");