From 917806ace65e580f6537c80bdf44f7b371a9b901 Mon Sep 17 00:00:00 2001 From: Robert Sammelson Date: Fri, 21 Jul 2023 00:02:15 -0400 Subject: [PATCH 1/3] Fix issues with formatting imports with comments --- src/imports.rs | 6 ++- tests/source/issue-5852.rs | 104 +++++++++++++++++++++++++++++++++++++ tests/target/issue-5852.rs | 97 ++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 tests/source/issue-5852.rs create mode 100644 tests/target/issue-5852.rs diff --git a/src/imports.rs b/src/imports.rs index 6f0050647dc..f8e7fa62890 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -589,7 +589,7 @@ impl UseTree { // Normalise foo::{bar} -> foo::bar if let UseSegmentKind::List(ref list) = last.kind { - if list.len() == 1 && list[0].to_string() != "self" { + if list.len() == 1 && list[0].to_string() != "self" && !list[0].has_comment() { normalize_sole_list = true; } } @@ -1032,7 +1032,9 @@ fn rewrite_nested_use_tree( let list_str = write_list(&list_items, &fmt)?; - let result = if (list_str.contains('\n') || list_str.len() > remaining_width) + let result = if (list_str.contains('\n') + || list_str.len() > remaining_width + || tactic == DefinitiveListTactic::Vertical) && context.config.imports_indent() == IndentStyle::Block { format!( diff --git a/tests/source/issue-5852.rs b/tests/source/issue-5852.rs new file mode 100644 index 00000000000..df84f8f58e1 --- /dev/null +++ b/tests/source/issue-5852.rs @@ -0,0 +1,104 @@ +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self // this is important +}; + +use foo :: bar +; + +use foo::{bar}; + +use foo::{ + bar + // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar +}; + +use foo::{ + self + // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self +}; + +use foo::{ + self // a + , +}; + +use foo::{ self /* a */ }; + +use foo::{ self /* a */, }; + +use foo::{ + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + // abc + bar, + abc +}; + +use foo::{ + bar, + // abc + abc +}; + +use foo::{ + bar, + abc + // abc +}; + +use foo::{ + bar, + abc, + // abc +}; + +use foo::{ + self, + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz + } +}; + +use path::{self /*comment*/,}; diff --git a/tests/target/issue-5852.rs b/tests/target/issue-5852.rs new file mode 100644 index 00000000000..a86872a68bf --- /dev/null +++ b/tests/target/issue-5852.rs @@ -0,0 +1,97 @@ +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self, // this is important +}; + +use foo::bar; + +use foo::bar; + +use foo::{ + bar, // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar, +}; + +use foo::{ + self, // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self, +}; + +use foo::{ + self, // a +}; + +use foo::{self /* a */}; + +use foo::{self /* a */}; + +use foo::{ + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + // abc + abc, + bar, +}; + +use foo::{ + abc, // abc + bar, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + self, + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz, + }, +}; + +use path::{self /*comment*/}; From 68a34cf8a121a0b1846f1fd93f509a42084b7dda Mon Sep 17 00:00:00 2001 From: David Bar-On Date: Wed, 17 Mar 2021 11:55:51 +0200 Subject: [PATCH 2/3] Fix issue with extra semicolon when import comment preceeds semicolon --- src/lists.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lists.rs b/src/lists.rs index a878e6cf9b2..41afef279e9 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -637,7 +637,7 @@ pub(crate) fn extract_post_comment( post_snippet.trim_matches(white_space) } // not comment or over two lines - else if post_snippet.ends_with(',') + else if post_snippet.ends_with(separator) && (!post_snippet.trim().starts_with("//") || post_snippet.trim().contains('\n')) { post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space) From e74e0e970f44b88fc69aa8e8c60cb925fc6b4364 Mon Sep 17 00:00:00 2001 From: Robert Sammelson Date: Thu, 27 Jul 2023 21:59:51 -0400 Subject: [PATCH 3/3] Improve tests for #5852 --- tests/source/issue-3984.rs | 12 ++ .../{issue-5852.rs => issue-5852/default.rs} | 0 tests/source/issue-5852/horizontal.rs | 106 +++++++++++++++++ .../source/issue-5852/horizontal_vertical.rs | 106 +++++++++++++++++ tests/source/issue-5852/issue_example.rs | 8 ++ tests/source/issue-5852/split.rs | 111 ++++++++++++++++++ tests/source/issue-5852/vertical.rs | 106 +++++++++++++++++ tests/target/issue-3984.rs | 12 ++ .../{issue-5852.rs => issue-5852/default.rs} | 0 tests/target/issue-5852/horizontal.rs | 99 ++++++++++++++++ .../target/issue-5852/horizontal_vertical.rs | 99 ++++++++++++++++ tests/target/issue-5852/issue_example.rs | 8 ++ tests/target/issue-5852/split.rs | 102 ++++++++++++++++ tests/target/issue-5852/vertical.rs | 105 +++++++++++++++++ 14 files changed, 874 insertions(+) create mode 100644 tests/source/issue-3984.rs rename tests/source/{issue-5852.rs => issue-5852/default.rs} (100%) create mode 100644 tests/source/issue-5852/horizontal.rs create mode 100644 tests/source/issue-5852/horizontal_vertical.rs create mode 100644 tests/source/issue-5852/issue_example.rs create mode 100644 tests/source/issue-5852/split.rs create mode 100644 tests/source/issue-5852/vertical.rs create mode 100644 tests/target/issue-3984.rs rename tests/target/{issue-5852.rs => issue-5852/default.rs} (100%) create mode 100644 tests/target/issue-5852/horizontal.rs create mode 100644 tests/target/issue-5852/horizontal_vertical.rs create mode 100644 tests/target/issue-5852/issue_example.rs create mode 100644 tests/target/issue-5852/split.rs create mode 100644 tests/target/issue-5852/vertical.rs diff --git a/tests/source/issue-3984.rs b/tests/source/issue-3984.rs new file mode 100644 index 00000000000..c9bcfa40635 --- /dev/null +++ b/tests/source/issue-3984.rs @@ -0,0 +1,12 @@ +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item /* comment */; +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/source/issue-5852.rs b/tests/source/issue-5852/default.rs similarity index 100% rename from tests/source/issue-5852.rs rename to tests/source/issue-5852/default.rs diff --git a/tests/source/issue-5852/horizontal.rs b/tests/source/issue-5852/horizontal.rs new file mode 100644 index 00000000000..63bfb7e57ce --- /dev/null +++ b/tests/source/issue-5852/horizontal.rs @@ -0,0 +1,106 @@ +// rustfmt-imports_layout: Horizontal + +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self // this is important +}; + +use foo :: bar +; + +use foo::{bar}; + +use foo::{ + bar + // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar +}; + +use foo::{ + self + // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self +}; + +use foo::{ + self // a + , +}; + +use foo::{ self /* a */ }; + +use foo::{ self /* a */, }; + +use foo::{ + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + // abc + bar, + abc +}; + +use foo::{ + bar, + // abc + abc +}; + +use foo::{ + bar, + abc + // abc +}; + +use foo::{ + bar, + abc, + // abc +}; + +use foo::{ + self, + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz + } +}; + +use path::{self /*comment*/,}; diff --git a/tests/source/issue-5852/horizontal_vertical.rs b/tests/source/issue-5852/horizontal_vertical.rs new file mode 100644 index 00000000000..3f3ce066909 --- /dev/null +++ b/tests/source/issue-5852/horizontal_vertical.rs @@ -0,0 +1,106 @@ +// rustfmt-imports_layout: HorizontalVertical + +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self // this is important +}; + +use foo :: bar +; + +use foo::{bar}; + +use foo::{ + bar + // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar +}; + +use foo::{ + self + // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self +}; + +use foo::{ + self // a + , +}; + +use foo::{ self /* a */ }; + +use foo::{ self /* a */, }; + +use foo::{ + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + // abc + bar, + abc +}; + +use foo::{ + bar, + // abc + abc +}; + +use foo::{ + bar, + abc + // abc +}; + +use foo::{ + bar, + abc, + // abc +}; + +use foo::{ + self, + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz + } +}; + +use path::{self /*comment*/,}; diff --git a/tests/source/issue-5852/issue_example.rs b/tests/source/issue-5852/issue_example.rs new file mode 100644 index 00000000000..20c2b764011 --- /dev/null +++ b/tests/source/issue-5852/issue_example.rs @@ -0,0 +1,8 @@ +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self // this is important +}; diff --git a/tests/source/issue-5852/split.rs b/tests/source/issue-5852/split.rs new file mode 100644 index 00000000000..14e9ea1b6ca --- /dev/null +++ b/tests/source/issue-5852/split.rs @@ -0,0 +1,111 @@ +// rustfmt-imports_granularity: Item + +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self // this is important +}; + +use foo :: bar +; + +use foo::{bar}; + +use foo::{ + bar + // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar +}; + +use foo::{ + self + // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self +}; + +use foo::{ + self // a + , +}; + +use foo::{ self /* a */ }; + +use foo::{ self /* a */, }; + +use foo::{ + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + bar, + abc +}; + +use foo::{ + // abc + bar, + abc +}; + +use foo::{ + bar, + // abc + abc +}; + +use foo::{ + bar, + abc + // abc +}; + +use foo::{ + bar, + abc, + // abc +}; + +use foo::{ + self, + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz + } +}; + +use path::{self /*comment*/,}; diff --git a/tests/source/issue-5852/vertical.rs b/tests/source/issue-5852/vertical.rs new file mode 100644 index 00000000000..b9ba99889ac --- /dev/null +++ b/tests/source/issue-5852/vertical.rs @@ -0,0 +1,106 @@ +// rustfmt-imports_layout: Vertical + +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self // this is important +}; + +use foo :: bar +; + +use foo::{bar}; + +use foo::{ + bar + // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar +}; + +use foo::{ + self + // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self +}; + +use foo::{ + self // a + , +}; + +use foo::{ self /* a */ }; + +use foo::{ self /* a */, }; + +use foo::{ + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + // abc + bar, + abc +}; + +use foo::{ + bar, + // abc + abc +}; + +use foo::{ + bar, + abc + // abc +}; + +use foo::{ + bar, + abc, + // abc +}; + +use foo::{ + self, + // abc + abc::{ + xyz + // 123 + } +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz + } +}; + +use path::{self /*comment*/,}; diff --git a/tests/target/issue-3984.rs b/tests/target/issue-3984.rs new file mode 100644 index 00000000000..9e700c0f761 --- /dev/null +++ b/tests/target/issue-3984.rs @@ -0,0 +1,12 @@ +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item; /* comment */ +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/target/issue-5852.rs b/tests/target/issue-5852/default.rs similarity index 100% rename from tests/target/issue-5852.rs rename to tests/target/issue-5852/default.rs diff --git a/tests/target/issue-5852/horizontal.rs b/tests/target/issue-5852/horizontal.rs new file mode 100644 index 00000000000..017d83c9f9a --- /dev/null +++ b/tests/target/issue-5852/horizontal.rs @@ -0,0 +1,99 @@ +// rustfmt-imports_layout: Horizontal + +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self, // this is important +}; + +use foo::bar; + +use foo::bar; + +use foo::{ + bar, // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar, +}; + +use foo::{ + self, // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self, +}; + +use foo::{ + self, // a +}; + +use foo::{self /* a */}; + +use foo::{self /* a */}; + +use foo::{ + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + // abc + abc, + bar, +}; + +use foo::{ + abc, // abc + bar, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + self, + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz, + }, +}; + +use path::{self /*comment*/}; diff --git a/tests/target/issue-5852/horizontal_vertical.rs b/tests/target/issue-5852/horizontal_vertical.rs new file mode 100644 index 00000000000..35e2d0a2624 --- /dev/null +++ b/tests/target/issue-5852/horizontal_vertical.rs @@ -0,0 +1,99 @@ +// rustfmt-imports_layout: HorizontalVertical + +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self, // this is important +}; + +use foo::bar; + +use foo::bar; + +use foo::{ + bar, // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar, +}; + +use foo::{ + self, // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self, +}; + +use foo::{ + self, // a +}; + +use foo::{self /* a */}; + +use foo::{self /* a */}; + +use foo::{ + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + // abc + abc, + bar, +}; + +use foo::{ + abc, // abc + bar, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + self, + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz, + }, +}; + +use path::{self /*comment*/}; diff --git a/tests/target/issue-5852/issue_example.rs b/tests/target/issue-5852/issue_example.rs new file mode 100644 index 00000000000..46326291433 --- /dev/null +++ b/tests/target/issue-5852/issue_example.rs @@ -0,0 +1,8 @@ +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self, // this is important +}; diff --git a/tests/target/issue-5852/split.rs b/tests/target/issue-5852/split.rs new file mode 100644 index 00000000000..e00086dd47b --- /dev/null +++ b/tests/target/issue-5852/split.rs @@ -0,0 +1,102 @@ +// rustfmt-imports_granularity: Item + +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self, // this is important +}; + +use foo::bar; + +use foo::bar; + +use foo::{ + bar, // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar, +}; + +use foo::{ + self, // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self, +}; + +use foo::{ + self, // a +}; + +use foo::{self /* a */}; + +use foo::{self /* a */}; + +use foo::{ + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::abc; +use foo::bar; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + // abc + abc, + bar, +}; + +use foo::{ + abc, // abc + bar, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + self, + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz, + }, +}; + +use path::{self /*comment*/}; diff --git a/tests/target/issue-5852/vertical.rs b/tests/target/issue-5852/vertical.rs new file mode 100644 index 00000000000..f53a68c944a --- /dev/null +++ b/tests/target/issue-5852/vertical.rs @@ -0,0 +1,105 @@ +// rustfmt-imports_layout: Vertical + +use std::{ + fs, + // (temporarily commented, we'll need this again in a second) io, +}; + +use foo::{ + self, // this is important +}; + +use foo::bar; + +use foo::bar; + +use foo::{ + bar, // abc +}; + +use foo::{ + bar, + // abc +}; + +use foo::{ + // 345 + bar, +}; + +use foo::{ + self, // abc +}; + +use foo::{ + self, + // abc +}; + +use foo::{ + // 345 + self, +}; + +use foo::{ + self, // a +}; + +use foo::{ + self, /* a */ +}; + +use foo::{ + self, /* a */ +}; + +use foo::{ + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + // abc + abc, + bar, +}; + +use foo::{ + abc, // abc + bar, +}; + +use foo::{ + abc, + // abc + bar, +}; + +use foo::{ + self, + // abc + abc::{ + xyz, // 123 + }, +}; + +use foo::{ + self, + // abc + abc::{ + // 123 + xyz, + }, +}; + +use path::{ + self, /*comment*/ +};