-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
281 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
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,177 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::in_macro; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::def::{DefKind, Res}; | ||
use rustc_hir::{Item, ItemKind, Mod, Node, Path, UseKind, VisibilityKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::def_id::DefId; | ||
use rustc_span::symbol::Ident; | ||
use rustc_span::{BytePos, Span}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for imports ending in `::{self};`. | ||
/// | ||
/// **Why is this bad?** In most cases, this can be written much more cleanly by omitting `self`. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// use std::io::{self}; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// use std::io; | ||
/// ``` | ||
pub UNNECESSARY_SELF_IMPORTS, | ||
style, | ||
"imports ending in `self`, which can be omitted" | ||
} | ||
|
||
declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for UnnecessarySelfImports { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { | ||
if_chain! { | ||
if !in_macro(item.span); | ||
if let ItemKind::Use(path, UseKind::ListStem) = &item.kind; | ||
if let Some(ident) = is_self_import(cx, item, path); | ||
if let Some(Res::Def(DefKind::Mod, def_id)) = path.segments[path.segments.len() - 2].res; | ||
if let Some(last) = path.segments.last(); | ||
if !mod_contains_item(cx, def_id, last.ident) || !item_is_in_scope(cx, ident, path.span); | ||
then { | ||
let mut applicability = Applicability::MachineApplicable; | ||
let snippet = if ident == path.segments[path.segments.len() - 1].ident { | ||
let adjusted_span = path.span.with_hi(path.span.hi() - BytePos(8)); | ||
format!( | ||
"{}", | ||
snippet_with_applicability(cx, adjusted_span, "..", &mut applicability) | ||
) | ||
} else { | ||
let adjusted_span = path.span.until(ident.span); | ||
let adjusted_span = adjusted_span.with_hi(adjusted_span.hi() - BytePos(11)); | ||
format!( | ||
"{} as {}", | ||
snippet_with_applicability(cx, adjusted_span, "..", &mut applicability), | ||
snippet_with_applicability(cx, ident.span, "..", &mut applicability) | ||
) | ||
}; | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_SELF_IMPORTS, | ||
path.span, | ||
"import ending with `self`", | ||
"consider omitting `::{self}`", | ||
snippet, | ||
applicability, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn is_self_import(cx: &LateContext<'_>, current_import: &Item<'_>, current_import_path: &Path<'_>) -> Option<Ident> { | ||
let definitions = cx.tcx.hir().definitions(); | ||
let mut amt = 0; | ||
let mut ident: Option<Ident> = None; | ||
|
||
for def in definitions.iter_local_def_id() { | ||
if_chain! { | ||
if let Some(Node::Item(item)) = cx.tcx.hir().get_if_local(def.to_def_id()); | ||
if let ItemKind::Use(path, kind) = item.kind; | ||
if matches!(kind, UseKind::Single | UseKind::Glob); | ||
if current_import.span.contains(item.span); | ||
then { | ||
amt += 1; | ||
if amt > 1 { return None; } | ||
|
||
if_chain! { | ||
if current_import_path.segments.len() == path.segments.len(); | ||
let current_import_last = ¤t_import_path.segments[current_import_path.segments.len() - 1]; | ||
let item_last = &path.segments[path.segments.len() - 1]; | ||
if current_import_last.ident == item_last.ident; | ||
then { | ||
ident = Some(item.ident); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
ident | ||
} | ||
|
||
fn mod_contains_item(cx: &LateContext<'_>, def_id: DefId, ident: Ident) -> bool { | ||
if let Some(Node::Item(node)) = cx.tcx.hir().get_if_local(def_id) { | ||
if let ItemKind::Mod(Mod { item_ids, .. }) = &node.kind { | ||
for item in item_ids.iter() { | ||
if_chain! { | ||
if let Some(Node::Item(node)) = cx.tcx.hir().get_if_local(item.def_id.to_def_id()); | ||
if let VisibilityKind::Public = node.vis.node; | ||
if node.ident == ident; | ||
if matches!(node.kind, ItemKind::Fn(..) | ItemKind::Const(..) | ItemKind::Static(..)); | ||
then { return true; } | ||
|
||
} | ||
} | ||
} | ||
} else { | ||
for item in cx.tcx.item_children(def_id).iter() { | ||
if_chain! { | ||
if item.ident == ident; | ||
if matches!(item.res, Res::Def(DefKind::Fn | DefKind::Const | DefKind::Static, _)); | ||
then { return true; } | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|
||
fn item_is_in_scope(cx: &LateContext<'_>, ident: Ident, current_import: Span) -> bool { | ||
let definitions = cx.tcx.hir().definitions(); | ||
let mut outer_mod: Option<Span> = None; | ||
|
||
for def in definitions.iter_local_def_id() { | ||
if let Some(Node::Item(item)) = cx.tcx.hir().get_if_local(def.to_def_id()) { | ||
let should_check = match outer_mod { | ||
Some(curr_mod) => { | ||
if curr_mod.contains(item.span) { | ||
false | ||
} else { | ||
outer_mod = None; | ||
true | ||
} | ||
}, | ||
None => true, | ||
}; | ||
|
||
if should_check { | ||
match item.kind { | ||
ItemKind::Fn(..) | ItemKind::Const(..) | ItemKind::Static(..) => { | ||
if item.ident == ident { | ||
return true; | ||
} | ||
}, | ||
ItemKind::Use(path, kind) => { | ||
if_chain! { | ||
if let UseKind::Single = kind; | ||
if !current_import.contains(path.span); | ||
if item.ident == ident; | ||
|
||
then { | ||
return true; | ||
} | ||
} | ||
}, | ||
_ => { | ||
outer_mod = Some(item.span); | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
false | ||
} |
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,38 @@ | ||
// run-rustfix | ||
#![warn(clippy::unnecessary_self_imports)] | ||
#![allow(non_camel_case_types, non_upper_case_globals, unused_imports, dead_code)] | ||
|
||
mod a { | ||
pub mod mod_and_fn_in_scope {} | ||
pub fn mod_and_fn_in_scope() {} | ||
|
||
pub mod only_module {} | ||
|
||
pub mod mod_and_fn {} | ||
pub fn mod_and_fn() {} | ||
|
||
pub enum enum_and_const {} | ||
pub const enum_and_const: u32 = 1; | ||
|
||
pub struct struct_and_static {} | ||
pub static struct_and_static: u32 = 1; | ||
} | ||
|
||
mod b { | ||
pub static struct_and_static: u32 = 2; | ||
} | ||
|
||
fn mod_and_fn_in_scope() {} | ||
const enum_and_const: u32 = 2; | ||
|
||
use std::io; | ||
|
||
use a::enum_and_const as alias; | ||
use a::mod_and_fn::{self, *}; | ||
use a::mod_and_fn_in_scope::{self}; | ||
use a::only_module; | ||
use a::struct_and_static::{self}; | ||
|
||
use b::struct_and_static; | ||
|
||
fn main() {} |
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,38 @@ | ||
// run-rustfix | ||
#![warn(clippy::unnecessary_self_imports)] | ||
#![allow(non_camel_case_types, non_upper_case_globals, unused_imports, dead_code)] | ||
|
||
mod a { | ||
pub mod mod_and_fn_in_scope {} | ||
pub fn mod_and_fn_in_scope() {} | ||
|
||
pub mod only_module {} | ||
|
||
pub mod mod_and_fn {} | ||
pub fn mod_and_fn() {} | ||
|
||
pub enum enum_and_const {} | ||
pub const enum_and_const: u32 = 1; | ||
|
||
pub struct struct_and_static {} | ||
pub static struct_and_static: u32 = 1; | ||
} | ||
|
||
mod b { | ||
pub static struct_and_static: u32 = 2; | ||
} | ||
|
||
fn mod_and_fn_in_scope() {} | ||
const enum_and_const: u32 = 2; | ||
|
||
use std::io::{self}; | ||
|
||
use a::enum_and_const::{self as alias}; | ||
use a::mod_and_fn::{self, *}; | ||
use a::mod_and_fn_in_scope::{self}; | ||
use a::only_module::{self}; | ||
use a::struct_and_static::{self}; | ||
|
||
use b::struct_and_static; | ||
|
||
fn main() {} |
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,22 @@ | ||
error: import ending with `self` | ||
--> $DIR/unnecessary_self_imports.rs:28:5 | ||
| | ||
LL | use std::io::{self}; | ||
| ^^^^^^^^^^^^^^^ help: consider omitting `::{self}`: `std::io` | ||
| | ||
= note: `-D clippy::unnecessary-self-imports` implied by `-D warnings` | ||
|
||
error: import ending with `self` | ||
--> $DIR/unnecessary_self_imports.rs:30:5 | ||
| | ||
LL | use a::enum_and_const::{self as alias}; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider omitting `::{self}`: `a::enum_and_const as alias` | ||
|
||
error: import ending with `self` | ||
--> $DIR/unnecessary_self_imports.rs:33:5 | ||
| | ||
LL | use a::only_module::{self}; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider omitting `::{self}`: `a::only_module` | ||
|
||
error: aborting due to 3 previous errors | ||
|