diff --git a/rustfmt-core/rustfmt-lib/src/expr.rs b/rustfmt-core/rustfmt-lib/src/expr.rs index 5ac6c26ef88..a1d5c7e5c36 100644 --- a/rustfmt-core/rustfmt-lib/src/expr.rs +++ b/rustfmt-core/rustfmt-lib/src/expr.rs @@ -833,11 +833,8 @@ impl<'a> ControlFlow<'a> { .span_after(self.span, self.connector.trim()); let comments_span = mk_sp(comments_lo, expr.span.lo()); - let missing_comments = match rewrite_missing_comment( - comments_span, - cond_shape, - context, - ) { + let missing_comments = match rewrite_missing_comment(comments_span, cond_shape, context) + { None => "".to_owned(), Some(comment) if self.connector.is_empty() || comment.is_empty() => comment, // Handle same-line block comments: diff --git a/rustfmt-core/rustfmt-lib/src/patterns.rs b/rustfmt-core/rustfmt-lib/src/patterns.rs index 1ef3bd5337b..c49abc1a12e 100644 --- a/rustfmt-core/rustfmt-lib/src/patterns.rs +++ b/rustfmt-core/rustfmt-lib/src/patterns.rs @@ -55,6 +55,17 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool { } } +struct RangeOperand<'a>(&'a Option>); + +impl<'a> Rewrite for RangeOperand<'a> { + fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + match &self.0 { + None => Some("".to_owned()), + Some(ref exp) => exp.rewrite(context, shape), + } + } +} + impl Rewrite for Pat { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { match self.kind { @@ -179,29 +190,34 @@ impl Rewrite for Pat { None } } - PatKind::Range(ref lhs, ref rhs, ref end_kind) => match (lhs, rhs) { - (Some(lhs), Some(rhs)) => { - let infix = match end_kind.node { - RangeEnd::Included(RangeSyntax::DotDotDot) => "...", - RangeEnd::Included(RangeSyntax::DotDotEq) => "..=", - RangeEnd::Excluded => "..", + PatKind::Range(ref lhs, ref rhs, ref end_kind) => { + let infix = match end_kind.node { + RangeEnd::Included(RangeSyntax::DotDotDot) => "...", + RangeEnd::Included(RangeSyntax::DotDotEq) => "..=", + RangeEnd::Excluded => "..", + }; + let infix = if context.config.spaces_around_ranges() { + let lhs_spacing = match lhs { + None => "", + Some(_) => " ", }; - let infix = if context.config.spaces_around_ranges() { - format!(" {} ", infix) - } else { - infix.to_owned() + let rhs_spacing = match rhs { + None => "", + Some(_) => " ", }; - rewrite_pair( - &**lhs, - &**rhs, - PairParts::infix(&infix), - context, - shape, - SeparatorPlace::Front, - ) - } - (_, _) => unimplemented!(), - }, + format!("{}{}{}", lhs_spacing, infix, rhs_spacing) + } else { + infix.to_owned() + }; + rewrite_pair( + &RangeOperand(lhs), + &RangeOperand(rhs), + PairParts::infix(&infix), + context, + shape, + SeparatorPlace::Front, + ) + } PatKind::Ref(ref pat, mutability) => { let prefix = format!("&{}", format_mutability(mutability)); rewrite_unary_prefix(context, &prefix, &**pat, shape) diff --git a/rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/false.rs b/rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/false.rs index 11ca76b1345..1878c68a5a0 100644 --- a/rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/false.rs +++ b/rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/false.rs @@ -20,3 +20,15 @@ fn main() { _ => bar, } } + +fn half_open() { + match [5 .. 4, 99 .. 105, 43 .. 44] { + [_, 99 .., _] => {} + [_, .. 105, _] => {} + _ => {} + }; + + if let ..= 5 = 0 {} + if let .. 5 = 0 {} + if let 5 .. = 0 {} +} diff --git a/rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/true.rs b/rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/true.rs index 50170892130..0eadfb28515 100644 --- a/rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/true.rs +++ b/rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/true.rs @@ -20,3 +20,15 @@ fn main() { _ => bar, } } + +fn half_open() { + match [5..4, 99..105, 43..44] { + [_, 99.., _] => {} + [_, ..105, _] => {} + _ => {} + }; + + if let ..=5 = 0 {} + if let ..5 = 0 {} + if let 5.. = 0 {} +} diff --git a/rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/false.rs b/rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/false.rs index 6319da98572..72b1be4804c 100644 --- a/rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/false.rs +++ b/rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/false.rs @@ -20,3 +20,15 @@ fn main() { _ => bar, } } + +fn half_open() { + match [5..4, 99..105, 43..44] { + [_, 99.., _] => {} + [_, ..105, _] => {} + _ => {} + }; + + if let ..=5 = 0 {} + if let ..5 = 0 {} + if let 5.. = 0 {} +} diff --git a/rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/true.rs b/rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/true.rs index 7bfcc23c8ea..c56fdbb02b6 100644 --- a/rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/true.rs +++ b/rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/true.rs @@ -20,3 +20,15 @@ fn main() { _ => bar, } } + +fn half_open() { + match [5 .. 4, 99 .. 105, 43 .. 44] { + [_, 99 .., _] => {} + [_, .. 105, _] => {} + _ => {} + }; + + if let ..= 5 = 0 {} + if let .. 5 = 0 {} + if let 5 .. = 0 {} +}