]> git.lizzy.rs Git - rust.git/commitdiff
Make string lit formatting fallible
authorNick Cameron <ncameron@mozilla.com>
Thu, 26 Jan 2017 19:04:13 +0000 (08:04 +1300)
committerNick Cameron <ncameron@mozilla.com>
Thu, 26 Jan 2017 19:04:52 +0000 (08:04 +1300)
This allows expressions containing string lits to wrap their content

Fixes #1282

src/expr.rs
tests/source/string-lit.rs
tests/target/string-lit.rs
tests/target/string_punctuation.rs

index c7d1ee3b680ede52b85239e0e5a74220381d0a7b..435155de28fc8524f56c8baf04fecde9d2c38d84 100644 (file)
@@ -64,7 +64,7 @@ fn format_expr(expr: &ast::Expr,
         ast::ExprKind::Lit(ref l) => {
             match l.node {
                 ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
-                    Some(rewrite_string_lit(context, l.span, width, offset))
+                    rewrite_string_lit(context, l.span, width, offset)
                 }
                 _ => {
                     wrap_str(context.snippet(expr.span),
@@ -586,6 +586,7 @@ fn and_one_line(x: Option<String>) -> Option<String> {
 }
 
 fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> {
+    debug!("nop_block_collapse {:?} {}", block_str, budget);
     block_str.map(|block_str| if block_str.starts_with('{') && budget >= 2 &&
                                  (block_str[1..].find(|c: char| !c.is_whitespace()).unwrap() ==
                                   block_str.len() - 2) {
@@ -1219,6 +1220,7 @@ fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str
 // Match arms.
 impl Rewrite for ast::Arm {
     fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
+        debug!("Arm::rewrite {:?} {} {:?}", self, width, offset);
         let &ast::Arm { ref attrs, ref pats, ref guard, ref body } = self;
 
         // FIXME this is all a bit grotty, would be nice to abstract out the
@@ -1310,13 +1312,14 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                 false
             };
 
-            let block_sep = match context.config.control_brace_style {
-                ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(),
-                _ => " ",
-            };
             match rewrite {
                 Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms ||
                                       is_block => {
+                    let block_sep = match context.config.control_brace_style {
+                        ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(),
+                        _ => " ",
+                    };
+
                     return Some(format!("{}{} =>{}{}{}",
                                         attr_str.trim_left(),
                                         pats_str,
@@ -1477,16 +1480,16 @@ fn rewrite_string_lit(context: &RewriteContext,
                       span: Span,
                       width: usize,
                       offset: Indent)
-                      -> String {
+                      -> Option<String> {
     let string_lit = context.snippet(span);
 
     if !context.config.format_strings && !context.config.force_format_strings {
-        return string_lit;
+        return Some(string_lit);
     }
 
     if !context.config.force_format_strings &&
        !string_requires_rewrite(context, span, &string_lit, width, offset) {
-        return string_lit;
+        return Some(string_lit);
     }
 
     let fmt = StringFormat {
@@ -1503,7 +1506,7 @@ fn rewrite_string_lit(context: &RewriteContext,
     // Remove the quote characters.
     let str_lit = &string_lit[1..string_lit.len() - 1];
 
-    rewrite_string(str_lit, &fmt).unwrap_or_else(|| string_lit.to_owned())
+    rewrite_string(str_lit, &fmt)
 }
 
 fn string_requires_rewrite(context: &RewriteContext,
index ca8aa36a90d8f63295e564ffd5b10f8c472f7ceb..59547cbe62f9e9ffc7f129f1ce4a74a1a838236b 100644 (file)
@@ -45,3 +45,13 @@ fn issue716() {
     println!("forall x. mult(e(), x) = x /\\
               forall x. mult(x, x) = e()");
 }
+
+fn issue_1282() {
+    {
+        match foo {
+            Permission::AndroidPermissionAccessLocationExtraCommands => {
+                "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
+            }
+        }
+    }
+}
index 1d4d21ff73e97763f03144798b5456ef16c5d7c6..4fba80e1c5161ca9c80daab258aa5938b6fee153 100644 (file)
@@ -10,8 +10,10 @@ fn main() -> &'static str {
     let too_many_lines = "Hello";
 
     // Make sure we don't break after an escape character.
-    let odd_length_name = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
-    let even_length_name = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
+    let odd_length_name =
+        "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
+    let even_length_name =
+        "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
 
     let really_long_variable_name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
 
@@ -22,7 +24,8 @@ fn main() -> &'static str {
 
     filename.replace(" ", "\\");
 
-    let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = funktion("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
+    let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx =
+        funktion("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
 
     let unicode = "a̐éö̲\r\n";
     let unicode2 = "Löwe 老虎 Léopard";
@@ -41,3 +44,13 @@ fn issue716() {
     println!("forall x. mult(e(), x) = x /\\
               forall x. mult(x, x) = e()");
 }
+
+fn issue_1282() {
+    {
+        match foo {
+            Permission::AndroidPermissionAccessLocationExtraCommands => {
+                "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
+            }
+        }
+    }
+}
index 1acad4043572fa77fef76d708a26eb1aed8c1151..ba5a478a5e74c42b234334aa73fa79a9cf0f70b7 100644 (file)
@@ -1,12 +1,7 @@
 fn main() {
     println!("ThisIsAReallyLongStringWithNoSpaces.It_should_prefer_to_break_onpunctuation:\
               Likethisssssssssssss");
-    format!("{}__{}__{}ItShouldOnlyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyNoticeSemicolonsPeriodsColonsAndCommasAndResortToMid-CharBreaksAfterPunctuation{}{}",
-            x,
-            y,
-            z,
-            a,
-            b);
+    format!("{}__{}__{}ItShouldOnlyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyNoticeSemicolonsPeriodsColonsAndCommasAndResortToMid-CharBreaksAfterPunctuation{}{}",x,y,z,a,b);
     println!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaalhijalfhiigjapdighjapdigjapdighdapighapdighpaidhg;\
               adopgihadoguaadbadgad,qeoihapethae8t0aet8haetadbjtaeg;\
               ooeouthaoeutgadlgajduabgoiuadogabudogubaodugbadgadgadga;adoughaoeugbaouea");