Repository: rust-lang/rustfmt (rust)

Issue:
buggy formatting on expression attributes with comments
Example code: 
```rs
#![feature(stmt_expr_attributes)]
fn main() {
    let some_function_call = || {};

    let filter = || {
        (
            // comment
            #[allow(clippy::disallowed_methods)]
            some_function_call()
        )
    };
}
```

Everytime you format it it replicates the attribute and doubles it:
```rs
#![feature(stmt_expr_attributes)]
fn main() {
    let some_function_call = || {};

    let filter = || {
        (
            // comment
            #[allow(clippy::disallowed_methods)]
            #[allow(clippy::disallowed_methods)]
            some_function_call()
        )
    };
}
```

Version: rustfmt 1.6.0-nightly (39f42ad9 2023-07-19)

Pull request description:
Bugfix/comment duplication
Fixes #5871 

The issue  was in the `rewrite_paren` function.

Since the span on expressions doesn't include attributes and attributes comes before the expression, this caused the attribute to be included in `pre_comment` since the end of the comment span was `subexpr.span.lo()`.
I changed the logic to use the span of the first attribute, if there are any attributes present. If not, default to the expression span.

Patch:
diff --git a/src/expr.rs b/src/expr.rs
index 450f1476..2f230ecc 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -1453,7 +1453,7 @@ pub(crate) fn rewrite_paren(
     let remove_nested_parens = context.config.remove_nested_parens();
     loop {
         // 1 = "(" or ")"
-        pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
+        pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span().lo());
         post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
         pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
         post_comment = rewrite_missing_comment(post_span, shape, context)?;


Test patch:
diff --git a/tests/target/issue-5871.rs b/tests/target/issue-5871.rs
new file mode 100644
index 00000000..3116533b
--- /dev/null
+++ b/tests/target/issue-5871.rs
@@ -0,0 +1,8 @@
+#![feature(stmt_expr_attributes)]
+fn okay() -> u32 {
+    (
+        // Comments in parentheses-expressions caused attributes to be duplicated.
+        #[allow(unused_variables)]
+        0
+    )
+}