diff options
| author | Caleb Cartwright <calebcartwright@users.noreply.github.com> | 2020-03-31 01:28:01 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-31 15:28:01 +0900 |
| commit | 00e199c974bd304695e6436fcb6b60bca5c8d6f4 (patch) | |
| tree | fa40b9a2cef7aeec52fb1947e0cee98f39580ec6 /src | |
| parent | 5ca3d023ff6c970e7dcd6cee3c83672dff14dc15 (diff) | |
backport new syntax to rustfmt 1.x (#4105)
* feat: support raw reference operator * feat: support const opt-out syntax * feat: support half open range syntax
Diffstat (limited to 'src')
| -rw-r--r-- | src/expr.rs | 10 | ||||
| -rw-r--r-- | src/patterns.rs | 58 | ||||
| -rw-r--r-- | src/types.rs | 7 |
3 files changed, 49 insertions, 26 deletions
diff --git a/src/expr.rs b/src/expr.rs index 8545b0f6f29..ed10a54c2a3 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1988,14 +1988,16 @@ pub(crate) fn prefer_next_line( fn rewrite_expr_addrof( context: &RewriteContext<'_>, - _borrow_kind: ast::BorrowKind, + borrow_kind: ast::BorrowKind, mutability: ast::Mutability, expr: &ast::Expr, shape: Shape, ) -> Option<String> { - let operator_str = match mutability { - ast::Mutability::Not => "&", - ast::Mutability::Mut => "&mut ", + let operator_str = match (mutability, borrow_kind) { + (ast::Mutability::Not, ast::BorrowKind::Ref) => "&", + (ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ", + (ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ", + (ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ", }; rewrite_unary_prefix(context, operator_str, expr, shape) } diff --git a/src/patterns.rs b/src/patterns.rs index 78292620232..5847bd08d80 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -55,6 +55,17 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool { } } +struct RangeOperand<'a>(&'a Option<ptr::P<ast::Expr>>); + +impl<'a> Rewrite for RangeOperand<'a> { + fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { + 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<String> { 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/src/types.rs b/src/types.rs index 4f92ff6246f..96e331779c4 100644 --- a/src/types.rs +++ b/src/types.rs @@ -527,7 +527,12 @@ impl Rewrite for ast::GenericBound { ast::TraitBoundModifier::Maybe => poly_trait_ref .rewrite(context, shape.offset_left(1)?) .map(|s| format!("?{}", s)), - _ => unimplemented!(), + ast::TraitBoundModifier::MaybeConst => poly_trait_ref + .rewrite(context, shape.offset_left(7)?) + .map(|s| format!("?const {}", s)), + ast::TraitBoundModifier::MaybeConstMaybe => poly_trait_ref + .rewrite(context, shape.offset_left(8)?) + .map(|s| format!("?const ?{}", s)), }; rewrite.map(|s| if has_paren { format!("({})", s) } else { s }) } |
