Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress warning if max_width reduced below other settings in macro #6356

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,16 @@ macro_rules! create_config {
// with `config.set_option(false)` if we ever get a stable/usable
// `concat_idents!()`.
#[allow(unreachable_pub)]
pub struct ConfigSetter<'a>(&'a mut Config);
pub struct ConfigSetter<'a> {
config: &'a mut Config,
warn: bool,
}

impl<'a> ConfigSetter<'a> {
$(
#[allow(unreachable_pub)]
pub fn $i(&mut self, value: <$ty as StyleEditionDefault>::ConfigType) {
(self.0).$i.2 = value;
(self.config).$i.2 = value;
match stringify!($i) {
"max_width"
| "use_small_heuristics"
Expand All @@ -129,11 +132,11 @@ macro_rules! create_config {
| "struct_lit_width"
| "struct_variant_width"
| "array_width"
| "chain_width" => self.0.set_heuristics(),
"merge_imports" => self.0.set_merge_imports(),
"fn_args_layout" => self.0.set_fn_args_layout(),
"hide_parse_errors" => self.0.set_hide_parse_errors(),
"version" => self.0.set_version(),
| "chain_width" => self.config.set_heuristics(self.warn),
"merge_imports" => self.config.set_merge_imports(),
"fn_args_layout" => self.config.set_fn_args_layout(),
"hide_parse_errors" => self.config.set_hide_parse_errors(),
"version" => self.config.set_version(),
&_ => (),
}
}
Expand All @@ -159,7 +162,7 @@ macro_rules! create_config {
| "struct_lit_width"
| "struct_variant_width"
| "array_width"
| "chain_width" => self.0.set_heuristics(),
| "chain_width" => self.0.set_heuristics(true),
"merge_imports" => self.0.set_merge_imports(),
"fn_args_layout" => self.0.set_fn_args_layout(),
"hide_parse_errors" => self.0.set_hide_parse_errors(),
Expand Down Expand Up @@ -226,7 +229,12 @@ macro_rules! create_config {

#[allow(unreachable_pub)]
pub fn set(&mut self) -> ConfigSetter<'_> {
ConfigSetter(self)
ConfigSetter { config: self, warn: true }
}

#[allow(unreachable_pub)]
pub fn set_no_warn(&mut self) -> ConfigSetter<'_> {
ConfigSetter { config: self, warn: false }
}

#[allow(unreachable_pub)]
Expand Down Expand Up @@ -256,7 +264,7 @@ macro_rules! create_config {
}
}
)+
self.set_heuristics();
self.set_heuristics(true);
self.set_ignore(dir);
self.set_merge_imports();
self.set_fn_args_layout();
Expand Down Expand Up @@ -359,7 +367,7 @@ macro_rules! create_config {
| "struct_lit_width"
| "struct_variant_width"
| "array_width"
| "chain_width" => self.set_heuristics(),
| "chain_width" => self.set_heuristics(true),
"merge_imports" => self.set_merge_imports(),
"fn_args_layout" => self.set_fn_args_layout(),
"hide_parse_errors" => self.set_hide_parse_errors(),
Expand Down Expand Up @@ -423,7 +431,7 @@ macro_rules! create_config {
)+
}

fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
fn set_width_heuristics(&mut self, heuristics: WidthHeuristics, warn: bool) {
let max_width = self.max_width.2;
let get_width_value = |
was_set: bool,
Expand All @@ -435,11 +443,13 @@ macro_rules! create_config {
return heuristic_value;
}
if override_value > max_width {
eprintln!(
"`{0}` cannot have a value that exceeds `max_width`. \
`{0}` will be set to the same value as `max_width`",
config_key,
);
if warn {
eprintln!(
"`{0}` cannot have a value that exceeds `max_width`. \
`{0}` will be set to the same value as `max_width` {1} - {2}",
config_key, override_value, max_width
);
}
return max_width;
}
override_value
Expand Down Expand Up @@ -510,13 +520,16 @@ macro_rules! create_config {
self.single_line_let_else_max_width.2 = single_line_let_else_max_width;
}

fn set_heuristics(&mut self) {
fn set_heuristics(&mut self, warn: bool) {
let max_width = self.max_width.2;
match self.use_small_heuristics.2 {
Heuristics::Default =>
self.set_width_heuristics(WidthHeuristics::scaled(max_width)),
Heuristics::Max => self.set_width_heuristics(WidthHeuristics::set(max_width)),
Heuristics::Off => self.set_width_heuristics(WidthHeuristics::null()),
self.set_width_heuristics(WidthHeuristics::scaled(max_width), warn),
Heuristics::Max => self.set_width_heuristics(
WidthHeuristics::set(max_width),
warn
),
Heuristics::Off => self.set_width_heuristics(WidthHeuristics::null(), warn),
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,14 +1329,14 @@ impl MacroBranch {
shape.indent.block_indent(&config)
};
let new_width = config.max_width() - body_indent.width();
config.set().max_width(new_width);
config.set_no_warn().max_width(new_width);

// First try to format as items, then as statements.
let new_body_snippet = match crate::format_snippet(&body_str, &config, true) {
Some(new_body) => new_body,
None => {
let new_width = new_width + config.tab_spaces();
config.set().max_width(new_width);
config.set_no_warn().max_width(new_width);
match crate::format_code_block(&body_str, &config, true) {
Some(new_body) => new_body,
None => {
Expand Down
Loading