Skip to content

Commit 2ad782c

Browse files
authored
Add option to control trailing zero in floating-point literals (#5834)
Add `float_literal_trailing_zero` option
1 parent 5619b64 commit 2ad782c

File tree

12 files changed

+620
-9
lines changed

12 files changed

+620
-9
lines changed

Configurations.md

+50
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,56 @@ Control the case of the letters in hexadecimal literal values
12561256
- **Possible values**: `Preserve`, `Upper`, `Lower`
12571257
- **Stable**: No (tracking issue: [#5081](https://github.com/rust-lang/rustfmt/issues/5081))
12581258

1259+
## `float_literal_trailing_zero`
1260+
1261+
Control the presence of trailing zero in floating-point literal values
1262+
1263+
- **Default value**: `Preserve`
1264+
- **Possible values**: `Preserve`, `Always`, `IfNoPostfix`, `Never`
1265+
- **Stable**: No (tracking issue: [#6471](https://github.com/rust-lang/rustfmt/issues/6471))
1266+
1267+
#### `Preserve` (default):
1268+
1269+
Leave the literal as-is.
1270+
1271+
```rust
1272+
fn main() {
1273+
let values = [1.0, 2., 3.0e10, 4f32];
1274+
}
1275+
```
1276+
1277+
#### `Always`:
1278+
1279+
Add a trailing zero to the literal:
1280+
1281+
```rust
1282+
fn main() {
1283+
let values = [1.0, 2.0, 3.0e10, 4.0f32];
1284+
}
1285+
```
1286+
1287+
#### `IfNoPostfix`:
1288+
1289+
Add a trailing zero by default. If the literal contains an exponent or a suffix, the zero
1290+
and the preceding period are removed:
1291+
1292+
```rust
1293+
fn main() {
1294+
let values = [1.0, 2.0, 3e10, 4f32];
1295+
}
1296+
```
1297+
1298+
#### `Never`:
1299+
1300+
Remove the trailing zero. If the literal contains an exponent or a suffix, the preceding
1301+
period is also removed:
1302+
1303+
```rust
1304+
fn main() {
1305+
let values = [1., 2., 3e10, 4f32];
1306+
}
1307+
```
1308+
12591309
## `hide_parse_errors`
12601310

12611311
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`.

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/config/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ create_config! {
7979
skip_macro_invocations: SkipMacroInvocations, false,
8080
"Skip formatting the bodies of macros invoked with the following names.";
8181
hex_literal_case: HexLiteralCaseConfig, false, "Format hexadecimal integer literals";
82+
float_literal_trailing_zero: FloatLiteralTrailingZeroConfig, false,
83+
"Add or remove trailing zero in floating-point literals";
8284

8385
// Single line expressions and items
8486
empty_item_single_line: EmptyItemSingleLine, false,
@@ -739,6 +741,7 @@ format_macro_matchers = false
739741
format_macro_bodies = true
740742
skip_macro_invocations = []
741743
hex_literal_case = "Preserve"
744+
float_literal_trailing_zero = "Preserve"
742745
empty_item_single_line = true
743746
struct_lit_single_line = true
744747
fn_single_line = false
@@ -829,6 +832,7 @@ format_macro_matchers = false
829832
format_macro_bodies = true
830833
skip_macro_invocations = []
831834
hex_literal_case = "Preserve"
835+
float_literal_trailing_zero = "Preserve"
832836
empty_item_single_line = true
833837
struct_lit_single_line = true
834838
fn_single_line = false

src/config/options.rs

+17
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ pub enum HexLiteralCase {
146146
Lower,
147147
}
148148

149+
/// How to treat trailing zeros in floating-point literals.
150+
#[config_type]
151+
pub enum FloatLiteralTrailingZero {
152+
/// Leave the literal as-is.
153+
Preserve,
154+
/// Add a trailing zero to the literal.
155+
Always,
156+
/// Add a trailing zero by default. If the literal contains an exponent or a suffix, the zero
157+
/// and the preceding period are removed.
158+
IfNoPostfix,
159+
/// Remove the trailing zero. If the literal contains an exponent or a suffix, the preceding
160+
/// period is also removed.
161+
Never,
162+
}
163+
149164
#[config_type]
150165
pub enum ReportTactic {
151166
Always,
@@ -613,6 +628,8 @@ config_option_with_style_edition_default!(
613628
FormatMacroBodies, bool, _ => true;
614629
SkipMacroInvocations, MacroSelectors, _ => MacroSelectors::default();
615630
HexLiteralCaseConfig, HexLiteralCase, _ => HexLiteralCase::Preserve;
631+
FloatLiteralTrailingZeroConfig, FloatLiteralTrailingZero, _ =>
632+
FloatLiteralTrailingZero::Preserve;
616633

617634
// Single line expressions and items
618635
EmptyItemSingleLine, bool, _ => true;

0 commit comments

Comments
 (0)