-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add import_rename lint, this adds a field on the Conf struct
- Loading branch information
Showing
8 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_opt}; | ||
|
||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{def::Res, def_id::DefId, Crate, Item, ItemKind, UseKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_span::Symbol; | ||
|
||
use crate::utils::conf::Rename; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks that any import matching `import_renames` is renamed. | ||
/// | ||
/// **Why is this bad?** This is bad for some items because their defined names are | ||
/// too vauge outside of their defining scope. | ||
/// | ||
/// **Known problems:** None | ||
/// | ||
/// **Example:** | ||
/// | ||
/// An example clippy.toml configuration: | ||
/// ```toml | ||
/// # clippy.toml | ||
/// import-renames = [ {path = "serde_json::Value", rename = "JsonValue" }] | ||
/// ``` | ||
/// | ||
/// ```rust,ignore | ||
/// use serde_json::Value; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust,ignore | ||
/// use serde_json::Value as JsonValue; | ||
/// ``` | ||
pub IMPORT_RENAME, | ||
restriction, | ||
"enforce import renames" | ||
} | ||
|
||
pub struct ImportRename { | ||
conf_renames: Vec<Rename>, | ||
renames: FxHashMap<DefId, Symbol>, | ||
} | ||
|
||
impl ImportRename { | ||
pub fn new(conf_renames: Vec<Rename>) -> Self { | ||
Self { | ||
conf_renames, | ||
renames: FxHashMap::default(), | ||
} | ||
} | ||
} | ||
|
||
impl_lint_pass!(ImportRename => [IMPORT_RENAME]); | ||
|
||
impl LateLintPass<'_> for ImportRename { | ||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { | ||
for Rename { path, rename } in &self.conf_renames { | ||
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &path.split("::").collect::<Vec<_>>()) { | ||
self.renames.insert(id, Symbol::intern(rename)); | ||
} | ||
} | ||
} | ||
|
||
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { | ||
if_chain! { | ||
if let ItemKind::Use(path, UseKind::Single) = &item.kind; | ||
if let Res::Def(_, id) = path.res; | ||
if let Some(snip) = snippet_opt(cx, item.span); | ||
if let Some(name) = self.renames.get(&id); | ||
// 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, | ||
item.span, | ||
"this import should be renamed", | ||
"try", | ||
format!( | ||
"{} as {}{}", | ||
import, | ||
name, | ||
if snip.ends_with(';') { ";" } else { "" }, | ||
), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import-renames = [ | ||
{ 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 = "std::clone", rename = "foo" }, | ||
{ path = "std::thread::sleep", rename = "thread_sleep" }, | ||
{ path = "std::any::type_name", rename = "ident" } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![warn(clippy::import_rename)] | ||
|
||
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 std::{ | ||
any::{type_name, Any}, | ||
clone, | ||
}; | ||
|
||
fn main() { | ||
use std::collections::BTreeMap as OopsWrongRename; | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/ui-toml/toml_import_rename/conf_import_rename.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
error: this import should be renamed | ||
--> $DIR/conf_import_rename.rs:5:20 | ||
| | ||
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: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 | any::{type_name, Any}, | ||
| ^^^^^^^^^ help: try: `type_name as ident` | ||
|
||
error: this import should be renamed | ||
--> $DIR/conf_import_rename.rs:9:5 | ||
| | ||
LL | clone, | ||
| ^^^^^ help: try: `clone as foo` | ||
|
||
error: this import should be renamed | ||
--> $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 5 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `third-party` at line 5 column 1 | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `import-renames`, `third-party` at line 5 column 1 | ||
|
||
error: aborting due to previous error | ||
|