Skip to content

Commit

Permalink
Format async bounds in rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Feb 13, 2024
1 parent 084ce5b commit 73536ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/tools/rustfmt/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,18 +537,29 @@ impl Rewrite for ast::Lifetime {
impl Rewrite for ast::GenericBound {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match *self {
ast::GenericBound::Trait(ref poly_trait_ref, modifiers) => {
ast::GenericBound::Trait(
ref poly_trait_ref,
ast::TraitBoundModifiers {
constness,
asyncness,
polarity,
},
) => {
let snippet = context.snippet(self.span());
let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
let mut constness = modifiers.constness.as_str().to_string();
let mut constness = constness.as_str().to_string();
if !constness.is_empty() {
constness.push(' ');
}
let polarity = modifiers.polarity.as_str();
let mut asyncness = asyncness.as_str().to_string();
if !asyncness.is_empty() {
asyncness.push(' ');
}
let polarity = polarity.as_str();
let shape = shape.offset_left(constness.len() + polarity.len())?;
poly_trait_ref
.rewrite(context, shape)
.map(|s| format!("{constness}{polarity}{s}"))
.map(|s| format!("{constness}{asyncness}{polarity}{s}"))
.map(|s| if has_paren { format!("({})", s) } else { s })
}
ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),
Expand Down
3 changes: 3 additions & 0 deletions src/tools/rustfmt/tests/target/asyncness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// rustfmt-edition: 2018

fn foo() -> impl async Fn() {}

0 comments on commit 73536ca

Please sign in to comment.