Skip to content

Commit

Permalink
feat: upper- or lowercase hexadecimal literals
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjenL committed Sep 8, 2021
1 parent 2837ca5 commit d176b76
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,13 @@ fn lorem() -> usize {

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

## `hex_literal_case`

Control the case of the letters in hexadecimal literal values

- **Default value**: `Preserve`
- **Possible values**: `Upper`, `Lower`
- **Stable**: No

## `hide_parse_errors`

Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ create_config! {
format_macro_matchers: bool, false, false,
"Format the metavariable matching patterns in macros";
format_macro_bodies: bool, true, false, "Format the bodies of macros";
hex_literal_case: HexLiteralCase, HexLiteralCase::Preserve, false,
"Format hexadecimal integer literals";

// Single line expressions and items
empty_item_single_line: bool, true, false,
Expand Down Expand Up @@ -569,6 +571,7 @@ license_template_path = ""
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
hex_literal_case = "Preserve"
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
Expand Down
11 changes: 11 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ pub enum ImportGranularity {
Item,
}

/// Controls how rustfmt should handle case in hexadecimal literals.
#[config_type]
pub enum HexLiteralCase {
/// Leave the literal as-is
Preserve,
/// Ensure all literals use uppercase lettering
Upper,
/// Ensure all literals use lowercase lettering
Lower,
}

#[config_type]
pub enum ReportTactic {
Always,
Expand Down
33 changes: 32 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::comment::{
rewrite_missing_comment, CharClasses, FindUncommented,
};
use crate::config::lists::*;
use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, Version};
use crate::lists::{
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
struct_lit_tactic, write_list, ListFormatting, Separator,
Expand Down Expand Up @@ -1176,6 +1176,7 @@ pub(crate) fn rewrite_literal(
) -> Option<String> {
match l.kind {
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
ast::LitKind::Int(..) => rewrite_int_lit(context, l, shape),
_ => wrap_str(
context.snippet(l.span).to_owned(),
context.config.max_width(),
Expand Down Expand Up @@ -1210,6 +1211,36 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
)
}

fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit, shape: Shape) -> Option<String> {
let span = lit.span;
let symbol = lit.token.symbol.as_str();

if symbol.starts_with("0x") {
let hex_lit = match context.config.hex_literal_case() {
HexLiteralCase::Preserve => None,
HexLiteralCase::Upper => Some(symbol[2..].to_ascii_uppercase()),
HexLiteralCase::Lower => Some(symbol[2..].to_ascii_lowercase()),
};
if let Some(hex_lit) = hex_lit {
return wrap_str(
format!(
"0x{}{}",
hex_lit,
lit.token.suffix.map_or(String::new(), |s| s.to_string())
),
context.config.max_width(),
shape,
);
}
}

wrap_str(
context.snippet(span).to_owned(),
context.config.max_width(),
shape,
)
}

fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {
if context.inside_macro() {
if span_ends_with_comma(context, span) {
Expand Down
5 changes: 5 additions & 0 deletions tests/source/hex_literal_lower.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-hex_literal_case: Lower
fn main() {
let h1 = 0xCAFE_5EA7;
let h2 = 0xCAFE_F00Du32;
}
5 changes: 5 additions & 0 deletions tests/source/hex_literal_upper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-hex_literal_case: Upper
fn main() {
let h1 = 0xCaFE_5ea7;
let h2 = 0xCAFE_F00Du32;
}
5 changes: 5 additions & 0 deletions tests/target/hex_literal_lower.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-hex_literal_case: Lower
fn main() {
let h1 = 0xcafe_5ea7;
let h2 = 0xcafe_f00du32;
}
5 changes: 5 additions & 0 deletions tests/target/hex_literal_preserve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-hex_literal_case: Preserve
fn main() {
let h1 = 0xcAfE_5Ea7;
let h2 = 0xCaFe_F00du32;
}
5 changes: 5 additions & 0 deletions tests/target/hex_literal_upper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-hex_literal_case: Upper
fn main() {
let h1 = 0xCAFE_5EA7;
let h2 = 0xCAFE_F00Du32;
}

0 comments on commit d176b76

Please sign in to comment.