From baafa4f011d6c50f51f9c1de718e37d23a533373 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sat, 19 Aug 2017 14:57:01 -0700 Subject: [PATCH] Fix wrapping of bounds in associated types Bounds were wrapped to the full width of the line rather then the width available after the "type ...: ", resulting in rustfmt unnecessarily producing lines that were longer than the maximum width. --- src/items.rs | 3 ++- tests/source/associated-types-bounds-wrapping.rs | 6 ++++++ tests/target/associated-types-bounds-wrapping.rs | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/source/associated-types-bounds-wrapping.rs create mode 100644 tests/target/associated-types-bounds-wrapping.rs diff --git a/src/items.rs b/src/items.rs index 302d15ee8aaa..0eed4204d45b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1551,7 +1551,8 @@ pub fn rewrite_associated_type( let prefix = format!("type {}", ident); let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt { - let shape = Shape::indented(indent, context.config); + // 2 = ": ".len() + let shape = try_opt!(Shape::indented(indent, context.config).offset_left(prefix.len() + 2)); let bounds: &[_] = ty_param_bounds; let bound_str = try_opt!( bounds diff --git a/tests/source/associated-types-bounds-wrapping.rs b/tests/source/associated-types-bounds-wrapping.rs new file mode 100644 index 000000000000..8f5e21725691 --- /dev/null +++ b/tests/source/associated-types-bounds-wrapping.rs @@ -0,0 +1,6 @@ +// rustfmt-max_width: 100 +// Test proper wrapping of long associated type bounds + +pub trait HttpService { + type WsService: 'static + Service; +} diff --git a/tests/target/associated-types-bounds-wrapping.rs b/tests/target/associated-types-bounds-wrapping.rs new file mode 100644 index 000000000000..d5b94a5e8699 --- /dev/null +++ b/tests/target/associated-types-bounds-wrapping.rs @@ -0,0 +1,7 @@ +// rustfmt-max_width: 100 +// Test proper wrapping of long associated type bounds + +pub trait HttpService { + type WsService: 'static + + Service; +}