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

Extend inline_attribute_width to more AST types #5968

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,23 @@ pub(crate) fn format_expr(
attrs.last().map_or(expr.span.lo(), |attr| attr.span.hi()),
expr.span.lo(),
);
combine_strs_with_missing_comments(context, &attrs_str, &expr_str, span, shape, false)

let allow_extend = if attrs.len() == 1 {
let line_len = attrs_str.len() + 1 + expr_str.len();
!attrs.first().unwrap().is_doc_comment()
&& context.config.inline_attribute_width() >= line_len
} else {
false
};
trevyn marked this conversation as resolved.
Show resolved Hide resolved

combine_strs_with_missing_comments(
context,
&attrs_str,
&expr_str,
span,
shape,
allow_extend,
)
})
}

Expand Down
23 changes: 21 additions & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,17 @@ pub(crate) fn rewrite_struct_field(
let prefix = rewrite_struct_field_prefix(context, field)?;

let attrs_str = field.attrs.rewrite(context, shape)?;
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);

let allow_extend = if field.attrs.len() == 1 {
let line_len = attrs_str.len() + 1 + prefix.len();
!field.attrs.first().unwrap().is_doc_comment()
&& context.config.inline_attribute_width() >= line_len
} else {
false
};

let attrs_extendable =
(field.ident.is_none() && is_attributes_extendable(&attrs_str)) || allow_extend;
let missing_span = if field.attrs.is_empty() {
mk_sp(field.span.lo(), field.span.lo())
} else {
Expand Down Expand Up @@ -3383,13 +3393,22 @@ impl Rewrite for ast::ForeignItem {
} else {
mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
};

let allow_extend = if self.attrs.len() == 1 {
let line_len = attrs_str.len() + 1 + item_str.len();
!self.attrs.first().unwrap().is_doc_comment()
&& context.config.inline_attribute_width() >= line_len
} else {
false
};

combine_strs_with_missing_comments(
context,
&attrs_str,
&item_str,
missing_span,
shape,
false,
allow_extend,
)
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/source/issue-3343.rs
trevyn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ extern crate len_is_50_;
#[cfg(feature = "alloc")]
extern crate len_is_51__;

// https://github.com/rust-lang/rustfmt/issues/3343#issuecomment-589945611
extern "C" {
#[no_mangle]
fn foo();
}
trevyn marked this conversation as resolved.
Show resolved Hide resolved

fn main() {
#[cfg(feature = "alloc")]
foo();
#[cfg(feature = "alloc")]
{
foo();
}
{
#[cfg(feature = "alloc")]
foo();
}
}

// https://github.com/rust-lang/rustfmt/pull/5538#issuecomment-1272367684
struct EventConfigWidget {
#[widget]
menu_delay: Spinner<u32>,
}

/// this is a comment to test is_sugared_doc property
use core::convert;

Expand Down
20 changes: 20 additions & 0 deletions tests/target/issue-3343.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ use total_len_is::_51___;
#[cfg(feature = "alloc")]
extern crate len_is_51__;

// https://github.com/rust-lang/rustfmt/issues/3343#issuecomment-589945611
extern "C" {
#[no_mangle] fn foo();
}

fn main() {
#[cfg(feature = "alloc")] foo();
#[cfg(feature = "alloc")] {
foo();
}
{
#[cfg(feature = "alloc")] foo();
}
}

// https://github.com/rust-lang/rustfmt/pull/5538#issuecomment-1272367684
struct EventConfigWidget {
#[widget] menu_delay: Spinner<u32>,
}

/// this is a comment to test is_sugared_doc property
use core::convert;

Expand Down
Loading