Skip to content

Commit

Permalink
fixup! Add import_rename lint, this adds a field on the Conf struct
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 committed Jun 14, 2021
1 parent 736c016 commit 7aeb626
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
9 changes: 8 additions & 1 deletion clippy_lints/src/import_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ impl LateLintPass<'_> for ImportRename {
// Remove semicolon since it is not present for nested imports
if !snip.replace(";", "").ends_with(&*name.as_str());
then {
let import = if snip.ends_with(";") {
snip.split_whitespace().take(2).collect::<Vec<_>>().join(" ").replace(";", "")
} else if snip.contains("as") {
snip.split_whitespace().take(1).collect::<Vec<_>>().join(" ")
} else {
snip.clone()
};
span_lint_and_sugg(
cx,
IMPORT_RENAME,
Expand All @@ -79,7 +86,7 @@ impl LateLintPass<'_> for ImportRename {
"try",
format!(
"{} as {}{}",
snip.split_whitespace().take(2).collect::<Vec<_>>().join(" "),
import,
name,
if snip.ends_with(';') { ";" } else { "" },
),
Expand Down
9 changes: 5 additions & 4 deletions tests/ui-toml/toml_import_rename/clippy.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import-renames = [
{ path = "syn::Path", rename = "SynPath" },
{ path = "serde::Deserializer", rename = "SomethingElse" },
{ path = "serde::Serializer", rename = "DarkTower" },
{ path = "std::option::Option", rename = "Maybe" },
{ path = "std::process::Child", rename = "Kid" },
{ path = "std::process::exit", rename = "goodbye" },
{ path = "std::collections::BTreeMap", rename = "Map" },
{ path = "regex::bytes", rename = "foo" },
{ path = "std::clone", rename = "foo" },
{ path = "std::thread::sleep", rename = "thread_sleep" },
{ path = "std::any::type_name", rename = "ident" }
]
16 changes: 7 additions & 9 deletions tests/ui-toml/toml_import_rename/conf_import_rename.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#![warn(clippy::import_rename)]

extern crate regex;
extern crate serde;
extern crate syn;

use std::alloc as colla;
use std::option::Option as Maybe;
use std::process::{exit as wrong_exit, Child as Kid};
use std::thread::sleep;

use regex::bytes as xegersetyb;
use serde::{de::Deserializer as WrongRename, ser::Serializer as DarkTower};
use syn::Path as SynPath;
use syn::TypePath;
use std::{
any::{type_name, Any},
clone,
};

fn main() {
use std::collections::BTreeMap as OopsWrongRename;
Expand Down
28 changes: 17 additions & 11 deletions tests/ui-toml/toml_import_rename/conf_import_rename.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
error: this import should be renamed
--> $DIR/conf_import_rename.rs:7:1
--> $DIR/conf_import_rename.rs:5:20
|
LL | use std::thread::sleep;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `use std::thread::sleep; as thread_sleep;`
LL | use std::process::{exit as wrong_exit, Child as Kid};
| ^^^^^^^^^^^^^^^^^^ help: try: `exit as goodbye`
|
= note: `-D clippy::import-rename` implied by `-D warnings`

error: this import should be renamed
--> $DIR/conf_import_rename.rs:9:1
--> $DIR/conf_import_rename.rs:6:1
|
LL | use std::thread::sleep;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `use std::thread::sleep as thread_sleep;`

error: this import should be renamed
--> $DIR/conf_import_rename.rs:8:11
|
LL | use regex::bytes as xegersetyb;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `use regex::bytes as foo;`
LL | any::{type_name, Any},
| ^^^^^^^^^ help: try: `type_name as ident`

error: this import should be renamed
--> $DIR/conf_import_rename.rs:10:13
--> $DIR/conf_import_rename.rs:9:5
|
LL | use serde::{de::Deserializer as WrongRename, ser::Serializer as DarkTower};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `de::Deserializer as as SomethingElse`
LL | clone,
| ^^^^^ help: try: `clone as foo`

error: this import should be renamed
--> $DIR/conf_import_rename.rs:15:5
--> $DIR/conf_import_rename.rs:13:5
|
LL | use std::collections::BTreeMap as OopsWrongRename;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `use std::collections::BTreeMap as Map;`

error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

0 comments on commit 7aeb626

Please sign in to comment.