Skip to content

Commit 327c558

Browse files
authored
missing_enforced_import_rename: Do not enforce for underscores (#16352)
Fixes #16150 , do not error when importing anonymously using an underscore. In the issue it is suggested that perhaps there could be a flag to enable/disable this new behavior, but I don't see a strong case for disallowing "as _" when a rename was specified. Please let me know if you think that flag would be useful and I can implement that on top. ``` changelog: [`missing_enforced_import_rename`]: do not enforce renaming consistency in the case "import ... as _;" ```
2 parents 9302f1b + f36b249 commit 327c558

File tree

5 files changed

+7
-3
lines changed

5 files changed

+7
-3
lines changed

clippy_lints/src/missing_enforced_import_rename.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ impl LateLintPass<'_> for ImportRename {
8282
&& let Some(import) = match snip.split_once(" as ") {
8383
None => Some(snip.as_str()),
8484
Some((import, rename)) => {
85-
if rename.trim() == name.as_str() {
85+
let trimmed_rename = rename.trim();
86+
if trimmed_rename == "_" || trimmed_rename == name.as_str() {
8687
None
8788
} else {
8889
Some(import.trim())

tests/ui-toml/missing_enforced_import_rename/clippy.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ enforced-import-renames = [
66
{ path = "std::clone", rename = "foo" },
77
{ path = "std::thread::sleep", rename = "thread_sleep" },
88
{ path = "std::any::type_name", rename = "ident" },
9-
{ path = "std::sync::Mutex", rename = "StdMutie" }
9+
{ path = "std::sync::Mutex", rename = "StdMutie" },
10+
{ path = "std::io::Write", rename = "to_test_rename_as_underscore" }
1011
]

tests/ui-toml/missing_enforced_import_rename/conf_missing_enforced_import_rename.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::{
1515
sync :: Mutex as StdMutie,
1616
//~^ missing_enforced_import_renames
1717
};
18+
use std::io::Write as _;
1819

1920
fn main() {
2021
use std::collections::BTreeMap as Map;

tests/ui-toml/missing_enforced_import_rename/conf_missing_enforced_import_rename.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::{
1515
sync :: Mutex,
1616
//~^ missing_enforced_import_renames
1717
};
18+
use std::io::Write as _;
1819

1920
fn main() {
2021
use std::collections::BTreeMap as OopsWrongRename;

tests/ui-toml/missing_enforced_import_rename/conf_missing_enforced_import_rename.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LL | sync :: Mutex,
3232
| ^^^^^^^^^^^^^ help: try: `sync :: Mutex as StdMutie`
3333

3434
error: this import should be renamed
35-
--> tests/ui-toml/missing_enforced_import_rename/conf_missing_enforced_import_rename.rs:20:5
35+
--> tests/ui-toml/missing_enforced_import_rename/conf_missing_enforced_import_rename.rs:21:5
3636
|
3737
LL | use std::collections::BTreeMap as OopsWrongRename;
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `use std::collections::BTreeMap as Map`

0 commit comments

Comments
 (0)