Skip to content

Commit a61370c

Browse files
committed
refactor: soft deprecate match_arm_blocks in favour of match_arm_wrapping (rust-lang#4896)
Map `match_arm_blocks` option as `match_arm_wrapping` variants.
1 parent 541aeba commit a61370c

File tree

4 files changed

+62
-22
lines changed

4 files changed

+62
-22
lines changed

Diff for: src/config/config_type.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ macro_rules! create_config {
106106
| "chain_width" => self.0.set_heuristics(),
107107
"license_template_path" => self.0.set_license_template(),
108108
"merge_imports" => self.0.set_merge_imports(),
109+
"match_arm_blocks" => self.0.set_match_arm_blocks(),
109110
&_ => (),
110111
}
111112
}
@@ -166,6 +167,7 @@ macro_rules! create_config {
166167
self.set_license_template();
167168
self.set_ignore(dir);
168169
self.set_merge_imports();
170+
self.set_match_arm_blocks();
169171
self
170172
}
171173

@@ -249,14 +251,16 @@ macro_rules! create_config {
249251
| "chain_width" => self.set_heuristics(),
250252
"license_template_path" => self.set_license_template(),
251253
"merge_imports" => self.set_merge_imports(),
254+
"match_arm_blocks" => self.set_match_arm_blocks(),
252255
&_ => (),
253256
}
254257
}
255258

256259
#[allow(unreachable_pub)]
257260
pub fn is_hidden_option(name: &str) -> bool {
258-
const HIDE_OPTIONS: [&str; 5] =
259-
["verbose", "verbose_diff", "file_lines", "width_heuristics", "merge_imports"];
261+
const HIDE_OPTIONS: [&str; 6] =
262+
["verbose", "verbose_diff", "file_lines", "width_heuristics", "merge_imports",
263+
"match_arm_blocks"];
260264
HIDE_OPTIONS.contains(&name)
261265
}
262266

@@ -421,6 +425,22 @@ macro_rules! create_config {
421425
}
422426
}
423427

428+
fn set_match_arm_blocks(&mut self) {
429+
if self.was_set().match_arm_blocks() {
430+
eprintln!(
431+
"Warning: the `match_arm_blocks` option is deprecated. \
432+
Use `match_arm_wrapping` instead"
433+
);
434+
if !self.was_set().match_arm_wrapping() {
435+
self.match_arm_wrapping.2 = if self.match_arm_blocks() {
436+
MatchArmWrapping::Default
437+
} else {
438+
MatchArmWrapping::NoBlockFirstLine
439+
};
440+
}
441+
}
442+
}
443+
424444
#[allow(unreachable_pub)]
425445
/// Returns `true` if the config key was explicitly set and is the default value.
426446
pub fn is_default(&self, key: &str) -> bool {

Diff for: src/config/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ create_config! {
110110
"Align struct fields if their diffs fits within threshold";
111111
enum_discrim_align_threshold: usize, 0, false,
112112
"Align enum variants discrims, if their diffs fit within threshold";
113-
match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
114-
the same line with the pattern of arms";
113+
match_arm_wrapping: MatchArmWrapping, MatchArmWrapping::Default, false,
114+
"Wrap the body of match arms according to the given options";
115+
match_arm_blocks: bool, true, false, "(deprecated: use match_arm_wrapping instead)";
115116
match_arm_leading_pipes: MatchArmLeadingPipe, MatchArmLeadingPipe::Never, true,
116117
"Determines whether leading pipes are emitted on match arms";
117118
force_multiline_blocks: bool, false, false,
@@ -426,6 +427,11 @@ mod test {
426427
"Merge imports";
427428
merge_imports: bool, false, false, "(deprecated: use imports_granularity instead)";
428429

430+
// match_arm_blocks deprecation
431+
match_arm_blocks: bool, true, false, "(deprecated: use match_arm_wrapping instead)";
432+
match_arm_wrapping: MatchArmWrapping, MatchArmWrapping::Default, false,
433+
"Wrap the body of match arms according to the given options";
434+
429435
// Width Heuristics
430436
use_small_heuristics: Heuristics, Heuristics::Default, true,
431437
"Whether to use different formatting for items and \
@@ -590,6 +596,7 @@ combine_control_expr = true
590596
overflow_delimited_expr = false
591597
struct_field_align_threshold = 0
592598
enum_discrim_align_threshold = 0
599+
match_arm_wrapping = "Default"
593600
match_arm_blocks = true
594601
match_arm_leading_pipes = "Never"
595602
force_multiline_blocks = false

Diff for: src/config/options.rs

+9
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,12 @@ pub enum MatchArmLeadingPipe {
442442
/// Preserve any existing leading pipes
443443
Preserve,
444444
}
445+
446+
/// Controls wrapping for match arm bodies
447+
#[config_type]
448+
pub enum MatchArmWrapping {
449+
/// Follow the Style Guide Prescription
450+
Default,
451+
/// Don't block wrap when the first line can't fit
452+
NoBlockFirstLine,
453+
}

Diff for: src/matches.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_span::{BytePos, Span};
77

88
use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
99
use crate::config::lists::*;
10-
use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, Version};
10+
use crate::config::{
11+
Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, MatchArmWrapping, Version,
12+
};
1113
use crate::expr::{
1214
format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond,
1315
ExprType, RhsTactics,
@@ -413,26 +415,28 @@ fn rewrite_match_body(
413415
}
414416

415417
let indent_str = shape.indent.to_string_with_newline(context.config);
416-
let (body_prefix, body_suffix) =
417-
if context.config.match_arm_blocks() && !context.inside_macro() {
418-
let comma = if context.config.match_block_trailing_comma() {
419-
","
418+
let (body_prefix, body_suffix) = if context.config.match_arm_wrapping()
419+
== MatchArmWrapping::Default
420+
&& !context.inside_macro()
421+
{
422+
let comma = if context.config.match_block_trailing_comma() {
423+
","
424+
} else {
425+
""
426+
};
427+
let semicolon = if context.config.version() == Version::One {
428+
""
429+
} else {
430+
if semicolon_for_expr(context, body) {
431+
";"
420432
} else {
421433
""
422-
};
423-
let semicolon = if context.config.version() == Version::One {
424-
""
425-
} else {
426-
if semicolon_for_expr(context, body) {
427-
";"
428-
} else {
429-
""
430-
}
431-
};
432-
("{", format!("{}{}}}{}", semicolon, indent_str, comma))
433-
} else {
434-
("", String::from(","))
434+
}
435435
};
436+
("{", format!("{}{}}}{}", semicolon, indent_str, comma))
437+
} else {
438+
("", String::from(","))
439+
};
436440

437441
let block_sep = match context.config.control_brace_style() {
438442
ControlBraceStyle::AlwaysNextLine => format!("{}{}", alt_block_sep, body_prefix),

0 commit comments

Comments
 (0)