From 9fb4b386cc03f8c94ad442910d3f77dbe042bb16 Mon Sep 17 00:00:00 2001 From: Ashvin Arsakularatne Date: Thu, 15 Jul 2021 19:31:25 +0530 Subject: [PATCH] refactor: soft deprecate match_arm_blocks in favour of match_arm_wrapping (#4896) Map `match_arm_blocks` option as `match_arm_wrapping` variants. --- src/config/config_type.rs | 24 +++++++++++++++++++++-- src/config/mod.rs | 11 +++++++++-- src/config/options.rs | 9 +++++++++ src/matches.rs | 40 +++++++++++++++++++++------------------ 4 files changed, 62 insertions(+), 22 deletions(-) diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 7fc4486ddcd..5db0352107a 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -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(), &_ => (), } } @@ -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 } @@ -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) } @@ -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 { diff --git a/src/config/mod.rs b/src/config/mod.rs index 8c04363b1fd..7c38f515f22 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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, @@ -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 \ @@ -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 diff --git a/src/config/options.rs b/src/config/options.rs index 3b91021813c..6ab31eb33f7 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -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, +} diff --git a/src/matches.rs b/src/matches.rs index 140ec226c02..686729aba1d 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -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, @@ -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),