From f4b80394d7b2fd24b3d3a1a3eeea2e22ddc8974b Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 18 Feb 2020 20:50:10 +0900 Subject: [PATCH 1/2] Use `Vec::with_capacity()` as possible --- clippy_lints/src/booleans.rs | 2 +- clippy_lints/src/matches.rs | 2 +- clippy_lints/src/shadow.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 60e62542a725..ee4ab7a9701d 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -359,7 +359,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> { } simplified.push(simple_negated); } - let mut improvements = Vec::new(); + let mut improvements = Vec::with_capacity(simplified.len()); 'simplified: for suggestion in &simplified { let simplified_stats = terminal_stats(suggestion); let mut improvement = false; diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index d753b664045d..72131084311a 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -724,7 +724,7 @@ fn is_panic_block(block: &Block<'_>) -> bool { fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) { if has_only_ref_pats(arms) { - let mut suggs = Vec::new(); + let mut suggs = Vec::with_capacity(arms.len() + 1); let (title, msg) = if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, ref inner) = ex.kind { let span = ex.span.source_callsite(); suggs.push((span, Sugg::hir_with_macro_callsite(cx, inner, "..").to_string())); diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index 28e3acdf6bea..8e2ede04476e 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -101,7 +101,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Shadow { } fn check_fn<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx FnDecl<'_>, body: &'tcx Body<'_>) { - let mut bindings = Vec::new(); + let mut bindings = Vec::with_capacity(decl.inputs.len()); for arg in iter_input_pats(decl, body) { if let PatKind::Binding(.., ident, _) = arg.pat.kind { bindings.push((ident.name, ident.span)) From 06cb96ed72e09b9f850ee3e08e50df0c51e02250 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 18 Feb 2020 22:28:18 +0900 Subject: [PATCH 2/2] Clean up imports --- clippy_lints/src/booleans.rs | 3 +-- clippy_lints/src/consts.rs | 1 - clippy_lints/src/escape.rs | 5 ++--- clippy_lints/src/indexing_slicing.rs | 14 ++++++-------- clippy_lints/src/loops.rs | 1 - clippy_lints/src/ptr.rs | 1 - clippy_lints/src/ptr_offset_with_cast.rs | 10 +++++----- clippy_lints/src/temporary_assignment.rs | 3 +-- clippy_lints/src/utils/internal_lints.rs | 8 +++----- clippy_lints/src/utils/sugg.rs | 3 +-- 10 files changed, 19 insertions(+), 30 deletions(-) diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index ee4ab7a9701d..d37f77cf6cc3 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -5,7 +5,6 @@ use crate::utils::{ use if_chain::if_chain; use rustc::hir::map::Map; use rustc_errors::Applicability; -use rustc_hir::intravisit; use rustc_hir::intravisit::*; use rustc_hir::*; use rustc_lint::{LateContext, LateLintPass}; @@ -60,7 +59,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonminimalBool { fn check_fn( &mut self, cx: &LateContext<'a, 'tcx>, - _: intravisit::FnKind<'tcx>, + _: FnKind<'tcx>, _: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, _: Span, diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index 211625aafbf8..5a79848c1287 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -11,7 +11,6 @@ use rustc_hir::*; use rustc_lint::LateContext; use rustc_span::symbol::Symbol; use std::cmp::Ordering::{self, Equal}; -use std::cmp::PartialOrd; use std::convert::TryInto; use std::hash::{Hash, Hasher}; use syntax::ast::{FloatTy, LitKind}; diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index ec1d3a24f036..73bd7b3a143c 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -1,7 +1,6 @@ use rustc::ty::layout::LayoutOf; use rustc::ty::{self, Ty}; -use rustc_hir::intravisit as visit; -use rustc_hir::HirIdSet; +use rustc_hir::intravisit; use rustc_hir::{self, *}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; @@ -54,7 +53,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal { fn check_fn( &mut self, cx: &LateContext<'a, 'tcx>, - _: visit::FnKind<'tcx>, + _: intravisit::FnKind<'tcx>, _: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, _: Span, diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 30bb09fd8081..ac9442e96078 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -1,9 +1,7 @@ //! lint on indexing and slicing operations use crate::consts::{constant, Constant}; -use crate::utils; -use crate::utils::higher; -use crate::utils::higher::Range; +use crate::utils::{higher, span_lint, span_lint_and_help}; use rustc::ty; use rustc_hir::*; use rustc_lint::{LateContext, LateLintPass}; @@ -100,7 +98,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { if let (Some(start), _) = const_range { if start > size { - utils::span_lint( + span_lint( cx, OUT_OF_BOUNDS_INDEXING, range.start.map_or(expr.span, |start| start.span), @@ -112,7 +110,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { if let (_, Some(end)) = const_range { if end > size { - utils::span_lint( + span_lint( cx, OUT_OF_BOUNDS_INDEXING, range.end.map_or(expr.span, |end| end.span), @@ -136,7 +134,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { (None, None) => return, // [..] is ok. }; - utils::span_lint_and_help(cx, INDEXING_SLICING, expr.span, "slicing may panic.", help_msg); + span_lint_and_help(cx, INDEXING_SLICING, expr.span, "slicing may panic.", help_msg); } else { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind { @@ -147,7 +145,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { } } - utils::span_lint_and_help( + span_lint_and_help( cx, INDEXING_SLICING, expr.span, @@ -163,7 +161,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { /// the range. If the start or end is not constant, None is returned. fn to_const_range<'a, 'tcx>( cx: &LateContext<'a, 'tcx>, - range: Range<'_>, + range: higher::Range<'_>, array_size: u128, ) -> (Option, Option) { let s = range.start.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c)); diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 3eee2728ce2c..393fbd2aaefc 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -18,7 +18,6 @@ use rustc::ty::{self, Ty}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id; use rustc_hir::intravisit::{walk_block, walk_expr, walk_pat, walk_stmt, NestedVisitorMap, Visitor}; use rustc_hir::*; use rustc_infer::infer::TyCtxtInferExt; diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 33cfa5d75c62..d868253b85cd 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -8,7 +8,6 @@ use crate::utils::{ use if_chain::if_chain; use rustc::ty; use rustc_errors::Applicability; -use rustc_hir::QPath; use rustc_hir::*; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 93bd3b57ad7e..ffc59d43750e 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -1,4 +1,4 @@ -use crate::utils; +use crate::utils::{snippet_opt, span_lint, span_lint_and_sugg}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; @@ -59,7 +59,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PtrOffsetWithCast { let msg = format!("use of `{}` with a `usize` casted to an `isize`", method); if let Some(sugg) = build_suggestion(cx, method, receiver_expr, cast_lhs_expr) { - utils::span_lint_and_sugg( + span_lint_and_sugg( cx, PTR_OFFSET_WITH_CAST, expr.span, @@ -69,7 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PtrOffsetWithCast { Applicability::MachineApplicable, ); } else { - utils::span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, &msg); + span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, &msg); } } } @@ -119,8 +119,8 @@ fn build_suggestion<'a, 'tcx>( receiver_expr: &Expr<'_>, cast_lhs_expr: &Expr<'_>, ) -> Option { - let receiver = utils::snippet_opt(cx, receiver_expr.span)?; - let cast_lhs = utils::snippet_opt(cx, cast_lhs_expr.span)?; + let receiver = snippet_opt(cx, receiver_expr.span)?; + let cast_lhs = snippet_opt(cx, cast_lhs_expr.span)?; Some(format!("{}.{}({})", receiver, method.suggestion(), cast_lhs)) } diff --git a/clippy_lints/src/temporary_assignment.rs b/clippy_lints/src/temporary_assignment.rs index fec9ad256336..bbb883aaf328 100644 --- a/clippy_lints/src/temporary_assignment.rs +++ b/clippy_lints/src/temporary_assignment.rs @@ -1,5 +1,4 @@ -use crate::utils::is_adjusted; -use crate::utils::span_lint; +use crate::utils::{is_adjusted, span_lint}; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; diff --git a/clippy_lints/src/utils/internal_lints.rs b/clippy_lints/src/utils/internal_lints.rs index 945999de89fe..759feafc75b7 100644 --- a/clippy_lints/src/utils/internal_lints.rs +++ b/clippy_lints/src/utils/internal_lints.rs @@ -11,12 +11,10 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; use rustc_hir::*; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass}; -use rustc_session::declare_tool_lint; -use rustc_session::{declare_lint_pass, impl_lint_pass}; +use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::{Span, Spanned}; use rustc_span::symbol::SymbolStr; -use syntax::ast; -use syntax::ast::{Crate as AstCrate, ItemKind, LitKind, Name}; +use syntax::ast::{Crate as AstCrate, ItemKind, LitKind, Name, NodeId}; use syntax::visit::FnKind; declare_clippy_lint! { @@ -380,7 +378,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnDataPass { declare_lint_pass!(ProduceIce => [PRODUCE_ICE]); impl EarlyLintPass for ProduceIce { - fn check_fn(&mut self, _: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: ast::NodeId) { + fn check_fn(&mut self, _: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: NodeId) { if is_trigger_fn(fn_kind) { panic!("Would you like some help with that?"); } diff --git a/clippy_lints/src/utils/sugg.rs b/clippy_lints/src/utils/sugg.rs index 3e694f40d193..5c9e3ab80bb5 100644 --- a/clippy_lints/src/utils/sugg.rs +++ b/clippy_lints/src/utils/sugg.rs @@ -12,9 +12,8 @@ use rustc_span::{BytePos, Pos}; use std::borrow::Cow; use std::convert::TryInto; use std::fmt::Display; -use syntax::ast; -use syntax::token; use syntax::util::parser::AssocOp; +use syntax::{ast, token}; pub use crate::literal_representation::format_numeric_literal;