Skip to content

Not removing commas from end of multiple attributes list #5394

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ impl Rewrite for ast::MetaItem {
}
ast::MetaItemKind::List(ref list) => {
let path = rewrite_path(context, PathContext::Type, None, &self.path, shape)?;
let has_trailing_comma = crate::expr::span_ends_with_comma(context, self.span);
let has_trailing_comma =
crate::expr::span_ends_with_comma(context, self.span, true);
overflow::rewrite_with_parens(
context,
&path,
Expand Down
23 changes: 17 additions & 6 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit, shape: Shape) -

fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {
if context.inside_macro() {
if span_ends_with_comma(context, span) {
if span_ends_with_comma(context, span, false) {
Some(SeparatorTactic::Always)
} else {
Some(SeparatorTactic::Never)
Expand Down Expand Up @@ -1417,16 +1417,27 @@ pub(crate) fn is_nested_call(expr: &ast::Expr) -> bool {
/// Returns `true` if a function call or a method call represented by the given span ends with a
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
/// comma from macro can potentially break the code.
pub(crate) fn span_ends_with_comma(context: &RewriteContext<'_>, span: Span) -> bool {
pub(crate) fn span_ends_with_comma(
context: &RewriteContext<'_>,
span: Span,
is_attributes: bool,
) -> bool {
let mut result: bool = Default::default();
let mut prev_char: char = Default::default();
let closing_delimiters = &[')', '}', ']'];

for (kind, c) in CharClasses::new(context.snippet(span).chars()) {
let snippet = context.snippet(span);
let mut len = snippet.len();
for (kind, c) in CharClasses::new(snippet.chars()) {
len -= 1;
match c {
_ if kind.is_comment() || c.is_whitespace() => continue,
c if closing_delimiters.contains(&c) => {
result &= !closing_delimiters.contains(&prev_char);
// #3277 fix - for Attributes, ignore the closing ']', as in case of attributes
// list, the last AST item includes both the list ')' and attributes ']' closings.
if !is_attributes || len != 0 || c != ']' {
result &= !closing_delimiters.contains(&prev_char);
}
}
',' => result = true,
_ => result = false,
Expand Down Expand Up @@ -1672,7 +1683,7 @@ fn rewrite_struct_lit<'a>(
let tactic = struct_lit_tactic(h_shape, context, &item_vec);
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);

let ends_with_comma = span_ends_with_comma(context, span);
let ends_with_comma = span_ends_with_comma(context, span, false);
let force_no_trailing_comma = context.inside_macro() && !ends_with_comma;

let fmt = struct_lit_formatting(
Expand Down Expand Up @@ -1843,7 +1854,7 @@ pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
if context.use_block_indent() {
// We use the same rule as function calls for rewriting tuples.
let force_tactic = if context.inside_macro() {
if span_ends_with_comma(context, span) {
if span_ends_with_comma(context, span, false) {
Some(SeparatorTactic::Always)
} else {
Some(SeparatorTactic::Never)
Expand Down
160 changes: 160 additions & 0 deletions tests/source/issue-3277/one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// rustfmt-version: One

// Original issue case
#![allow(clippy::needless_pass_by_value, clippy::new_ret_no_self, clippy::new_without_default_derive,)]

// Different cases with commas between parenthesis
#[cfg(feature1 = "std1",feature2 = "std2",)]
#[cfg(all(feature1 = "std1",feature2 = "std2",))]
#[cfg(all(feature1 = "std1",feature2 = "std2",),)]
#[cfg(all(all(feature1 = "std1",feature2 = "std2",),),)]
#[cfg(all(all(feature1 = "std1",feature2 = "std2",)),)]
#[cfg(all(all(feature1 = "std1",feature2 = "std2",)))]
type Os = NoSource;

// Comma at the end of the last list items
#[live_prop_test(
precondition = "inputs.input_flows.len() == self.num_inputs()",
postcondition = "result.len() == self.num_outputs()",
postcondition = "output_times_valid(inputs, &result)",
)]
fn output_flows(
&self,
inputs: MachineObservedInputs,
future: &Self::Future,
) -> Inputs<Option<MaterialFlow>> {
inputs![]
}

#[cfg(all(feature1 = "std1",feature2 = "std2",))]
#[cfg(all(feature1 = "long textttttttttttttttttttttttttttttttttttttttt",feature2 = "long textttttttttttttttttttttttttttttttttttttttt",))]
#[cfg(not(all(
feature1 = "long textttttttttttttttttttttttttttttttttttttttt",
feature2 = "long textttttttttttttttttttttttttttttttttttttttt",
)))]
type Os = NoSource;

#[cfg(not(all(
feature = "std",
any(target_os = "linux",
target_os = "android",
)
)))]
#[cfg(not(all(
feature = "std",
any(target_os = "linuxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
target_os = "android",
)
)))]
#[cfg(not(all(
any(
target_os = "linux",
target_os = "android",
),
feature = "std",
)))]
#[cfg(
any(
target_os = "linux",
target_os = "android",
)
)]
#[cfg(
target_os = "linux",
target_os = "android",
)]
#[cfg(not(all(
target_os = "linux",
target_os = "android",
),
feature = "std",
))]
type Os = NoSource;

#[cfg(
any(
target_os = "linux",
all(
target_os = "android",
target_os = "windows",
)
)
)]
#[cfg(
any(
target_os = "linux",
all(
target_os = "android",
target_os = "windows",
),
target_os = "unix",
)
)]
type Os = NoSource;

