Skip to content

Commit 5dd042c

Browse files
authored
Merge pull request rust-lang#3511 from topecongiro/issue3498
Avoid overflowing item with attributes
2 parents 760ec07 + d3e578b commit 5dd042c

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,7 @@ pub fn can_be_overflowed_expr(
13431343
args_len: usize,
13441344
) -> bool {
13451345
match expr.node {
1346+
_ if !expr.attrs.is_empty() => false,
13461347
ast::ExprKind::Match(..) => {
13471348
(context.use_block_indent() && args_len == 1)
13481349
|| (context.config.indent_style() == IndentStyle::Visual && args_len > 1)

src/overflow.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ impl<'a> Spanned for OverflowableItem<'a> {
9292
}
9393

9494
impl<'a> OverflowableItem<'a> {
95+
fn has_attrs(&self) -> bool {
96+
match self {
97+
OverflowableItem::Expr(ast::Expr { attrs, .. })
98+
| OverflowableItem::GenericParam(ast::GenericParam { attrs, .. }) => !attrs.is_empty(),
99+
OverflowableItem::StructField(ast::StructField { attrs, .. }) => !attrs.is_empty(),
100+
OverflowableItem::MacroArg(MacroArg::Expr(expr)) => !expr.attrs.is_empty(),
101+
OverflowableItem::MacroArg(MacroArg::Item(item)) => !item.attrs.is_empty(),
102+
_ => false,
103+
}
104+
}
105+
95106
pub fn map<F, T>(&self, f: F) -> T
96107
where
97108
F: Fn(&dyn IntoOverflowableItem<'a>) -> T,
@@ -448,6 +459,7 @@ impl<'a> Context<'a> {
448459
// 1 = "("
449460
let combine_arg_with_callee = self.items.len() == 1
450461
&& self.items[0].is_expr()
462+
&& !self.items[0].has_attrs()
451463
&& self.ident.len() < self.context.config.tab_spaces();
452464
let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, &self.items);
453465

tests/source/expr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,16 @@ fn issue3457() {
532532
}
533533
}
534534
}
535+
536+
// #3498
537+
static REPRO: &[usize] = &[#[cfg(feature = "zero")]
538+
0];
539+
540+
fn overflow_with_attr() {
541+
foo(#[cfg(feature = "zero")]
542+
0);
543+
foobar(#[cfg(feature = "zero")]
544+
0);
545+
foobar(x, y, #[cfg(feature = "zero")]
546+
{});
547+
}

tests/target/expr.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,26 @@ fn issue3457() {
615615
}
616616
}
617617
}
618+
619+
// #3498
620+
static REPRO: &[usize] = &[
621+
#[cfg(feature = "zero")]
622+
0,
623+
];
624+
625+
fn overflow_with_attr() {
626+
foo(
627+
#[cfg(feature = "zero")]
628+
0,
629+
);
630+
foobar(
631+
#[cfg(feature = "zero")]
632+
0,
633+
);
634+
foobar(
635+
x,
636+
y,
637+
#[cfg(feature = "zero")]
638+
{},
639+
);
640+
}

0 commit comments

Comments
 (0)