1 use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg};
2 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3 use rustc::{declare_lint_pass, declare_tool_lint};
4 use rustc_errors::Applicability;
7 use syntax::parse::{parser, token};
8 use syntax::tokenstream::{TokenStream, TokenTree};
9 use syntax_pos::symbol::Symbol;
11 declare_clippy_lint! {
12 /// **What it does:** This lint warns when you use `println!("")` to
15 /// **Why is this bad?** You should use `println!()`, which is simpler.
17 /// **Known problems:** None.
23 pub PRINTLN_EMPTY_STRING,
25 "using `println!(\"\")` with an empty string"
28 declare_clippy_lint! {
29 /// **What it does:** This lint warns when you use `print!()` with a format
31 /// ends in a newline.
33 /// **Why is this bad?** You should use `println!()` instead, which appends the
36 /// **Known problems:** None.
40 /// # let name = "World";
41 /// print!("Hello {}!\n", name);
43 /// use println!() instead
45 /// # let name = "World";
46 /// println!("Hello {}!", name);
48 pub PRINT_WITH_NEWLINE,
50 "using `print!()` with a format string that ends in a single newline"
53 declare_clippy_lint! {
54 /// **What it does:** Checks for printing on *stdout*. The purpose of this lint
55 /// is to catch debugging remnants.
57 /// **Why is this bad?** People often print on *stdout* while debugging an
58 /// application and might forget to remove those prints afterward.
60 /// **Known problems:** Only catches `print!` and `println!` calls.
64 /// println!("Hello world!");
71 declare_clippy_lint! {
72 /// **What it does:** Checks for use of `Debug` formatting. The purpose of this
73 /// lint is to catch debugging remnants.
75 /// **Why is this bad?** The purpose of the `Debug` trait is to facilitate
76 /// debugging Rust code. It should not be used in in user-facing output.
80 /// println!("{:?}", foo);
84 "use of `Debug`-based formatting"
87 declare_clippy_lint! {
88 /// **What it does:** This lint warns about the use of literals as `print!`/`println!` args.
90 /// **Why is this bad?** Using literals as `println!` args is inefficient
91 /// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
92 /// (i.e., just put the literal in the format string)
94 /// **Known problems:** Will also warn with macro calls as arguments that expand to literals
95 /// -- e.g., `println!("{}", env!("FOO"))`.
99 /// println!("{}", "foo");
101 /// use the literal without formatting:
107 "printing a literal with a format string"
110 declare_clippy_lint! {
111 /// **What it does:** This lint warns when you use `writeln!(buf, "")` to
114 /// **Why is this bad?** You should use `writeln!(buf)`, which is simpler.
116 /// **Known problems:** None.
120 /// # use std::fmt::Write;
121 /// # let mut buf = String::new();
122 /// writeln!(buf, "");
124 pub WRITELN_EMPTY_STRING,
126 "using `writeln!(buf, \"\")` with an empty string"
129 declare_clippy_lint! {
130 /// **What it does:** This lint warns when you use `write!()` with a format
132 /// ends in a newline.
134 /// **Why is this bad?** You should use `writeln!()` instead, which appends the
137 /// **Known problems:** None.
141 /// # use std::fmt::Write;
142 /// # let mut buf = String::new();
143 /// # let name = "World";
144 /// write!(buf, "Hello {}!\n", name);
146 pub WRITE_WITH_NEWLINE,
148 "using `write!()` with a format string that ends in a single newline"
151 declare_clippy_lint! {
152 /// **What it does:** This lint warns about the use of literals as `write!`/`writeln!` args.
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)
158 /// **Known problems:** Will also warn with macro calls as arguments that expand to literals
159 /// -- e.g., `writeln!(buf, "{}", env!("FOO"))`.
163 /// # use std::fmt::Write;
164 /// # let mut buf = String::new();
165 /// writeln!(buf, "{}", "foo");
169 "writing a literal with a format string"
172 declare_lint_pass!(Write => [
174 PRINTLN_EMPTY_STRING,
179 WRITELN_EMPTY_STRING,
183 impl EarlyLintPass for Write {
184 fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
185 if mac.node.path == sym!(println) {
186 span_lint(cx, PRINT_STDOUT, mac.span, "use of `println!`");
187 if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
191 PRINTLN_EMPTY_STRING,
193 "using `println!(\"\")`",
195 "println!()".to_string(),
196 Applicability::MachineApplicable,
200 } else if mac.node.path == sym!(print) {
201 span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
202 if let (Some(fmtstr), _, is_raw) = check_tts(cx, &mac.node.tts, false) {
203 if check_newlines(&fmtstr, is_raw) {
208 "using `print!()` with a format string that ends in a \
209 single newline, consider using `println!()` instead",
213 } else if mac.node.path == sym!(write) {
214 if let (Some(fmtstr), _, is_raw) = check_tts(cx, &mac.node.tts, true) {
215 if check_newlines(&fmtstr, is_raw) {
220 "using `write!()` with a format string that ends in a \
221 single newline, consider using `writeln!()` instead",
225 } else if mac.node.path == sym!(writeln) {
226 let check_tts = check_tts(cx, &mac.node.tts, true);
227 if let Some(fmtstr) = check_tts.0 {
229 let mut applicability = Applicability::MachineApplicable;
230 let suggestion = check_tts.1.map_or_else(
232 applicability = Applicability::HasPlaceholders;
235 move |expr| snippet_with_applicability(cx, expr.span, "v", &mut applicability),
240 WRITELN_EMPTY_STRING,
242 format!("using `writeln!({}, \"\")`", suggestion).as_str(),
244 format!("writeln!({})", suggestion),
253 /// Checks the arguments of `print[ln]!` and `write[ln]!` calls. It will return a tuple of two
254 /// options and a bool. The first part of the tuple is `format_str` of the macros. The second part
255 /// of the tuple is in the `write[ln]!` case the expression the `format_str` should be written to.
256 /// The final part is a boolean flag indicating if the string is a raw string.
260 /// Calling this function on
262 /// # use std::fmt::Write;
263 /// # let mut buf = String::new();
264 /// # let something = "something";
265 /// writeln!(buf, "string to write: {}", something);
269 /// (Some("string to write: {}"), Some(buf), false)
271 fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (Option<String>, Option<Expr>, bool) {
273 let tts = tts.clone();
274 let mut is_raw = false;
275 if let TokenStream(Some(tokens)) = &tts {
276 for token in tokens.iter() {
277 if let (TokenTree::Token(_, token::Token::Literal(lit)), _) = token {
280 token::StrRaw(_) => {
289 let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false);
290 let mut expr: Option<Expr> = None;
292 expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
293 Ok(p) => Some(p.into_inner()),
294 Err(_) => return (None, None, is_raw),
296 // might be `writeln!(foo)`
297 if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() {
298 return (None, expr, is_raw);
302 let fmtstr = match parser.parse_str().map_err(|mut err| err.cancel()) {
303 Ok(token) => token.0.to_string(),
304 Err(_) => return (None, expr, is_raw),
306 let tmp = fmtstr.clone();
307 let mut args = vec![];
308 let mut fmt_parser = Parser::new(&tmp, None, Vec::new(), false);
309 while let Some(piece) = fmt_parser.next() {
310 if !fmt_parser.errors.is_empty() {
311 return (None, expr, is_raw);
313 if let Piece::NextArgument(arg) = piece {
314 if arg.format.ty == "?" {
315 // FIXME: modify rustc's fmt string parser to give us the current span
316 span_lint(cx, USE_DEBUG, parser.prev_span, "use of `Debug`-based formatting");
321 let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL };
324 const SIMPLE: FormatSpec<'_> = FormatSpec {
328 precision: CountImplied,
332 if !parser.eat(&token::Comma) {
333 return (Some(fmtstr), expr, is_raw);
335 let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
337 Err(_) => return (Some(fmtstr), None, is_raw),
339 match &token_expr.node {
340 ExprKind::Lit(_) => {
341 let mut all_simple = true;
342 let mut seen = false;
345 ArgumentImplicitlyIs(n) | ArgumentIs(n) => {
347 all_simple &= arg.format == SIMPLE;
351 ArgumentNamed(_) => {},
354 if all_simple && seen {
355 span_lint(cx, lint, token_expr.span, "literal with an empty format string");
359 ExprKind::Assign(lhs, rhs) => {
360 if let ExprKind::Lit(_) = rhs.node {
361 if let ExprKind::Path(_, p) = &lhs.node {
362 let mut all_simple = true;
363 let mut seen = false;
366 ArgumentImplicitlyIs(_) | ArgumentIs(_) => {},
367 ArgumentNamed(name) => {
368 if *p == Symbol::intern(name) {
370 all_simple &= arg.format == SIMPLE;
375 if all_simple && seen {
376 span_lint(cx, lint, rhs.span, "literal with an empty format string");
386 // Checks if `s` constains a single newline that terminates it
387 // Literal and escaped newlines are both checked (only literal for raw strings)
388 fn check_newlines(s: &str, is_raw: bool) -> bool {
389 if s.ends_with('\n') {
399 let bytes = s.as_bytes();
400 if bytes[bytes.len() - 2] != b'\\' || bytes[bytes.len() - 1] != b'n' {
404 let mut escaping = false;
405 for (index, &byte) in bytes.iter().enumerate() {
408 return index == bytes.len() - 1;
411 } else if byte == b'\\' {