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

Fix handling of attribute in enum #6286

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
12 changes: 9 additions & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,17 @@ enum Operation {
/// Print version information
Version,
/// Output default config to a file, or stdout if None
ConfigOutputDefault { path: Option<String> },
ConfigOutputDefault {
path: Option<String>,
},
/// Output current config (as if formatting to a file) to stdout
ConfigOutputCurrent { path: Option<String> },
ConfigOutputCurrent {
path: Option<String>,
},
/// No file specified, read from stdin
Stdin { input: String },
Stdin {
input: String,
},
}

/// Rustfmt operations errors.
Expand Down
33 changes: 30 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use crate::expr::{
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, rewrite_let_else_block,
RhsAssignKind, RhsTactics,
};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::lists::{
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
};
use crate::macros::{rewrite_macro, MacroPosition};
use crate::overflow;
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
Expand Down Expand Up @@ -643,8 +645,33 @@ impl<'a> FmtVisitor<'a> {
let mut items: Vec<_> = itemize_list_with(self.config.struct_variant_width());

// If one of the variants use multiple lines, use multi-lined formatting for all variants.
let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains('\n'));
let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains('\n'));
let is_multi_line_variant = |item: &ListItem| -> bool {
let variant_str = item.inner_as_ref();
if self.config.style_edition() < StyleEdition::Edition2024 {
malikolivier marked this conversation as resolved.
Show resolved Hide resolved
// Fall back to previous naive implementation (#5662) because of
// rustfmt's stability guarantees
return variant_str.contains('\n');
}

let mut first_line_is_read = false;
for line in variant_str.split('\n') {
if first_line_is_read {
return false;
}

// skip rustdoc comments and macro attributes
let line = line.trim_start();
if line.starts_with("///") || line.starts_with("#") {
continue;
} else {
first_line_is_read = true;
}
}

true
};
let has_multiline_variant = items.iter().any(is_multi_line_variant);
let has_single_line_variant = items.iter().any(|item| !is_multi_line_variant(item));
malikolivier marked this conversation as resolved.
Show resolved Hide resolved
if has_multiline_variant && has_single_line_variant {
items = itemize_list_with(0);
}
Expand Down
8 changes: 6 additions & 2 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ pub struct ModuleResolutionError {
pub(crate) enum ModuleResolutionErrorKind {
/// Find a file that cannot be parsed.
#[error("cannot parse {file}")]
ParseError { file: PathBuf },
ParseError {
file: PathBuf,
},
/// File cannot be found.
#[error("{file} does not exist")]
NotFound { file: PathBuf },
NotFound {
file: PathBuf,
},
/// File a.rs and a/mod.rs both exist
#[error("file for module found at both {default_path:?} and {secondary_path:?}")]
MultipleCandidates {
Expand Down
7 changes: 7 additions & 0 deletions tests/target/attribute-in-enum/horizontal-no-doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-style_edition: 2024
enum MyType {
A { field1: bool, field2: bool },
B { field1: bool, field2: bool },
C { field1: bool, field2: bool },
D { field1: bool, field2: bool },
}
8 changes: 8 additions & 0 deletions tests/target/attribute-in-enum/horizontal-with-doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-style_edition: 2024
enum MyType {
A { field1: bool, field2: bool },
B { field1: bool, field2: bool },
/// OMG a comment
C { field1: bool, field2: bool },
D { field1: bool, field2: bool },
}
14 changes: 14 additions & 0 deletions tests/target/attribute-in-enum/vertical-macro-one-line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-style_edition: 2024
enum A {
B {
a: usize,
b: usize,
c: usize,
d: usize,
},

#[attr]
C {
a: usize,
},
}
13 changes: 13 additions & 0 deletions tests/target/attribute-in-enum/vertical-no-doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// rustfmt-style_edition: 2024
enum A {
B {
a: usize,
b: usize,
c: usize,
d: usize,
},

C {
a: usize,
},
}
14 changes: 14 additions & 0 deletions tests/target/attribute-in-enum/vertical-with-doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-style_edition: 2024
enum A {
B {
a: usize,
b: usize,
c: usize,
d: usize,
},

/// C
C {
a: usize,
},
}
Loading