Skip to content

Commit

Permalink
Auto merge of #5029 - flip1995:wildcard_imports, r=phansch
Browse files Browse the repository at this point in the history
New Lint: `wildcard imports`

Fixes #1228

### A few notes:
- I put this lint in the pedantic group, even though in the issue restriction was mentioned.
- Every fallout fix was automatically applied by `cargo fix` (This produced 3 `unused_imports` warnings) and are in commit 7e834c8. So reverting these changes wouldn't be a problem.

### A few ideas:
- A configuration to specify the amount of imported Items, where a `*` might be better.
- ~~A configuration to disable the lint for enums. Or just disable the lint for enums, since there is [`enum_glob_use`](https://rust-lang.github.io/rust-clippy/master/index.html#enum_glob_use)~~ I moved `enum_glob_use` into this lint in 12937f0

### A few quotes from the issue:
> Is there a way to ask the compiler about the modules or symbols that the current file is using?

Yes there is. I found it, once I was nearly finished implementing it myself. See 321d64a

> one hard optional feature would be to figure out what is currently used and add a suggestion to replace it with a full import list.

Yeah that was pretty hard, until I found the query for this. Queries are cool, but too hard to find.

> FWIW VS Code and Intellij IDEA both offer imports deglobbing which replace * with required imports.

And now, Clippy can do this too! 🎉

---

Your thoughts on the notes/ideas?

changelog: Add new lint [`wildcard imports`]. Add suggestion to [`enum_glob_use`]
  • Loading branch information
bors committed Feb 21, 2020
2 parents acfcbee + 4dd2252 commit e342047
Show file tree
Hide file tree
Showing 130 changed files with 888 additions and 250 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,7 @@ Released 2018-09-13
[`while_let_on_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator
[`wildcard_dependencies`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_dependencies
[`wildcard_enum_match_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_enum_match_arm
[`wildcard_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_imports
[`wildcard_in_or_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_in_or_patterns
[`write_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_literal
[`write_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_with_newline
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.

[There are 356 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are 357 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)

We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:

Expand Down
5 changes: 4 additions & 1 deletion clippy_dev/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![cfg_attr(feature = "deny-warnings", deny(warnings))]

use clap::{App, Arg, SubCommand};
use clippy_dev::*;
use clippy_dev::{
gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
replace_region_in_file, Lint, DOCS_LINK,
};
use std::path::Path;

mod fmt;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/approx_const.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils::span_lint;
use rustc_hir::*;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/as_conversions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc::lint::in_external_macro;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use syntax::ast::*;
use syntax::ast::{Expr, ExprKind};

use crate::utils::span_lint_and_help;

Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/assertions_on_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::consts::{constant, Constant};
use crate::utils::paths;
use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, snippet_opt, span_lint_and_help};
use if_chain::if_chain;
use rustc_hir::*;
use rustc_hir::{Expr, ExprKind, PatKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use syntax::ast::LitKind;
Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ fn lint_misrefactored_assign_op(

#[must_use]
fn is_commutative(op: hir::BinOpKind) -> bool {
use rustc_hir::BinOpKind::*;
use rustc_hir::BinOpKind::{
Add, And, BitAnd, BitOr, BitXor, Div, Eq, Ge, Gt, Le, Lt, Mul, Ne, Or, Rem, Shl, Shr, Sub,
};
match op {
Add | Mul | And | Or | BitXor | BitAnd | BitOr | Eq | Ne => true,
Sub | Div | Rem | Shl | Shr | Lt | Le | Ge | Gt => false,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/atomic_ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::utils::{match_def_path, span_lint_and_help};
use if_chain::if_chain;
use rustc::ty;
use rustc_hir::def_id::DefId;
use rustc_hir::*;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

Expand Down
6 changes: 4 additions & 2 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! checks for attributes
use crate::reexport::*;
use crate::reexport::Name;
use crate::utils::{
first_line_of_span, is_present_in_source, match_def_path, paths, snippet_opt, span_lint, span_lint_and_sugg,
span_lint_and_then, without_block_comments,
Expand All @@ -9,7 +9,9 @@ use if_chain::if_chain;
use rustc::lint::in_external_macro;
use rustc::ty;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{
Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, StmtKind, TraitItem, TraitItemKind, TraitMethod,
};
use rustc_lint::{CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/bit_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::utils::sugg::Sugg;
use crate::utils::{span_lint, span_lint_and_then};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Span;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/blacklisted_name.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::span_lint;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::*;
use rustc_hir::{Pat, PatKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/block_in_if_condition.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::utils::*;
use crate::utils::{differing_macro_contexts, higher, snippet_block_with_applicability, span_lint, span_lint_and_sugg};
use matches::matches;
use rustc::hir::map::Map;
use rustc::lint::in_external_macro;
use rustc_errors::Applicability;
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use rustc_hir::*;
use rustc_hir::{BlockCheckMode, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};

Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::utils::{
use if_chain::if_chain;
use rustc::hir::map::Map;
use rustc_errors::Applicability;
use rustc_hir::intravisit::*;
use rustc_hir::*;
use rustc_hir::intravisit::{walk_expr, FnKind, NestedVisitorMap, Visitor};
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
Expand Down Expand Up @@ -161,7 +161,7 @@ struct SuggestContext<'a, 'tcx, 'v> {

impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
use quine_mc_cluskey::Bool::*;
use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
match suggestion {
True => {
self.output.push_str("true");
Expand Down Expand Up @@ -277,7 +277,7 @@ fn suggest(cx: &LateContext<'_, '_>, suggestion: &Bool, terminals: &[&Expr<'_>])
}

fn simple_negate(b: Bool) -> Bool {
use quine_mc_cluskey::Bool::*;
use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
match b {
True => False,
False => True,
Expand Down Expand Up @@ -325,7 +325,7 @@ fn terminal_stats(b: &Bool) -> Stats {
&Term(n) => stats.terminals[n as usize] += 1,
}
}
use quine_mc_cluskey::Bool::*;
use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
let mut stats = Stats::default();
recurse(b, &mut stats);
stats
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/bytecount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::utils::{
use if_chain::if_chain;
use rustc::ty;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use syntax::ast::{Name, UintTy};
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/cargo_common_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::utils::span_lint;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::DUMMY_SP;
use syntax::ast::*;
use syntax::ast::Crate;

declare_clippy_lint! {
/// **What it does:** Checks to see if all common metadata is defined in
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/checked_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use if_chain::if_chain;
use rustc::lint::in_external_macro;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, QPath, TyKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use syntax::ast::LitKind;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/cognitive_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use rustc::hir::map::Map;
use rustc_hir::intravisit::{walk_expr, FnKind, NestedVisitorMap, Visitor};
use rustc_hir::*;
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Span;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/comparison_chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::utils::{
get_trait_def_id, if_sequence, implements_trait, parent_node_is_if_expr, paths, span_lint_and_help, SpanlessEq,
};
use rustc_hir::*;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

Expand Down
10 changes: 4 additions & 6 deletions clippy_lints/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use rustc::ty::{self, Ty, TyCtxt};
use rustc::{bug, span_bug};
use rustc_data_structures::sync::Lrc;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::*;
use rustc_hir::{BinOp, BinOpKind, Block, Expr, ExprKind, HirId, QPath, UnOp};
use rustc_lint::LateContext;
use rustc_span::symbol::Symbol;
use std::cmp::Ordering::{self, Equal};
use std::convert::TryInto;
use std::hash::{Hash, Hasher};
use syntax::ast::{FloatTy, LitKind};
use syntax::ast::{FloatTy, LitFloatType, LitKind};

/// A `LitKind`-like enum to fold constant `Expr`s into.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -152,8 +152,6 @@ impl Constant {

/// Parses a `LitKind` to a `Constant`.
pub fn lit_to_constant(lit: &LitKind, ty: Option<Ty<'_>>) -> Constant {
use syntax::ast::*;

match *lit {
LitKind::Str(ref is, _) => Constant::Str(is.to_string()),
LitKind::Byte(b) => Constant::Int(u128::from(b)),
Expand Down Expand Up @@ -277,7 +275,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {

#[allow(clippy::cast_possible_wrap)]
fn constant_not(&self, o: &Constant, ty: Ty<'_>) -> Option<Constant> {
use self::Constant::*;
use self::Constant::{Bool, Int};
match *o {
Bool(b) => Some(Bool(!b)),
Int(value) => {
Expand All @@ -293,7 +291,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
}

fn constant_negate(&self, o: &Constant, ty: Ty<'_>) -> Option<Constant> {
use self::Constant::*;
use self::Constant::{Int, F32, F64};
match *o {
Int(value) => {
let ity = match ty.kind {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::utils::{get_parent_expr, higher, if_sequence, same_tys, snippet, span
use crate::utils::{SpanlessEq, SpanlessHash};
use rustc::ty::Ty;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::*;
use rustc_hir::{Arm, Block, Expr, ExprKind, MatchSource, Pat, PatKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::Symbol;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/default_trait_access.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use if_chain::if_chain;
use rustc::ty;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::utils::paths;
use crate::utils::{is_automatically_derived, is_copy, match_path, span_lint_and_then};
use if_chain::if_chain;
use rustc::ty::{self, Ty};
use rustc_hir::*;
use rustc_hir::{Item, ItemKind, TraitRef};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
Expand Down
8 changes: 5 additions & 3 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ fn check_attrs<'a>(cx: &LateContext<'_, '_>, valid_idents: &FxHashSet<String>, a
let parser = pulldown_cmark::Parser::new(&doc).into_offset_iter();
// Iterate over all `Events` and combine consecutive events into one
let events = parser.coalesce(|previous, current| {
use pulldown_cmark::Event::*;
use pulldown_cmark::Event::Text;

let previous_range = previous.1;
let current_range = current.1;
Expand All @@ -374,8 +374,10 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
spans: &[(usize, Span)],
) -> DocHeaders {
// true if a safety header was found
use pulldown_cmark::Event::*;
use pulldown_cmark::Tag::*;
use pulldown_cmark::Event::{
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
};
use pulldown_cmark::Tag::{CodeBlock, Heading, Link};

let mut headers = DocHeaders {
safety: false,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/double_comparison.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Lint on unnecessary double comparisons. Some examples:
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/double_parens.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::utils::span_lint;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use syntax::ast::*;
use syntax::ast::{Expr, ExprKind};

declare_clippy_lint! {
/// **What it does:** Checks for unnecessary double parentheses.
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/drop_bounds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::{match_def_path, paths, span_lint};
use if_chain::if_chain;
use rustc_hir::*;
use rustc_hir::{GenericBound, GenericParam, WhereBoundPredicate, WherePredicate};
use rustc_lint::LateLintPass;
use rustc_session::{declare_lint_pass, declare_tool_lint};

Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/drop_forget_ref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::utils::{is_copy, match_def_path, paths, qpath_res, span_lint_and_note};
use if_chain::if_chain;
use rustc::ty;
use rustc_hir::*;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/duration_subsec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Spanned;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/else_if_without_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use rustc::lint::in_external_macro;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use syntax::ast::*;
use syntax::ast::{Expr, ExprKind};

use crate::utils::span_lint_and_help;

Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/empty_enum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! lint when there is an enum with no variants
use crate::utils::span_lint_and_then;
use rustc_hir::*;
use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use if_chain::if_chain;
use rustc::hir::map::Map;
use rustc_errors::Applicability;
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use rustc_hir::*;
use rustc_hir::{BorrowKind, Expr, ExprKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/enum_clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::consts::{miri_to_const, Constant};
use crate::utils::span_lint;
use rustc::ty;
use rustc::ty::util::IntTypeExt;
use rustc_hir::*;
use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use std::convert::TryFrom;
Expand Down
Loading

0 comments on commit e342047

Please sign in to comment.