diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 14217caba0a..7671144c9b6 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -431,18 +431,24 @@ macro_rules! create_config { heuristic_value: usize, config_key: &str, | -> usize { - if !was_set { - return heuristic_value; - } - if override_value > max_width { + let value = if !was_set { + heuristic_value + } else { + override_value + }; + + let value = value.min(max_width); + + if 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, ); - return max_width; + max_width + } else { + value } - override_value }; let fn_call_width = get_width_value( diff --git a/src/config/mod.rs b/src/config/mod.rs index ea257c0a8ba..2a7ee8cfc84 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -680,6 +680,21 @@ mod test { assert_eq!(config.was_set().verbose(), false); } + #[test] + fn test_clamp_to_max_width() { + let toml = r#" + max_width = 100 + struct_lit_width = 200 + "#; + let mut config = Config::from_toml(toml, Path::new("./rustfmt.toml")).unwrap(); + assert_eq!(config.struct_lit_width(), config.max_width()); + + // simulate entering a macro scope + let new = config.max_width() - 4; + config.set().max_width(new); + assert_eq!(config.struct_lit_width(), config.max_width()); + } + const PRINT_DOCS_STABLE_OPTION: &str = "stable_option Default: false"; const PRINT_DOCS_UNSTABLE_OPTION: &str = "unstable_option Default: false (unstable)"; const PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION: &str = @@ -1049,10 +1064,10 @@ make_backup = false max_width = 100 "#; let config = Config::from_toml(toml, Path::new("./rustfmt.toml")).unwrap(); - assert_eq!(config.array_width(), usize::MAX); - assert_eq!(config.attr_fn_like_width(), usize::MAX); - assert_eq!(config.chain_width(), usize::MAX); - assert_eq!(config.fn_call_width(), usize::MAX); + assert_eq!(config.array_width(), config.max_width()); + assert_eq!(config.attr_fn_like_width(), config.max_width()); + assert_eq!(config.chain_width(), config.max_width()); + assert_eq!(config.fn_call_width(), config.max_width()); assert_eq!(config.single_line_if_else_max_width(), 0); assert_eq!(config.struct_lit_width(), 0); assert_eq!(config.struct_variant_width(), 0);