Skip to content

Commit 497e17e

Browse files
committed
Fix floating-point literals in ranges
1 parent 004bc6b commit 497e17e

File tree

9 files changed

+81
-30
lines changed

9 files changed

+81
-30
lines changed

src/chains.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl ChainItemKind {
264264
return (
265265
ChainItemKind::Parent {
266266
expr: expr.clone(),
267-
parens: is_method_call_receiver && should_add_parens(expr),
267+
parens: is_method_call_receiver && should_add_parens(expr, context),
268268
},
269269
expr.span,
270270
);
@@ -1049,12 +1049,12 @@ fn trim_tries(s: &str) -> String {
10491049
/// 1. .method();
10501050
/// ```
10511051
/// Which all need parenthesis or a space before `.method()`.
1052-
fn should_add_parens(expr: &ast::Expr) -> bool {
1052+
fn should_add_parens(expr: &ast::Expr, context: &RewriteContext<'_>) -> bool {
10531053
match expr.kind {
1054-
ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit),
1054+
ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit, context),
10551055
ast::ExprKind::Closure(ref cl) => match cl.body.kind {
10561056
ast::ExprKind::Range(_, _, ast::RangeLimits::HalfOpen) => true,
1057-
ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit),
1057+
ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit, context),
10581058
_ => false,
10591059
},
10601060
_ => false,

src/expr.rs

+28-26
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ pub(crate) enum ExprType {
5454
SubExpression,
5555
}
5656

57-
pub(crate) fn lit_ends_in_dot(lit: &Lit) -> bool {
58-
matches!(lit, Lit { kind: LitKind::Float, suffix: None, symbol } if symbol.as_str().ends_with('.'))
57+
pub(crate) fn lit_ends_in_dot(lit: &Lit, context: &RewriteContext<'_>) -> bool {
58+
match lit.kind {
59+
LitKind::Float => do_rewrite_float_lit(context, *lit).ends_with('.'),
60+
_ => false,
61+
}
5962
}
6063

6164
pub(crate) fn format_expr(
@@ -297,7 +300,7 @@ pub(crate) fn format_expr(
297300

298301
fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool {
299302
match lhs.kind {
300-
ast::ExprKind::Lit(token_lit) => lit_ends_in_dot(&token_lit),
303+
ast::ExprKind::Lit(token_lit) => lit_ends_in_dot(&token_lit, context),
301304
ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, expr),
302305
ast::ExprKind::Binary(_, _, ref rhs_expr) => {
303306
needs_space_before_range(context, rhs_expr)
@@ -1350,27 +1353,17 @@ fn rewrite_int_lit(
13501353
.max_width_error(shape.width, span)
13511354
}
13521355

1353-
fn rewrite_float_lit(
1354-
context: &RewriteContext<'_>,
1355-
token_lit: token::Lit,
1356-
span: Span,
1357-
shape: Shape,
1358-
) -> RewriteResult {
1356+
fn do_rewrite_float_lit(context: &RewriteContext<'_>, token_lit: token::Lit) -> String {
1357+
let symbol = token_lit.symbol.as_str();
1358+
let suffix = token_lit.suffix.as_ref().map(|s| s.as_str());
1359+
13591360
if matches!(
13601361
context.config.float_literal_trailing_zero(),
13611362
FloatLiteralTrailingZero::Preserve
13621363
) {
1363-
return wrap_str(
1364-
context.snippet(span).to_owned(),
1365-
context.config.max_width(),
1366-
shape,
1367-
)
1368-
.max_width_error(shape.width, span);
1364+
return format!("{}{}", symbol, suffix.unwrap_or(""));
13691365
}
13701366

1371-
let symbol = token_lit.symbol.as_str();
1372-
let suffix = token_lit.suffix.as_ref().map(|s| s.as_str());
1373-
13741367
let FloatSymbolParts {
13751368
integer_part,
13761369
fractional_part,
@@ -1400,15 +1393,24 @@ fn rewrite_float_lit(
14001393
} else {
14011394
""
14021395
};
1396+
format!(
1397+
"{}{}{}{}{}",
1398+
integer_part,
1399+
period,
1400+
fractional_part,
1401+
exponent.unwrap_or(""),
1402+
suffix.unwrap_or(""),
1403+
)
1404+
}
1405+
1406+
fn rewrite_float_lit(
1407+
context: &RewriteContext<'_>,
1408+
token_lit: token::Lit,
1409+
span: Span,
1410+
shape: Shape,
1411+
) -> RewriteResult {
14031412
wrap_str(
1404-
format!(
1405-
"{}{}{}{}{}",
1406-
integer_part,
1407-
period,
1408-
fractional_part,
1409-
exponent.unwrap_or(""),
1410-
suffix.unwrap_or(""),
1411-
),
1413+
do_rewrite_float_lit(context, token_lit),
14121414
context.config.max_width(),
14131415
shape,
14141416
)

tests/source/configs/float_literal_trailing_zero/always.rs

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ fn float_literals() {
2222
let s = 1_000_.000_000;
2323
}
2424

25+
fn range_bounds() {
26+
if (1.0..2.0).contains(&1.0) {}
27+
if (1.1..2.2).contains(&1.1) {}
28+
if (1.0e1..2.0e1).contains(&1.0e1) {}
29+
let _binop_range = 3.0 / 2.0..4.0;
30+
}
31+
2532
fn line_wrapping() {
2633
let array = [
2734
1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18.,

tests/source/configs/float_literal_trailing_zero/if-no-postfix.rs

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ fn float_literals() {
2222
let s = 1_000_.000_000;
2323
}
2424

25+
fn range_bounds() {
26+
if (1.0..2.0).contains(&1.0) {}
27+
if (1.1..2.2).contains(&1.1) {}
28+
if (1.0e1..2.0e1).contains(&1.0e1) {}
29+
let _binop_range = 3.0 / 2.0..4.0;
30+
}
31+
2532
fn line_wrapping() {
2633
let array = [
2734
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.,

tests/source/configs/float_literal_trailing_zero/never.rs

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ fn float_literals() {
2222
let s = 1_000_.000_000;
2323
}
2424

25+
fn range_bounds() {
26+
if (1.0..2.0).contains(&1.0) {}
27+
if (1.1..2.2).contains(&1.1) {}
28+
if (1.0e1..2.0e1).contains(&1.0e1) {}
29+
let _binop_range = 3.0 / 2.0..4.0;
30+
}
31+
2532
fn line_wrapping() {
2633
let array = [
2734
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,

tests/target/configs/float_literal_trailing_zero/always.rs

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ fn float_literals() {
2222
let s = 1_000_.000_000;
2323
}
2424

25+
fn range_bounds() {
26+
if (1.0..2.0).contains(&1.0) {}
27+
if (1.1..2.2).contains(&1.1) {}
28+
if (1.0e1..2.0e1).contains(&1.0e1) {}
29+
let _binop_range = 3.0 / 2.0..4.0;
30+
}
31+
2532
fn line_wrapping() {
2633
let array = [
2734
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,

tests/target/configs/float_literal_trailing_zero/if-no-postfix.rs

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ fn float_literals() {
2222
let s = 1_000_.000_000;
2323
}
2424

25+
fn range_bounds() {
26+
if (1.0..2.0).contains(&1.0) {}
27+
if (1.1..2.2).contains(&1.1) {}
28+
if (1e1..2e1).contains(&1e1) {}
29+
let _binop_range = 3.0 / 2.0..4.0;
30+
}
31+
2532
fn line_wrapping() {
2633
let array = [
2734
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,

tests/target/configs/float_literal_trailing_zero/never.rs

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ fn float_literals() {
2222
let s = 1_000_.;
2323
}
2424

25+
fn range_bounds() {
26+
if (1. ..2.).contains(&1.) {}
27+
if (1.1..2.2).contains(&1.1) {}
28+
if (1e1..2e1).contains(&1e1) {}
29+
let _binop_range = 3. / 2. ..4.;
30+
}
31+
2532
fn line_wrapping() {
2633
let array = [
2734
1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18.,

tests/target/configs/float_literal_trailing_zero/preserve.rs

+7
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ fn float_literals() {
2121
let r = 1_000_.;
2222
let s = 1_000_.000_000;
2323
}
24+
25+
fn range_bounds() {
26+
if (1.0..2.0).contains(&1.0) {}
27+
if (1.1..2.2).contains(&1.1) {}
28+
if (1.0e1..2.0e1).contains(&1.0e1) {}
29+
let _binop_range = 3.0 / 2.0..4.0;
30+
}

0 commit comments

Comments
 (0)