From 793aa85d3c685297c88976a80fc7b93fa12d40ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=82ngelo=20Lovatto?= <12701614+0xangelo@users.noreply.github.com> Date: Sat, 4 Jun 2022 13:33:42 -0300 Subject: [PATCH] feat: add reorder_type_constraints option --- Configurations.md | 12 ++++++++++++ src/config/mod.rs | 3 +++ src/items.rs | 17 ++++++++++++++++- tests/source/issue-5116/a.rs | 5 +++++ tests/target/issue-5116/a.rs | 5 +++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-5116/a.rs create mode 100644 tests/target/issue-5116/a.rs diff --git a/Configurations.md b/Configurations.md index 8c84614352c..a8fe31fb54f 100644 --- a/Configurations.md +++ b/Configurations.md @@ -2148,6 +2148,18 @@ mod sit; **Note** `mod` with `#[macro_export]` will not be reordered since that could change the semantics of the original source code. +## `reorder_type_constraints` + +Reorder type constraints alphabetically in group. + +- **Default value**: `false` +- **Possible values**: `true`, `false` +- **Stable**: No + +#### `true` + +#### `false` (default) + ## `required_version` Require a specific version of rustfmt. If you want to make sure that the diff --git a/src/config/mod.rs b/src/config/mod.rs index a5169528187..95844138453 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -89,6 +89,8 @@ create_config! { reorder_imports: bool, true, true, "Reorder import and extern crate statements alphabetically"; reorder_modules: bool, true, true, "Reorder module statements alphabetically in group"; reorder_impl_items: bool, false, false, "Reorder impl items"; + reorder_type_constraints: bool, false, false, + "Reorder type constraints alphabetically in group"; // Spaces around punctuation type_punctuation_density: TypeDensity, TypeDensity::Wide, false, @@ -550,6 +552,7 @@ group_imports = "Preserve" reorder_imports = true reorder_modules = true reorder_impl_items = false +reorder_type_constraints = false type_punctuation_density = "Wide" space_before_colon = false space_after_colon = true diff --git a/src/items.rs b/src/items.rs index 79f6ff69aa9..dd4feec0873 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2721,7 +2721,22 @@ fn rewrite_generics( return Some(ident.to_owned()); } - let params = generics.params.iter(); + let mut params_vec = generics.params.clone(); + if context.config.reorder_type_constraints() { + for param in params_vec.iter_mut() { + param.bounds.sort_by_key(|bound| match bound { + ast::GenericBound::Trait(poly_trait_ref, _) => poly_trait_ref + .trait_ref + .path + .segments + .iter() + .map(|s| s.ident.to_string()) + .collect::(), + ast::GenericBound::Outlives(lifetime) => lifetime.ident.to_string(), + }) + } + } + let params = params_vec.iter(); overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span) } diff --git a/tests/source/issue-5116/a.rs b/tests/source/issue-5116/a.rs new file mode 100644 index 00000000000..989e8f80bc0 --- /dev/null +++ b/tests/source/issue-5116/a.rs @@ -0,0 +1,5 @@ +// rustfmt-reorder_type_constraints: true + +fn foo(a: T) { + todo!(); +} diff --git a/tests/target/issue-5116/a.rs b/tests/target/issue-5116/a.rs new file mode 100644 index 00000000000..8ec6fe6c967 --- /dev/null +++ b/tests/target/issue-5116/a.rs @@ -0,0 +1,5 @@ +// rustfmt-reorder_type_constraints: true + +fn foo(a: T) { + todo!(); +}