Skip to content

Commit

Permalink
feat: implement 2024 Style Edition for expr overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Aug 2, 2024
1 parent 676b64b commit 4579a63
Show file tree
Hide file tree
Showing 9 changed files with 602 additions and 2 deletions.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
error_on_line_overflow = true
error_on_unformatted = true
style_edition = "2024"
overflow_delimited_expr = false
32 changes: 32 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,4 +914,36 @@ mod test {
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
}

#[nightly_only_test]
#[test]
fn correct_defaults_for_style_edition_loaded() {
let mut options = GetOptsOptions::default();
options.style_edition = Some(StyleEdition::Edition2024);
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
}

#[nightly_only_test]
#[test]
fn style_edition_defaults_overridden_from_config() {
let options = GetOptsOptions::default();
let config_file = Some(Path::new("tests/config/style-edition/overrides"));
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), false);
}

#[nightly_only_test]
#[test]
fn style_edition_defaults_overridden_from_cli() {
let mut options = GetOptsOptions::default();
let config_file = Some(Path::new("tests/config/style-edition/just-style-edition"));
options.inline_config =
HashMap::from([("overflow_delimited_expr".to_owned(), "false".to_owned())]);
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), false);
}
}
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
short_array_element_width_threshold = 10
overflow_delimited_expr = false
overflow_delimited_expr = true
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
Expand Down
2 changes: 1 addition & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ config_option_with_style_edition_default!(
RemoveNestedParens, bool, _ => true;
CombineControlExpr, bool, _ => true;
ShortArrayElementWidthThreshold, usize, _ => 10;
OverflowDelimitedExpr, bool, _ => false;
OverflowDelimitedExpr, bool, Edition2024 => true, _ => false;
StructFieldAlignThreshold, usize, _ => 0;
EnumDiscrimAlignThreshold, usize, _ => 0;
MatchArmBlocks, bool, _ => true;
Expand Down
2 changes: 2 additions & 0 deletions tests/config/style-edition/overrides/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
style_edition = "2024"
overflow_delimited_expr = false
155 changes: 155 additions & 0 deletions tests/source/configs/style_edition/overflow_delim_expr_2015.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// rustfmt-style_edition: 2015

fn combine_blocklike() {
do_thing(
|param| {
action();
foo(param)
},
);

do_thing(
x,
|param| {
action();
foo(param)
},
);

do_thing(
x,

// I'll be discussing the `action` with your para(m)legal counsel
|param| {
action();
foo(param)
},
);

do_thing(
Bar {
x: value,
y: value2,
},
);

do_thing(
x,
Bar {
x: value,
y: value2,
},
);

do_thing(
x,

// Let me tell you about that one time at the `Bar`
Bar {
x: value,
y: value2,
},
);

do_thing(
&[
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,
&[
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,

// Just admit it; my list is longer than can be folded on to one line
&[
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
vec![
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,
vec![
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,

// Just admit it; my list is longer than can be folded on to one line
vec![
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,
(
1,
2,
3,
|param| {
action();
foo(param)
},
),
);
}

fn combine_struct_sample() {
let identity = verify(
&ctx,
VerifyLogin {
type_: LoginType::Username,
username: args.username.clone(),
password: Some(args.password.clone()),
domain: None,
},
)?;
}

fn combine_macro_sample() {
rocket::ignite()
.mount(
"/",
routes![
http::auth::login,
http::auth::logout,
http::cors::options,
http::action::dance,
http::action::sleep,
],
)
.launch();
}
155 changes: 155 additions & 0 deletions tests/source/configs/style_edition/overflow_delim_expr_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// rustfmt-style_edition: 2024

fn combine_blocklike() {
do_thing(
|param| {
action();
foo(param)
},
);

do_thing(
x,
|param| {
action();
foo(param)
},
);

do_thing(
x,

// I'll be discussing the `action` with your para(m)legal counsel
|param| {
action();
foo(param)
},
);

do_thing(
Bar {
x: value,
y: value2,
},
);

do_thing(
x,
Bar {
x: value,
y: value2,
},
);

do_thing(
x,

// Let me tell you about that one time at the `Bar`
Bar {
x: value,
y: value2,
},
);

do_thing(
&[
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,
&[
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,

// Just admit it; my list is longer than can be folded on to one line
&[
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
vec![
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,
vec![
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,

// Just admit it; my list is longer than can be folded on to one line
vec![
value_with_longer_name,
value2_with_longer_name,
value3_with_longer_name,
value4_with_longer_name,
],
);

do_thing(
x,
(
1,
2,
3,
|param| {
action();
foo(param)
},
),
);
}

fn combine_struct_sample() {
let identity = verify(
&ctx,
VerifyLogin {
type_: LoginType::Username,
username: args.username.clone(),
password: Some(args.password.clone()),
domain: None,
},
)?;
}

fn combine_macro_sample() {
rocket::ignite()
.mount(
"/",
routes![
http::auth::login,
http::auth::logout,
http::cors::options,
http::action::dance,
http::action::sleep,
],
)
.launch();
}
Loading

0 comments on commit 4579a63

Please sign in to comment.