From 77941b16e6709bb98949bac6fe6b023e82946020 Mon Sep 17 00:00:00 2001 From: GambitingMan Date: Sat, 16 Sep 2023 21:06:36 +0200 Subject: [PATCH 1/3] Fix #5871 --- src/expr.rs | 6 +++++- tests/source/issue-5871.rs | 8 ++++++++ tests/target/issue-5871.rs | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-5871.rs create mode 100644 tests/target/issue-5871.rs diff --git a/src/expr.rs b/src/expr.rs index 450f1476db0..1a49bceca91 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1453,7 +1453,11 @@ 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()); + let expr_lo = subexpr + .attrs + .first() + .map_or(subexpr.span().lo(), |attr| attr.span.lo()); + pre_span = mk_sp(span.lo() + BytePos(1), expr_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)?; diff --git a/tests/source/issue-5871.rs b/tests/source/issue-5871.rs new file mode 100644 index 00000000000..3116533bcb8 --- /dev/null +++ b/tests/source/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 + ) +} diff --git a/tests/target/issue-5871.rs b/tests/target/issue-5871.rs new file mode 100644 index 00000000000..3116533bcb8 --- /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 + ) +} From 09ff9b46c9429398d08b5252f9bd8d517e8ff6e1 Mon Sep 17 00:00:00 2001 From: GambitingMan Date: Sat, 16 Sep 2023 21:19:07 +0200 Subject: [PATCH 2/3] Only idempotence test is necessary --- tests/source/issue-5871.rs | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 tests/source/issue-5871.rs diff --git a/tests/source/issue-5871.rs b/tests/source/issue-5871.rs deleted file mode 100644 index 3116533bcb8..00000000000 --- a/tests/source/issue-5871.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![feature(stmt_expr_attributes)] -fn okay() -> u32 { - ( - // Comments in parentheses-expressions caused attributes to be duplicated. - #[allow(unused_variables)] - 0 - ) -} From 5d96ac4b224ecc48d1308e1206e7d02617a212de Mon Sep 17 00:00:00 2001 From: GambitingMan <68272188+GambitingMan@users.noreply.github.com> Date: Sun, 17 Sep 2023 07:57:01 +0200 Subject: [PATCH 3/3] Update src/expr.rs Co-authored-by: Caleb Cartwright --- src/expr.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 1a49bceca91..2f230ecc3b4 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1453,11 +1453,7 @@ pub(crate) fn rewrite_paren( let remove_nested_parens = context.config.remove_nested_parens(); loop { // 1 = "(" or ")" - let expr_lo = subexpr - .attrs - .first() - .map_or(subexpr.span().lo(), |attr| attr.span.lo()); - pre_span = mk_sp(span.lo() + BytePos(1), expr_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)?;