Skip to content

Commit

Permalink
Fix floating-point literals in ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
amatveiakin committed Jan 5, 2025
1 parent 004bc6b commit e90e763
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
Expand Down
54 changes: 28 additions & 26 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand Down
6 changes: 6 additions & 0 deletions tests/source/configs/float_literal_trailing_zero/always.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.,
Expand Down
6 changes: 6 additions & 0 deletions tests/source/configs/float_literal_trailing_zero/never.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/target/configs/float_literal_trailing_zero/always.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/target/configs/float_literal_trailing_zero/never.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.,
Expand Down
6 changes: 6 additions & 0 deletions tests/target/configs/float_literal_trailing_zero/preserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}

0 comments on commit e90e763

Please sign in to comment.