// Tests with no comma at the end of the last list items
#[live_prop_test(
precondition = "inputs.input_flows.len() == self.num_inputs()",
postcondition = "result.len() == self.num_outputs()",
postcondition = "output_times_valid(inputs, &result)"
)]
fn output_flows(
&self,
inputs: MachineObservedInputs,
future: &Self::Future,
) -> Inputs<Option<MaterialFlow>> {
inputs![]
}

#[cfg(all(feature1 = "long textttttttttttttttttttttttttttttttttttttttt",feature2 = "long textttttttttttttttttttttttttttttttttttttttt"))]
#[cfg(not(all(
feature1 = "long textttttttttttttttttttttttttttttttttttttttt",
feature2 = "long textttttttttttttttttttttttttttttttttttttttt"
)))]
type Os = NoSource;

#[cfg(not(all(
feature = "std",
any(target_os = "linux",
target_os = "android"
)
)))]
#[cfg(not(all(
any(
target_os = "linux",
target_os = "android"
),
feature = "std"
)))]
#[cfg(
any(
target_os = "linux",
target_os = "android"
)
)]
#[cfg(
target_os = "linux",
target_os = "android"
)]
type Os = NoSource;

#[cfg(
any(
target_os = "linux",
all(
target_os = "android",
target_os = "windows"
)
)
)]
#[cfg(
any(
target_os = "linux",
all(
target_os = "android",
target_os = "windows"
),
target_os = "unix"
)
)]
type Os = NoSource;
160 changes: 160 additions & 0 deletions tests/source/issue-3277/two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// rustfmt-version: Two

// Original issue case
#![allow(clippy::needless_pass_by_value, clippy::new_ret_no_self, clippy::new_without_default_derive,)]

// Different cases with commas between parenthesis
#[cfg(feature1 = "std1",feature2 = "std2",)]
#[cfg(all(feature1 = "std1",feature2 = "std2",))]
#[cfg(all(feature1 = "std1",feature2 = "std2",),)]
#[cfg(all(all(feature1 = "std1",feature2 = "std2",),),)]
#[cfg(all(all(feature1 = "std1",feature2 = "std2",)),)]
#[cfg(all(all(feature1 = "std1",feature2 = "std2",)))]
type Os = NoSource;

// Comma at the end of the last list items
#[live_prop_test(
precondition = "inputs.input_flows.len() == self.num_inputs()",
postcondition = "result.len() == self.num_outputs()",
postcondition = "output_times_valid(inputs, &result)",
)]
fn output_flows(
&self,
inputs: MachineObservedInputs,
future: &Self::Future,
) -> Inputs<Option<MaterialFlow>> {
inputs![]
}

#[cfg(all(feature1 = "std1",feature2 = "std2",))]
#[cfg(all(feature1 = "long textttttttttttttttttttttttttttttttttttttttt",feature2 = "long textttttttttttttttttttttttttttttttttttttttt",))]
#[cfg(not(all(
feature1 = "long textttttttttttttttttttttttttttttttttttttttt",
feature2 = "long textttttttttttttttttttttttttttttttttttttttt",
)))]
type Os = NoSource;

#[cfg(not(all(
feature = "std",
any(target_os = "linux",
target_os = "android",
)
)))]
#[cfg(not(all(
feature = "std",
any(target_os = "linuxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
target_os = "android",
)
)))]
#[cfg(not(all(
any(
target_os = "linux",
target_os = "android",
),
feature = "std",
)))]
#[cfg(
any(
target_os = "linux",
target_os = "android",
)
)]
#[cfg(
target_os = "linux",
target_os = "android",
)]
#[cfg(not(all(
target_os = "linux",
target_os = "android",
),
feature = "std",
))]
type Os = NoSource;

#[cfg(
any(
target_os = "linux",
all(
target_os = "android",
target_os = "windows",
)
)
)]
#[cfg(
any(
target_os = "linux",
all(
target_os = "android",
target_os = "windows",
),
target_os = "unix",
)
)]
type Os = NoSource;

// Tests with no comma at the end of the last list items
#[live_prop_test(
precondition = "inputs.input_flows.len() == self.num_inputs()",
postcondition = "result.len() == self.num_outputs()",
postcondition = "output_times_valid(inputs, &result)"
)]
fn output_flows(
&self,
inputs: MachineObservedInputs,
future: &Self::Future,
) -> Inputs<Option<MaterialFlow>> {
inputs![]
}

#[cfg(all(feature1 = "long textttttttttttttttttttttttttttttttttttttttt",feature2 = "long textttttttttttttttttttttttttttttttttttttttt"))]
#[cfg(not(all(
feature1 = "long textttttttttttttttttttttttttttttttttttttttt",
feature2 = "long textttttttttttttttttttttttttttttttttttttttt"
)))]
type Os = NoSource;

#[cfg(not(all(
feature = "std",
any(target_os = "linux",
target_os = "android"
)
)))]
#[cfg(not(all(
any(
target_os = "linux",
target_os = "android"
),
feature = "std"
)))]
#[cfg(
any(
target_os = "linux",
target_os = "android"
)
)]
#[cfg(
target_os = "linux",
target_os = "android"
)]
type Os = NoSource;

#[cfg(
any(
target_os = "linux",
all(
target_os = "android",
target_os = "windows"
)
)
)]
#[cfg(
any(
target_os = "linux",
all(
target_os = "android",
target_os = "windows"
),
target_os = "unix"
)
)]
type Os = NoSource;
7 changes: 7 additions & 0 deletions tests/target/issue-3228/one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-version: One

// Making sure trailing commas are not removed from attributes (no need for test source)
#![cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always,))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always,),)]
#![cfg_attr(feature = "cargo-clippy",)]
#![allow(nline_always,)]
Loading