diff --git a/src/chains.rs b/src/chains.rs index acd0c2961b1..50a129b695f 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -264,7 +264,7 @@ impl ChainItemKind { return ( ChainItemKind::Parent { expr: expr.clone(), - parens: is_method_call_receiver && should_add_parens(expr), + parens: is_method_call_receiver && should_add_parens(expr, context), }, expr.span, ); @@ -1049,12 +1049,12 @@ fn trim_tries(s: &str) -> String { /// 1. .method(); /// ``` /// Which all need parenthesis or a space before `.method()`. -fn should_add_parens(expr: &ast::Expr) -> bool { +fn should_add_parens(expr: &ast::Expr, context: &RewriteContext<'_>) -> bool { match expr.kind { - ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit), + ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit, context), ast::ExprKind::Closure(ref cl) => match cl.body.kind { ast::ExprKind::Range(_, _, ast::RangeLimits::HalfOpen) => true, - ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit), + ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit, context), _ => false, }, _ => false, diff --git a/src/expr.rs b/src/expr.rs index 3dc2e8b930c..09c865495a4 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -54,8 +54,11 @@ pub(crate) enum ExprType { SubExpression, } -pub(crate) fn lit_ends_in_dot(lit: &Lit) -> bool { - matches!(lit, Lit { kind: LitKind::Float, suffix: None, symbol } if symbol.as_str().ends_with('.')) +pub(crate) fn lit_ends_in_dot(lit: &Lit, context: &RewriteContext<'_>) -> bool { + match lit.kind { + LitKind::Float => do_rewrite_float_lit(context, *lit).ends_with('.'), + _ => false, + } } pub(crate) fn format_expr( @@ -297,7 +300,7 @@ pub(crate) fn format_expr( fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool { match lhs.kind { - ast::ExprKind::Lit(token_lit) => lit_ends_in_dot(&token_lit), + ast::ExprKind::Lit(token_lit) => lit_ends_in_dot(&token_lit, context), ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, expr), ast::ExprKind::Binary(_, _, ref rhs_expr) => { needs_space_before_range(context, rhs_expr) @@ -1350,27 +1353,17 @@ fn rewrite_int_lit( .max_width_error(shape.width, span) } -fn rewrite_float_lit( - context: &RewriteContext<'_>, - token_lit: token::Lit, - span: Span, - shape: Shape, -) -> RewriteResult { +fn do_rewrite_float_lit(context: &RewriteContext<'_>, token_lit: token::Lit) -> String { + let symbol = token_lit.symbol.as_str(); + let suffix = token_lit.suffix.as_ref().map(|s| s.as_str()); + if matches!( context.config.float_literal_trailing_zero(), FloatLiteralTrailingZero::Preserve ) { - return wrap_str( - context.snippet(span).to_owned(), - context.config.max_width(), - shape, - ) - .max_width_error(shape.width, span); + return format!("{}{}", symbol, suffix.unwrap_or("")); } - let symbol = token_lit.symbol.as_str(); - let suffix = token_lit.suffix.as_ref().map(|s| s.as_str()); - let FloatSymbolParts { integer_part, fractional_part, @@ -1400,15 +1393,24 @@ fn rewrite_float_lit( } else { "" }; + format!( + "{}{}{}{}{}", + integer_part, + period, + fractional_part, + exponent.unwrap_or(""), + suffix.unwrap_or(""), + ) +} + +fn rewrite_float_lit( + context: &RewriteContext<'_>, + token_lit: token::Lit, + span: Span, + shape: Shape, +) -> RewriteResult { wrap_str( - format!( - "{}{}{}{}{}", - integer_part, - period, - fractional_part, - exponent.unwrap_or(""), - suffix.unwrap_or(""), - ), + do_rewrite_float_lit(context, token_lit), context.config.max_width(), shape, ) diff --git a/tests/source/configs/float_literal_trailing_zero/always.rs b/tests/source/configs/float_literal_trailing_zero/always.rs index aa5f84a9eaa..e7bde34d18c 100644 --- a/tests/source/configs/float_literal_trailing_zero/always.rs +++ b/tests/source/configs/float_literal_trailing_zero/always.rs @@ -22,6 +22,12 @@ fn float_literals() { let s = 1_000_.000_000; } +fn range_bounds() { + if (1.0..2.0).contains(&1.0) {} + if (1.1..2.2).contains(&1.1) {} + if (1.0e1..2.0e1).contains(&1.0e1) {} +} + fn line_wrapping() { let array = [ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., diff --git a/tests/source/configs/float_literal_trailing_zero/if-no-postfix.rs b/tests/source/configs/float_literal_trailing_zero/if-no-postfix.rs index be8af142af8..a1677a9763c 100644 --- a/tests/source/configs/float_literal_trailing_zero/if-no-postfix.rs +++ b/tests/source/configs/float_literal_trailing_zero/if-no-postfix.rs @@ -22,6 +22,12 @@ fn float_literals() { let s = 1_000_.000_000; } +fn range_bounds() { + if (1.0..2.0).contains(&1.0) {} + if (1.1..2.2).contains(&1.1) {} + if (1.0e1..2.0e1).contains(&1.0e1) {} +} + fn line_wrapping() { let array = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11., 12., 13., 14., 15., 16., 17., 18., diff --git a/tests/source/configs/float_literal_trailing_zero/never.rs b/tests/source/configs/float_literal_trailing_zero/never.rs index afd99dd0ac3..bf94414706c 100644 --- a/tests/source/configs/float_literal_trailing_zero/never.rs +++ b/tests/source/configs/float_literal_trailing_zero/never.rs @@ -22,6 +22,12 @@ fn float_literals() { let s = 1_000_.000_000; } +fn range_bounds() { + if (1.0..2.0).contains(&1.0) {} + if (1.1..2.2).contains(&1.1) {} + if (1.0e1..2.0e1).contains(&1.0e1) {} +} + fn line_wrapping() { let array = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, diff --git a/tests/target/configs/float_literal_trailing_zero/always.rs b/tests/target/configs/float_literal_trailing_zero/always.rs index 4205db675a4..3d45977d26d 100644 --- a/tests/target/configs/float_literal_trailing_zero/always.rs +++ b/tests/target/configs/float_literal_trailing_zero/always.rs @@ -22,6 +22,12 @@ fn float_literals() { let s = 1_000_.000_000; } +fn range_bounds() { + if (1.0..2.0).contains(&1.0) {} + if (1.1..2.2).contains(&1.1) {} + if (1.0e1..2.0e1).contains(&1.0e1) {} +} + fn line_wrapping() { let array = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, diff --git a/tests/target/configs/float_literal_trailing_zero/if-no-postfix.rs b/tests/target/configs/float_literal_trailing_zero/if-no-postfix.rs index b534fa88593..5dcb0b0c6e4 100644 --- a/tests/target/configs/float_literal_trailing_zero/if-no-postfix.rs +++ b/tests/target/configs/float_literal_trailing_zero/if-no-postfix.rs @@ -22,6 +22,12 @@ fn float_literals() { let s = 1_000_.000_000; } +fn range_bounds() { + if (1.0..2.0).contains(&1.0) {} + if (1.1..2.2).contains(&1.1) {} + if (1e1..2e1).contains(&1e1) {} +} + fn line_wrapping() { let array = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, diff --git a/tests/target/configs/float_literal_trailing_zero/never.rs b/tests/target/configs/float_literal_trailing_zero/never.rs index 9b211b41d14..3ee016b3418 100644 --- a/tests/target/configs/float_literal_trailing_zero/never.rs +++ b/tests/target/configs/float_literal_trailing_zero/never.rs @@ -22,6 +22,12 @@ fn float_literals() { let s = 1_000_.; } +fn range_bounds() { + if (1. ..2.).contains(&1.) {} + if (1.1..2.2).contains(&1.1) {} + if (1e1..2e1).contains(&1e1) {} +} + fn line_wrapping() { let array = [ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., diff --git a/tests/target/configs/float_literal_trailing_zero/preserve.rs b/tests/target/configs/float_literal_trailing_zero/preserve.rs index 17c3941f342..5c78e1debb8 100644 --- a/tests/target/configs/float_literal_trailing_zero/preserve.rs +++ b/tests/target/configs/float_literal_trailing_zero/preserve.rs @@ -21,3 +21,9 @@ fn float_literals() { let r = 1_000_.; let s = 1_000_.000_000; } + +fn range_bounds() { + if (1.0..2.0).contains(&1.0) {} + if (1.1..2.2).contains(&1.1) {} + if (1.0e1..2.0e1).contains(&1.0e1) {} +}