diff --git a/src/attr.rs b/src/attr.rs index dab9cabccdc..2d25c263277 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -183,9 +183,11 @@ fn format_derive( } else if let SeparatorTactic::Always = context.config.trailing_comma() { // Retain the trailing comma. result.push_str(&item_str); - } else { + } else if item_str.ends_with(",") { // Remove the trailing comma. result.push_str(&item_str[..item_str.len() - 1]); + } else { + result.push_str(&item_str); } result.push_str(")]"); diff --git a/tests/source/issue_4584.rs b/tests/source/issue_4584.rs new file mode 100644 index 00000000000..695c5590559 --- /dev/null +++ b/tests/source/issue_4584.rs @@ -0,0 +1,19 @@ +// rustfmt-indent_style: Visual + +#[derive(Debug,)] +pub enum Case { + Upper, + Lower +} + +#[derive(Debug, Clone, PartialEq, Eq,)] +pub enum Case { + Upper, + Lower +} + +// NB - This formatting looks potentially off the desired state, but is +// consistent with current behavior. Included here to provide a line wrapped +// derive case with the changes applied to resolve issue #4584 +#[derive(Add, Sub, Mul, Div, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Serialize, Mul,)] +struct Foo {} \ No newline at end of file diff --git a/tests/target/issue_4584.rs b/tests/target/issue_4584.rs new file mode 100644 index 00000000000..20255beadb2 --- /dev/null +++ b/tests/target/issue_4584.rs @@ -0,0 +1,32 @@ +// rustfmt-indent_style: Visual + +#[derive(Debug)] +pub enum Case { + Upper, + Lower, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Case { + Upper, + Lower, +} + +// NB - This formatting looks potentially off the desired state, but is +// consistent with current behavior. Included here to provide a line wrapped +// derive case with the changes applied to resolve issue #4584 +#[derive(Add, + Sub, + Mul, + Div, + Clone, + Copy, + Eq, + PartialEq, + Ord, + PartialOrd, + Debug, + Hash, + Serialize, + Mul)] +struct Foo {}