Skip to content

Feature: add reorder_type_constraints option #5370

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>(),
ast::GenericBound::Outlives(lifetime) => lifetime.ident.to_string(),
})
}
}
let params = params_vec.iter();
overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
}

Expand Down
5 changes: 5 additions & 0 deletions tests/source/issue-5116/a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-reorder_type_constraints: true

fn foo<T: PartialEq + Copy + Debug + 'static + Clone>(a: T) {
todo!();
}
5 changes: 5 additions & 0 deletions tests/target/issue-5116/a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-reorder_type_constraints: true

fn foo<T: 'static + Clone + Copy + Debug + PartialEq>(a: T) {
todo!();
}