Skip to content
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

Preserve blank line between match inner attrs and first arm #6005

Open
dtolnay opened this issue Jan 4, 2024 · 3 comments · May be fixed by #6119
Open

Preserve blank line between match inner attrs and first arm #6005

dtolnay opened this issue Jan 4, 2024 · 3 comments · May be fixed by #6119
Assignees

Comments

@dtolnay
Copy link
Member

dtolnay commented Jan 4, 2024

Example real-world code, which I have manually formatted exactly as I would want; this is the input and desired output:

#![feature(non_exhaustive_omitted_patterns_lint)]

use syn::*;

pub fn attrs_mut(e: &mut Expr) -> Option<&mut Vec<Attribute>> {
    match e {
        #![deny(non_exhaustive_omitted_patterns)]

        Expr::Array(ExprArray { attrs, .. })
        | Expr::Assign(ExprAssign { attrs, .. })
        | Expr::Async(ExprAsync { attrs, .. })
        | Expr::Await(ExprAwait { attrs, .. })
        | Expr::Binary(ExprBinary { attrs, .. })
        | Expr::Block(ExprBlock { attrs, .. })
        | Expr::Break(ExprBreak { attrs, .. })
        | Expr::Call(ExprCall { attrs, .. })
        | Expr::Cast(ExprCast { attrs, .. })
        | Expr::Closure(ExprClosure { attrs, .. })
        | Expr::Const(ExprConst { attrs, .. })
        | Expr::Continue(ExprContinue { attrs, .. })
        | Expr::Field(ExprField { attrs, .. })
        | Expr::ForLoop(ExprForLoop { attrs, .. })
        | Expr::Group(ExprGroup { attrs, .. })
        | Expr::If(ExprIf { attrs, .. })
        | Expr::Index(ExprIndex { attrs, .. })
        | Expr::Infer(ExprInfer { attrs, .. })
        | Expr::Let(ExprLet { attrs, .. })
        | Expr::Lit(ExprLit { attrs, .. })
        | Expr::Loop(ExprLoop { attrs, .. })
        | Expr::Macro(ExprMacro { attrs, .. })
        | Expr::Match(ExprMatch { attrs, .. })
        | Expr::MethodCall(ExprMethodCall { attrs, .. })
        | Expr::Paren(ExprParen { attrs, .. })
        | Expr::Path(ExprPath { attrs, .. })
        | Expr::Range(ExprRange { attrs, .. })
        | Expr::Reference(ExprReference { attrs, .. })
        | Expr::Repeat(ExprRepeat { attrs, .. })
        | Expr::Return(ExprReturn { attrs, .. })
        | Expr::Struct(ExprStruct { attrs, .. })
        | Expr::Try(ExprTry { attrs, .. })
        | Expr::TryBlock(ExprTryBlock { attrs, .. })
        | Expr::Tuple(ExprTuple { attrs, .. })
        | Expr::Unary(ExprUnary { attrs, .. })
        | Expr::Unsafe(ExprUnsafe { attrs, .. })
        | Expr::While(ExprWhile { attrs, .. })
        | Expr::Yield(ExprYield { attrs, .. }) => Some(attrs),

        Expr::Verbatim(_) => None,
        _ => None,
    }
}

But, rustfmt applies the following diff, which is undesirable.

 pub fn attrs_mut(e: &mut Expr) -> Option<&mut Vec<Attribute>> {
     match e {
         #![deny(non_exhaustive_omitted_patterns)]
-
         Expr::Array(ExprArray { attrs, .. })
         | Expr::Assign(ExprAssign { attrs, .. })
         | Expr::Async(ExprAsync { attrs, .. })

Rustfmt's formatting makes it look as though the inner attr is an outer attr on the first arm.

pub fn attrs_mut(e: &mut Expr) -> Option<&mut Vec<Attribute>> {
    match e {
        #![deny(non_exhaustive_omitted_patterns)]
        Expr::Array(ExprArray { attrs, .. })
        | Expr::Assign(ExprAssign { attrs, .. })
        | Expr::Async(ExprAsync { attrs, .. })

Module-level inner attrs, like the #![feature(...)] above, are not affected. If rustfmt were removing blank line between module-level inner attrs and the first item, I think it's obvious that would be considered undesirable:

#![feature(non_exhaustive_omitted_patterns_lint)]
use syn::*;

pub fn attrs_mut(e: &mut Expr) -> Option<&mut Vec<Attribute>> {
    match e {...}
}
@dtolnay
Copy link
Member Author

dtolnay commented Jan 4, 2024

Amusingly, this is the reverse of #5309, where a blank line is preserved between outer attributes and the item. For outer attrs the blank line should not be preserved. For inner attrs (this issue), I want it to be preserved.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 5, 2024

This is where we're rewriting the inner attributes:

rustfmt/src/matches.rs

Lines 101 to 109 in 85e21fa

// Inner attributes.
let inner_attrs = &inner_attributes(attrs);
let inner_attrs_str = if inner_attrs.is_empty() {
String::new()
} else {
inner_attrs
.rewrite(context, shape)
.map(|s| format!("{}{}\n", nested_indent_str, s))?
};

@ding-young
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants