Skip to content

Commit

Permalink
refactor: soft deprecate match_arm_blocks in favour of match_arm_wrap…
Browse files Browse the repository at this point in the history
…ping (rust-lang#4896)

Map `match_arm_blocks` option as `match_arm_wrapping` variants.
  • Loading branch information
ashvin021 committed Jul 27, 2021
1 parent ae22069 commit 67c47d2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
24 changes: 22 additions & 2 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ macro_rules! create_config {
| "chain_width" => self.0.set_heuristics(),
"license_template_path" => self.0.set_license_template(),
"merge_imports" => self.0.set_merge_imports(),
"match_arm_blocks" => self.0.set_match_arm_blocks(),
&_ => (),
}
}
Expand Down Expand Up @@ -166,6 +167,7 @@ macro_rules! create_config {
self.set_license_template();
self.set_ignore(dir);
self.set_merge_imports();
self.set_match_arm_blocks();
self
}

Expand Down Expand Up @@ -249,14 +251,16 @@ macro_rules! create_config {
| "chain_width" => self.set_heuristics(),
"license_template_path" => self.set_license_template(),
"merge_imports" => self.set_merge_imports(),
"match_arm_blocks" => self.set_match_arm_blocks(),
&_ => (),
}
}

#[allow(unreachable_pub)]
pub fn is_hidden_option(name: &str) -> bool {
const HIDE_OPTIONS: [&str; 5] =
["verbose", "verbose_diff", "file_lines", "width_heuristics", "merge_imports"];
const HIDE_OPTIONS: [&str; 6] =
["verbose", "verbose_diff", "file_lines", "width_heuristics", "merge_imports",
"match_arm_blocks"];
HIDE_OPTIONS.contains(&name)
}

Expand Down Expand Up @@ -421,6 +425,22 @@ macro_rules! create_config {
}
}

fn set_match_arm_blocks(&mut self) {
if self.was_set().match_arm_blocks() {
eprintln!(
"Warning: the `match_arm_blocks` option is deprecated. \
Use `match_arm_wrapping` instead"
);
if !self.was_set().match_arm_wrapping() {
self.match_arm_wrapping.2 = if self.match_arm_blocks() {
MatchArmWrapping::Default
} else {
MatchArmWrapping::NoBlockFirstLine
};
}
}
}

#[allow(unreachable_pub)]
/// Returns `true` if the config key was explicitly set and is the default value.
pub fn is_default(&self, key: &str) -> bool {
Expand Down
11 changes: 9 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ create_config! {
"Align struct fields if their diffs fits within threshold";
enum_discrim_align_threshold: usize, 0, false,
"Align enum variants discrims, if their diffs fit within threshold";
match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
the same line with the pattern of arms";
match_arm_wrapping: MatchArmWrapping, MatchArmWrapping::Default, false,
"Wrap the body of match arms according to the given options";
match_arm_blocks: bool, true, false, "(deprecated: use match_arm_wrapping instead)";
match_arm_leading_pipes: MatchArmLeadingPipe, MatchArmLeadingPipe::Never, true,
"Determines whether leading pipes are emitted on match arms";
force_multiline_blocks: bool, false, false,
Expand Down Expand Up @@ -426,6 +427,11 @@ mod test {
"Merge imports";
merge_imports: bool, false, false, "(deprecated: use imports_granularity instead)";

// match_arm_blocks deprecation
match_arm_blocks: bool, true, false, "(deprecated: use match_arm_wrapping instead)";
match_arm_wrapping: MatchArmWrapping, MatchArmWrapping::Default, false,
"Wrap the body of match arms according to the given options";

// Width Heuristics
use_small_heuristics: Heuristics, Heuristics::Default, true,
"Whether to use different formatting for items and \
Expand Down Expand Up @@ -590,6 +596,7 @@ combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_wrapping = "Default"
match_arm_blocks = true
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
Expand Down
9 changes: 9 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,12 @@ pub enum MatchArmLeadingPipe {
/// Preserve any existing leading pipes
Preserve,
}

/// Controls wrapping for match arm bodies
#[config_type]
pub enum MatchArmWrapping {
/// Follow the Style Guide Prescription
Default,
/// Don't block wrap when the first line can't fit
NoBlockFirstLine,
}
40 changes: 22 additions & 18 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use rustc_span::{BytePos, Span};

use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
use crate::config::lists::*;
use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, Version};
use crate::config::{
Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, MatchArmWrapping, Version,
};
use crate::expr::{
format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond,
ExprType, RhsTactics,
Expand Down Expand Up @@ -413,26 +415,28 @@ fn rewrite_match_body(
}

let indent_str = shape.indent.to_string_with_newline(context.config);
let (body_prefix, body_suffix) =
if context.config.match_arm_blocks() && !context.inside_macro() {
let comma = if context.config.match_block_trailing_comma() {
","
let (body_prefix, body_suffix) = if context.config.match_arm_wrapping()
== MatchArmWrapping::Default
&& !context.inside_macro()
{
let comma = if context.config.match_block_trailing_comma() {
","
} else {
""
};
let semicolon = if context.config.version() == Version::One {
""
} else {
if semicolon_for_expr(context, body) {
";"
} else {
""
};
let semicolon = if context.config.version() == Version::One {
""
} else {
if semicolon_for_expr(context, body) {
";"
} else {
""
}
};
("{", format!("{}{}}}{}", semicolon, indent_str, comma))
} else {
("", String::from(","))
}
};
("{", format!("{}{}}}{}", semicolon, indent_str, comma))
} else {
("", String::from(","))
};

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

0 comments on commit 67c47d2

Please sign in to comment.