Skip to content

Commit 7ffd59f

Browse files
committed
feat: upper- or lowercase hexadecimal literals
1 parent 2837ca5 commit 7ffd59f

9 files changed

+70
-1
lines changed

Diff for: Configurations.md

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

10481048
See also: [`tab_spaces`](#tab_spaces).
10491049

1050+
## `hex_literal_case`
1051+
1052+
Change the case of the letters in hexadecimal literal values
1053+
1054+
- **Default value**: `Ignore`
1055+
- **Possible values**: `ToUpper`, `ToLower`
1056+
- **Stable**: No
10501057

10511058
## `hide_parse_errors`
10521059

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::Ignore, false,
73+
"Format hexadecimal integer literals";
7274

7375
// Single line expressions and items
7476
empty_item_single_line: bool, true, false,
@@ -569,6 +571,7 @@ license_template_path = ""
569571
format_strings = false
570572
format_macro_matchers = false
571573
format_macro_bodies = true
574+
hex_literal_case = "Ignore"
572575
empty_item_single_line = true
573576
struct_lit_single_line = true
574577
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+
Ignore,
135+
/// Uppercase the literal
136+
ToUpper,
137+
/// Lowercase the literal
138+
ToLower,
139+
}
140+
130141
#[config_type]
131142
pub enum ReportTactic {
132143
Always,

Diff for: src/expr.rs

+24-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,
@@ -1176,6 +1176,7 @@ pub(crate) fn rewrite_literal(
11761176
) -> Option<String> {
11771177
match l.kind {
11781178
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
1179+
ast::LitKind::Int(..) => rewrite_int_lit(context, l),
11791180
_ => wrap_str(
11801181
context.snippet(l.span).to_owned(),
11811182
context.config.max_width(),
@@ -1210,6 +1211,28 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12101211
)
12111212
}
12121213

1214+
fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit) -> Option<String> {
1215+
let span = lit.span;
1216+
let symbol = lit.token.symbol.as_str();
1217+
1218+
if symbol.starts_with("0x") {
1219+
let hex_lit = match context.config.hex_literal_case() {
1220+
HexLiteralCase::Ignore => None,
1221+
HexLiteralCase::ToUpper => Some(symbol[2..].to_ascii_uppercase()),
1222+
HexLiteralCase::ToLower => Some(symbol[2..].to_ascii_lowercase()),
1223+
};
1224+
if let Some(hex_lit) = hex_lit {
1225+
return Some(format!(
1226+
"0x{}{}",
1227+
hex_lit,
1228+
lit.token.suffix.map_or(String::new(), |s| s.to_string())
1229+
));
1230+
}
1231+
}
1232+
1233+
Some(context.snippet(span).to_owned())
1234+
}
1235+
12131236
fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {
12141237
if context.inside_macro() {
12151238
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: ToLower
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: ToUpper
2+
fn main() {
3+
let h1 = 0xCAFE_5EA7;
4+
let h2 = 0xCAFE_F00Du32;
5+
}

Diff for: tests/target/hex_literal_ignore.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-hex_literal_case: Ignore
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: ToLower
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: ToUpper
2+
fn main() {
3+
let h1 = 0xCAFE_5EA7;
4+
let h2 = 0xCAFE_F00Du32;
5+
}

0 commit comments

Comments
 (0)