Skip to content

Commit f772d4c

Browse files
committed
Clamp width settings to max_width before warning about exceeding it
Within a macro scope, max_width is reduced, which can trigger warnings if it is reduced below some other width setting (e.g. struct_lit_width.) Width settings were already being clamped to max_width, but only after the warning fired. The order is now reversed.
1 parent 5f48fe9 commit f772d4c

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/config/config_type.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,24 @@ macro_rules! create_config {
431431
heuristic_value: usize,
432432
config_key: &str,
433433
| -> usize {
434-
if !was_set {
435-
return heuristic_value;
436-
}
437-
if override_value > max_width {
434+
let value = if !was_set {
435+
heuristic_value
436+
} else {
437+
override_value
438+
};
439+
440+
let value = value.min(max_width);
441+
442+
if value > max_width {
438443
eprintln!(
439444
"`{0}` cannot have a value that exceeds `max_width`. \
440445
`{0}` will be set to the same value as `max_width`",
441446
config_key,
442447
);
443-
return max_width;
448+
max_width
449+
} else {
450+
value
444451
}
445-
override_value
446452
};
447453

448454
let fn_call_width = get_width_value(

src/config/mod.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,21 @@ mod test {
680680
assert_eq!(config.was_set().verbose(), false);
681681
}
682682

683+
#[test]
684+
fn test_clamp_to_max_width() {
685+
let toml = r#"
686+
max_width = 100
687+
struct_lit_width = 200
688+
"#;
689+
let mut config = Config::from_toml(toml, Path::new("./rustfmt.toml")).unwrap();
690+
assert_eq!(config.struct_lit_width(), config.max_width());
691+
692+
// simulate entering a macro scope
693+
let new = config.max_width() - 4;
694+
config.set().max_width(new);
695+
assert_eq!(config.struct_lit_width(), config.max_width());
696+
}
697+
683698
const PRINT_DOCS_STABLE_OPTION: &str = "stable_option <boolean> Default: false";
684699
const PRINT_DOCS_UNSTABLE_OPTION: &str = "unstable_option <boolean> Default: false (unstable)";
685700
const PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION: &str =
@@ -1049,10 +1064,10 @@ make_backup = false
10491064
max_width = 100
10501065
"#;
10511066
let config = Config::from_toml(toml, Path::new("./rustfmt.toml")).unwrap();
1052-
assert_eq!(config.array_width(), usize::MAX);
1053-
assert_eq!(config.attr_fn_like_width(), usize::MAX);
1054-
assert_eq!(config.chain_width(), usize::MAX);
1055-
assert_eq!(config.fn_call_width(), usize::MAX);
1067+
assert_eq!(config.array_width(), config.max_width());
1068+
assert_eq!(config.attr_fn_like_width(), config.max_width());
1069+
assert_eq!(config.chain_width(), config.max_width());
1070+
assert_eq!(config.fn_call_width(), config.max_width());
10561071
assert_eq!(config.single_line_if_else_max_width(), 0);
10571072
assert_eq!(config.struct_lit_width(), 0);
10581073
assert_eq!(config.struct_variant_width(), 0);

0 commit comments

Comments
 (0)