Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to control trailing zero in floating-point literals #5834

Merged
merged 8 commits into from
Feb 17, 2025
50 changes: 50 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,56 @@ Control the case of the letters in hexadecimal literal values
- **Possible values**: `Preserve`, `Upper`, `Lower`
- **Stable**: No (tracking issue: [#5081](https://github.com/rust-lang/rustfmt/issues/5081))

## `float_literal_trailing_zero`

Control the presence of trailing zero in floating-point literal values

- **Default value**: `Preserve`
- **Possible values**: `Preserve`, `Always`, `IfNoPostfix`, `Never`
- **Stable**: No (tracking issue: [#6471](https://github.com/rust-lang/rustfmt/issues/6471))

#### `Preserve` (default):

Leave the literal as-is.

```rust
fn main() {
let values = [1.0, 2., 3.0e10, 4f32];
}
```

#### `Always`:

Add a trailing zero to the literal:

```rust
fn main() {
let values = [1.0, 2.0, 3.0e10, 4.0f32];
}
```

#### `IfNoPostfix`:

Add a trailing zero by default. If the literal contains an exponent or a suffix, the zero
and the preceding period are removed:

```rust
fn main() {
let values = [1.0, 2.0, 3e10, 4f32];
}
```

#### `Never`:

Remove the trailing zero. If the literal contains an exponent or a suffix, the preceding
period is also removed:

```rust
fn main() {
let values = [1., 2., 3e10, 4f32];
}
```

## `hide_parse_errors`

This option is deprecated and has been renamed to `show_parse_errors` to avoid confusion around the double negative default of `hide_parse_errors=false`.
Expand Down
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
4 changes: 4 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ create_config! {
skip_macro_invocations: SkipMacroInvocations, false,
"Skip formatting the bodies of macros invoked with the following names.";
hex_literal_case: HexLiteralCaseConfig, false, "Format hexadecimal integer literals";
float_literal_trailing_zero: FloatLiteralTrailingZeroConfig, false,
"Add or remove trailing zero in floating-point literals";

// Single line expressions and items
empty_item_single_line: EmptyItemSingleLine, false,
Expand Down Expand Up @@ -739,6 +741,7 @@ format_macro_matchers = false
format_macro_bodies = true
skip_macro_invocations = []
hex_literal_case = "Preserve"
float_literal_trailing_zero = "Preserve"
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
Expand Down Expand Up @@ -829,6 +832,7 @@ format_macro_matchers = false
format_macro_bodies = true
skip_macro_invocations = []
hex_literal_case = "Preserve"
float_literal_trailing_zero = "Preserve"
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
Expand Down
17 changes: 17 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ pub enum HexLiteralCase {
Lower,
}

/// How to treat trailing zeros in floating-point literals.
#[config_type]
pub enum FloatLiteralTrailingZero {
/// Leave the literal as-is.
Preserve,
/// Add a trailing zero to the literal.
Always,
/// Add a trailing zero by default. If the literal contains an exponent or a suffix, the zero
/// and the preceding period are removed.
IfNoPostfix,
/// Remove the trailing zero. If the literal contains an exponent or a suffix, the preceding
/// period is also removed.
Never,
}

#[config_type]
pub enum ReportTactic {
Always,
Expand Down Expand Up @@ -613,6 +628,8 @@ config_option_with_style_edition_default!(
FormatMacroBodies, bool, _ => true;
SkipMacroInvocations, MacroSelectors, _ => MacroSelectors::default();
HexLiteralCaseConfig, HexLiteralCase, _ => HexLiteralCase::Preserve;
FloatLiteralTrailingZeroConfig, FloatLiteralTrailingZero, _ =>
FloatLiteralTrailingZero::Preserve;

// Single line expressions and items
EmptyItemSingleLine, bool, _ => true;
Expand Down
Loading
Loading