]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/write.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / write.rs
index 5c9fc943492c43d83f4a2b0270bf76faba03e828..7d72a21ac0360cdb22f826d2b0aa2c71418b23a5 100644 (file)
@@ -1,12 +1,12 @@
-use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, sym};
+use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then};
 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
 use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_errors::Applicability;
 use std::borrow::Cow;
 use syntax::ast::*;
 use syntax::parse::{parser, token};
-use syntax::symbol::Symbol;
-use syntax::tokenstream::{TokenStream, TokenTree};
+use syntax::tokenstream::TokenStream;
+use syntax_pos::{BytePos, Span};
 
 declare_clippy_lint! {
     /// **What it does:** This lint warns when you use `println!("")` to
     /// lint is to catch debugging remnants.
     ///
     /// **Why is this bad?** The purpose of the `Debug` trait is to facilitate
-    /// debugging Rust code. It should not be used in in user-facing output.
+    /// debugging Rust code. It should not be used in user-facing output.
     ///
     /// **Example:**
     /// ```rust
+    /// # let foo = "bar";
     /// println!("{:?}", foo);
     /// ```
     pub USE_DEBUG,
 
 impl EarlyLintPass for Write {
     fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
-        if mac.node.path == *sym::println {
+        if mac.path == sym!(println) {
             span_lint(cx, PRINT_STDOUT, mac.span, "use of `println!`");
-            if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
-                if fmtstr == "" {
+            if let (Some(fmt_str), _) = check_tts(cx, &mac.tts, false) {
+                if fmt_str.contents.is_empty() {
                     span_lint_and_sugg(
                         cx,
                         PRINTLN_EMPTY_STRING,
@@ -197,37 +198,54 @@ fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
                     );
                 }
             }
-        } else if mac.node.path == *sym::print {
+        } else if mac.path == sym!(print) {
             span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
-            if let (Some(fmtstr), _, is_raw) = check_tts(cx, &mac.node.tts, false) {
-                if check_newlines(&fmtstr, is_raw) {
-                    span_lint(
+            if let (Some(fmt_str), _) = check_tts(cx, &mac.tts, false) {
+                if check_newlines(&fmt_str) {
+                    span_lint_and_then(
                         cx,
                         PRINT_WITH_NEWLINE,
                         mac.span,
-                        "using `print!()` with a format string that ends in a \
-                         single newline, consider using `println!()` instead",
+                        "using `print!()` with a format string that ends in a single newline",
+                        |err| {
+                            err.multipart_suggestion(
+                                "use `println!` instead",
+                                vec![
+                                    (mac.path.span, String::from("println")),
+                                    (fmt_str.newline_span(), String::new()),
+                                ],
+                                Applicability::MachineApplicable,
+                            );
+                        },
                     );
                 }
             }
-        } else if mac.node.path == *sym::write {
-            if let (Some(fmtstr), _, is_raw) = check_tts(cx, &mac.node.tts, true) {
-                if check_newlines(&fmtstr, is_raw) {
-                    span_lint(
+        } else if mac.path == sym!(write) {
+            if let (Some(fmt_str), _) = check_tts(cx, &mac.tts, true) {
+                if check_newlines(&fmt_str) {
+                    span_lint_and_then(
                         cx,
                         WRITE_WITH_NEWLINE,
                         mac.span,
-                        "using `write!()` with a format string that ends in a \
-                         single newline, consider using `writeln!()` instead",
-                    );
+                        "using `write!()` with a format string that ends in a single newline",
+                        |err| {
+                            err.multipart_suggestion(
+                                "use `writeln!()` instead",
+                                vec![
+                                    (mac.path.span, String::from("writeln")),
+                                    (fmt_str.newline_span(), String::new()),
+                                ],
+                                Applicability::MachineApplicable,
+                            );
+                        },
+                    )
                 }
             }
-        } else if mac.node.path == *sym::writeln {
-            let check_tts = check_tts(cx, &mac.node.tts, true);
-            if let Some(fmtstr) = check_tts.0 {
-                if fmtstr == "" {
+        } else if mac.path == sym!(writeln) {
+            if let (Some(fmt_str), expr) = check_tts(cx, &mac.tts, true) {
+                if fmt_str.contents.is_empty() {
                     let mut applicability = Applicability::MachineApplicable;
-                    let suggestion = check_tts.1.map_or_else(
+                    let suggestion = expr.map_or_else(
                         move || {
                             applicability = Applicability::HasPlaceholders;
                             Cow::Borrowed("v")
@@ -250,10 +268,44 @@ fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
     }
 }
 
+/// The arguments of a `print[ln]!` or `write[ln]!` invocation.
+struct FmtStr {
+    /// The contents of the format string (inside the quotes).
+    contents: String,
+    style: StrStyle,
+    /// The span of the format string, including quotes, the raw marker, and any raw hashes.
+    span: Span,
+}
+
+impl FmtStr {
+    /// Given a format string that ends in a newline and its span, calculates the span of the
+    /// newline.
+    fn newline_span(&self) -> Span {
+        let sp = self.span;
+
+        let newline_sp_hi = sp.hi()
+            - match self.style {
+                StrStyle::Cooked => BytePos(1),
+                StrStyle::Raw(hashes) => BytePos((1 + hashes).into()),
+            };
+
+        let newline_sp_len = if self.contents.ends_with('\n') {
+            BytePos(1)
+        } else if self.contents.ends_with(r"\n") {
+            BytePos(2)
+        } else {
+            panic!("expected format string to contain a newline");
+        };
+
+        sp.with_lo(newline_sp_hi - newline_sp_len).with_hi(newline_sp_hi)
+    }
+}
+
 /// Checks the arguments of `print[ln]!` and `write[ln]!` calls. It will return a tuple of two
-/// options and a bool. The first part of the tuple is `format_str` of the macros. The second part
-/// of the tuple is in the `write[ln]!` case the expression the `format_str` should be written to.
-/// The final part is a boolean flag indicating if the string is a raw string.
+/// `Option`s. The first `Option` of the tuple is the macro's format string. It includes
+/// the contents of the string, whether it's a raw string, and the span of the literal in the
+/// source. The second `Option` in the tuple is, in the `write[ln]!` case, the expression the
+/// `format_str` should be written to.
 ///
 /// Example:
 ///
@@ -266,49 +318,37 @@ fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
 /// ```
 /// will return
 /// ```rust,ignore
-/// (Some("string to write: {}"), Some(buf), false)
+/// (Some("string to write: {}"), Some(buf))
 /// ```
-fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (Option<String>, Option<Expr>, bool) {
+#[allow(clippy::too_many_lines)]
+fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (Option<FmtStr>, Option<Expr>) {
     use fmt_macros::*;
     let tts = tts.clone();
-    let mut is_raw = false;
-    if let TokenStream(Some(tokens)) = &tts {
-        for token in tokens.iter() {
-            if let (TokenTree::Token(_, token::Token::Literal(lit, _)), _) = token {
-                match lit {
-                    token::Lit::Str_(_) => break,
-                    token::Lit::StrRaw(_, _) => {
-                        is_raw = true;
-                        break;
-                    },
-                    _ => {},
-                }
-            }
-        }
-    }
-    let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false);
+
+    let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false, None);
     let mut expr: Option<Expr> = None;
     if is_write {
         expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
             Ok(p) => Some(p.into_inner()),
-            Err(_) => return (None, None, is_raw),
+            Err(_) => return (None, None),
         };
         // might be `writeln!(foo)`
         if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() {
-            return (None, expr, is_raw);
+            return (None, expr);
         }
     }
 
-    let fmtstr = match parser.parse_str().map_err(|mut err| err.cancel()) {
-        Ok(token) => token.0.to_string(),
-        Err(_) => return (None, expr, is_raw),
+    let (fmtstr, fmtstyle) = match parser.parse_str().map_err(|mut err| err.cancel()) {
+        Ok((fmtstr, fmtstyle)) => (fmtstr.to_string(), fmtstyle),
+        Err(_) => return (None, expr),
     };
+    let fmtspan = parser.prev_span;
     let tmp = fmtstr.clone();
     let mut args = vec![];
     let mut fmt_parser = Parser::new(&tmp, None, Vec::new(), false);
     while let Some(piece) = fmt_parser.next() {
         if !fmt_parser.errors.is_empty() {
-            return (None, expr, is_raw);
+            return (None, expr);
         }
         if let Piece::NextArgument(arg) = piece {
             if arg.format.ty == "?" {
@@ -326,15 +366,32 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (O
             align: AlignUnknown,
             flags: 0,
             precision: CountImplied,
+            precision_span: None,
             width: CountImplied,
+            width_span: None,
             ty: "",
         };
         if !parser.eat(&token::Comma) {
-            return (Some(fmtstr), expr, is_raw);
+            return (
+                Some(FmtStr {
+                    contents: fmtstr,
+                    style: fmtstyle,
+                    span: fmtspan,
+                }),
+                expr,
+            );
         }
-        let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
-            Ok(expr) => expr,
-            Err(_) => return (Some(fmtstr), None, is_raw),
+        let token_expr = if let Ok(expr) = parser.parse_expr().map_err(|mut err| err.cancel()) {
+            expr
+        } else {
+            return (
+                Some(FmtStr {
+                    contents: fmtstr,
+                    style: fmtstyle,
+                    span: fmtspan,
+                }),
+                None,
+            );
         };
         match &token_expr.node {
             ExprKind::Lit(_) => {
@@ -365,9 +422,7 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (O
                             match arg.position {
                                 ArgumentImplicitlyIs(_) | ArgumentIs(_) => {},
                                 ArgumentNamed(name) => {
-                                    // FIXME: remove this interning if possible
-                                    // https://github.com/rust-lang/rust/issues/60795
-                                    if *p == Symbol::intern(name) {
+                                    if *p == name {
                                         seen = true;
                                         all_simple &= arg.format == SIMPLE;
                                     }
@@ -385,12 +440,15 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (O
     }
 }
 
-// Checks if `s` constains a single newline that terminates it
-// Literal and escaped newlines are both checked (only literal for raw strings)
-fn check_newlines(s: &str, is_raw: bool) -> bool {
+/// Checks if the format string constains a single newline that terminates it.
+///
+/// Literal and escaped newlines are both checked (only literal for raw strings).
+fn check_newlines(fmt_str: &FmtStr) -> bool {
+    let s = &fmt_str.contents;
+
     if s.ends_with('\n') {
         return true;
-    } else if is_raw {
+    } else if let StrStyle::Raw(_) = fmt_str.style {
         return false;
     }