From 1f3100d7bb2e4889ec243d2781b77e252fb29c5c Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 27 Jan 2017 08:04:13 +1300 Subject: [PATCH] Make string lit formatting fallible This allows expressions containing string lits to wrap their content Fixes #1282 --- src/expr.rs | 21 ++++++++++++--------- tests/source/string-lit.rs | 10 ++++++++++ tests/target/string-lit.rs | 19 ++++++++++++++++--- tests/target/string_punctuation.rs | 7 +------ 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index c7d1ee3b680e..435155de28fc 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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) -> Option { } fn nop_block_collapse(block_str: Option, budget: usize) -> Option { + 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 { + 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 @@ impl Rewrite for ast::Arm { 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 { 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, diff --git a/tests/source/string-lit.rs b/tests/source/string-lit.rs index ca8aa36a90d8..59547cbe62f9 100644 --- a/tests/source/string-lit.rs +++ b/tests/source/string-lit.rs @@ -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" + } + } + } +} diff --git a/tests/target/string-lit.rs b/tests/target/string-lit.rs index 1d4d21ff73e9..4fba80e1c516 100644 --- a/tests/target/string-lit.rs +++ b/tests/target/string-lit.rs @@ -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 @@ formatting"#; 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" + } + } + } +} diff --git a/tests/target/string_punctuation.rs b/tests/target/string_punctuation.rs index 1acad4043572..ba5a478a5e74 100644 --- a/tests/target/string_punctuation.rs +++ b/tests/target/string_punctuation.rs @@ -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");