]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #55138 - zackmdavis:the_paren_trap, r=pnkfelix
authorPietro Albini <pietro@pietroalbini.org>
Thu, 25 Oct 2018 12:31:01 +0000 (14:31 +0200)
committerGitHub <noreply@github.com>
Thu, 25 Oct 2018 12:31:01 +0000 (14:31 +0200)
in which unused-parens suggestions heed what the user actually wrote

Aaron Hill pointed out that unnecessary parens around a macro call (paradigmatically, `format!`) yielded a suggestion of hideous macro-expanded code. `span_to_snippet` is fallable as far as the type system is concerned, so the pretty-printing can live on in the oft-neglected `else` branch.

Resolves #55109.

1  2 
src/librustc_lint/unused.rs

index 4cf2072e792ca3ee6a50420f4df9c27177f71f67,e6909697322913ecf1a950cc6576801b8981ec4d..96d04253cc48549f3fd981e0a774ee922a21ab44
@@@ -276,16 -276,18 +276,21 @@@ impl UnusedParens 
                                  cx: &EarlyContext,
                                  value: &ast::Expr,
                                  msg: &str,
 -                                struct_lit_needs_parens: bool) {
 +                                followed_by_block: bool) {
          if let ast::ExprKind::Paren(ref inner) = value.node {
 -            let necessary = struct_lit_needs_parens &&
 -                            parser::contains_exterior_struct_lit(&inner);
 +            let necessary = followed_by_block && if let ast::ExprKind::Ret(_) = inner.node {
 +                true
 +            } else {
 +                parser::contains_exterior_struct_lit(&inner)
 +            };
              if !necessary {
-                 let pattern = pprust::expr_to_string(value);
-                 Self::remove_outer_parens(cx, value.span, &pattern, msg);
+                 let expr_text = if let Ok(snippet) = cx.sess().source_map()
+                     .span_to_snippet(value.span) {
+                         snippet
+                     } else {
+                         pprust::expr_to_string(value)
+                     };
+                 Self::remove_outer_parens(cx, value.span, &expr_text, msg);
              }
          }
      }