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
27 changes: 24 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,27 @@ 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'));
fn is_multi_line_variant(item: &ListItem) -> bool {
let variant_str = item.inner_as_ref();
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
6 changes: 6 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,6 @@
enum MyType {
A { field1: bool, field2: bool },
B { field1: bool, field2: bool },
C { field1: bool, field2: bool },
D { field1: bool, field2: bool },
}
7 changes: 7 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,7 @@
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 },
}
13 changes: 13 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,13 @@
enum A {
B {
a: usize,
b: usize,
c: usize,
d: usize,
},

#[attr]
C {
a: usize,
},
}
12 changes: 12 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,12 @@
enum A {
B {
a: usize,
b: usize,
c: usize,
d: usize,
},

C {
a: usize,
},
}
13 changes: 13 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,13 @@
enum A {
B {
a: usize,
b: usize,
c: usize,
d: usize,
},

/// C
C {
a: usize,
},
}
5 changes: 1 addition & 4 deletions tests/target/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ pub enum EnumWithAttributes {
SkippedItem(String,String,), // Post-comment
#[another_attr]
#[attr2]
ItemStruct {
x: usize,
y: usize,
}, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
ItemStruct { x: usize, y: usize }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
// And another
ForcedPreflight, /* AAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
Expand Down
Loading