Skip to content

Commit d304dec

Browse files
ArjenLcalebcartwright
authored andcommitted
feat: upper- or lowercase hexadecimal literals
1 parent 1b87a8b commit d304dec

9 files changed

+78
-1
lines changed

Diff for: Configurations.md

+7
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,13 @@ fn lorem() -> usize {
10561056

10571057
See also: [`tab_spaces`](#tab_spaces).
10581058

1059+
## `hex_literal_case`
1060+
1061+
Control the case of the letters in hexadecimal literal values
1062+
1063+
- **Default value**: `Preserve`
1064+
- **Possible values**: `Upper`, `Lower`
1065+
- **Stable**: No
10591066

10601067
## `hide_parse_errors`
10611068

Diff for: src/config/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ create_config! {
6969
format_macro_matchers: bool, false, false,
7070
"Format the metavariable matching patterns in macros";
7171
format_macro_bodies: bool, true, false, "Format the bodies of macros";
72+
hex_literal_case: HexLiteralCase, HexLiteralCase::Preserve, false,
73+
"Format hexadecimal integer literals";
7274

7375
// Single line expressions and items
7476
empty_item_single_line: bool, true, false,
@@ -570,6 +572,7 @@ license_template_path = ""
570572
format_strings = false
571573
format_macro_matchers = false
572574
format_macro_bodies = true
575+
hex_literal_case = "Preserve"
573576
empty_item_single_line = true
574577
struct_lit_single_line = true
575578
fn_single_line = false

Diff for: src/config/options.rs

+11
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ pub enum ImportGranularity {
127127
Item,
128128
}
129129

130+
/// Controls how rustfmt should handle case in hexadecimal literals.
131+
#[config_type]
132+
pub enum HexLiteralCase {
133+
/// Leave the literal as-is
134+
Preserve,
135+
/// Ensure all literals use uppercase lettering
136+
Upper,
137+
/// Ensure all literals use lowercase lettering
138+
Lower,
139+
}
140+
130141
#[config_type]
131142
pub enum ReportTactic {
132143
Always,

Diff for: src/expr.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::comment::{
1313
rewrite_missing_comment, CharClasses, FindUncommented,
1414
};
1515
use crate::config::lists::*;
16-
use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
16+
use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, Version};
1717
use crate::lists::{
1818
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
1919
struct_lit_tactic, write_list, ListFormatting, Separator,
@@ -1166,6 +1166,7 @@ pub(crate) fn rewrite_literal(
11661166
) -> Option<String> {
11671167
match l.kind {
11681168
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
1169+
ast::LitKind::Int(..) => rewrite_int_lit(context, l, shape),
11691170
_ => wrap_str(
11701171
context.snippet(l.span).to_owned(),
11711172
context.config.max_width(),
@@ -1200,6 +1201,36 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12001201
)
12011202
}
12021203

1204+
fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit, shape: Shape) -> Option<String> {
1205+
let span = lit.span;
1206+
let symbol = lit.token.symbol.as_str();
1207+
1208+
if symbol.starts_with("0x") {
1209+
let hex_lit = match context.config.hex_literal_case() {
1210+
HexLiteralCase::Preserve => None,
1211+
HexLiteralCase::Upper => Some(symbol[2..].to_ascii_uppercase()),
1212+
HexLiteralCase::Lower => Some(symbol[2..].to_ascii_lowercase()),
1213+
};
1214+
if let Some(hex_lit) = hex_lit {
1215+
return wrap_str(
1216+
format!(
1217+
"0x{}{}",
1218+
hex_lit,
1219+
lit.token.suffix.map_or(String::new(), |s| s.to_string())
1220+
),
1221+
context.config.max_width(),
1222+
shape,
1223+
);
1224+
}
1225+
}
1226+
1227+
wrap_str(
1228+
context.snippet(span).to_owned(),
1229+
context.config.max_width(),
1230+
shape,
1231+
)
1232+
}
1233+
12031234
fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {
12041235
if context.inside_macro() {
12051236
if span_ends_with_comma(context, span) {

Diff for: tests/source/hex_literal_lower.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-hex_literal_case: Lower
2+
fn main() {
3+
let h1 = 0xCAFE_5EA7;
4+
let h2 = 0xCAFE_F00Du32;
5+
}

Diff for: tests/source/hex_literal_upper.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-hex_literal_case: Upper
2+
fn main() {
3+
let h1 = 0xCaFE_5ea7;
4+
let h2 = 0xCAFE_F00Du32;
5+
}

Diff for: tests/target/hex_literal_lower.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-hex_literal_case: Lower
2+
fn main() {
3+
let h1 = 0xcafe_5ea7;
4+
let h2 = 0xcafe_f00du32;
5+
}

Diff for: tests/target/hex_literal_preserve.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-hex_literal_case: Preserve
2+
fn main() {
3+
let h1 = 0xcAfE_5Ea7;
4+
let h2 = 0xCaFe_F00du32;
5+
}

Diff for: tests/target/hex_literal_upper.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-hex_literal_case: Upper
2+
fn main() {
3+
let h1 = 0xCAFE_5EA7;
4+
let h2 = 0xCAFE_F00Du32;
5+
}

0 commit comments

Comments
 (0)