X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fmisc_early.rs;h=7f70de01daaacbbd7970640e29a4cbf981c421b5;hb=e5a5b0a0774625eebbe7b29c67b49dc6431544d1;hp=d991d499764c87f590a9149d7e3e640060055f51;hpb=99be5221bcb958dd8532ac34ba9166d8c784a341;p=rust.git diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index d991d499764..7f70de01daa 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -1,11 +1,13 @@ use crate::utils::{ - constants, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, + constants, snippet_opt, snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg, + span_lint_and_then, }; use if_chain::if_chain; +use rustc::declare_lint_pass; use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass}; -use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::Applicability; +use rustc_session::declare_tool_lint; use syntax::ast::*; use syntax::source_map::Span; use syntax::visit::{walk_expr, FnKind, Visitor}; @@ -199,6 +201,10 @@ /// **What it does:** Checks for tuple patterns with a wildcard /// pattern (`_`) is next to a rest pattern (`..`). /// + /// _NOTE_: While `_, ..` means there is at least one element left, `..` + /// means there are 0 or more elements left. This can make a difference + /// when refactoring, but shouldn't result in errors in the refactored code, + /// since the wildcard pattern isn't used anyway. /// **Why is this bad?** The wildcard pattern is unneeded as the rest pattern /// can match that element as well. /// @@ -248,6 +254,7 @@ struct ReturnVisitor { } impl ReturnVisitor { + #[must_use] fn new() -> Self { Self { found_return: false } } @@ -255,9 +262,9 @@ fn new() -> Self { impl<'ast> Visitor<'ast> for ReturnVisitor { fn visit_expr(&mut self, ex: &'ast Expr) { - if let ExprKind::Ret(_) = ex.node { + if let ExprKind::Ret(_) = ex.kind { self.found_return = true; - } else if let ExprKind::Try(_) = ex.node { + } else if let ExprKind::Try(_) = ex.kind { self.found_return = true; } @@ -283,7 +290,7 @@ fn check_generics(&mut self, cx: &EarlyContext<'_>, gen: &Generics) { } fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { - if let PatKind::Struct(ref npat, ref pfields, _) = pat.node { + if let PatKind::Struct(ref npat, ref pfields, _) = pat.kind { let mut wilds = 0; let type_name = npat .segments @@ -293,7 +300,7 @@ fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { .name; for field in pfields { - if let PatKind::Wild = field.pat.node { + if let PatKind::Wild = field.pat.kind { wilds += 1; } } @@ -311,7 +318,7 @@ fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { let mut normal = vec![]; for field in pfields { - match field.pat.node { + match field.pat.kind { PatKind::Wild => {}, _ => { if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) { @@ -321,7 +328,7 @@ fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { } } for field in pfields { - if let PatKind::Wild = field.pat.node { + if let PatKind::Wild = field.pat.kind { wilds -= 1; if wilds > 0 { span_lint( @@ -345,8 +352,8 @@ fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { } } - if let PatKind::Ident(_, ident, Some(ref right)) = pat.node { - if let PatKind::Wild = right.node { + if let PatKind::Ident(_, ident, Some(ref right)) = pat.kind { + if let PatKind::Wild = right.kind { span_lint_and_sugg( cx, REDUNDANT_PATTERN, @@ -369,7 +376,7 @@ fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: S let mut registered_names: FxHashMap = FxHashMap::default(); for arg in &decl.inputs { - if let PatKind::Ident(_, ident, None) = arg.pat.node { + if let PatKind::Ident(_, ident, None) = arg.pat.kind { let arg_name = ident.to_string(); if arg_name.starts_with('_') { @@ -396,10 +403,10 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { if in_external_macro(cx.sess(), expr.span) { return; } - match expr.node { + match expr.kind { ExprKind::Call(ref paren, _) => { - if let ExprKind::Paren(ref closure) = paren.node { - if let ExprKind::Closure(_, _, _, ref decl, ref block, _) = closure.node { + if let ExprKind::Paren(ref closure) = paren.kind { + if let ExprKind::Closure(_, _, _, ref decl, ref block, _) = closure.kind { let mut visitor = ReturnVisitor::new(); visitor.visit_expr(block); if !visitor.found_return { @@ -410,13 +417,10 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { "Try not to call a closure in the expression where it is declared.", |db| { if decl.inputs.is_empty() { - let hint = snippet(cx, block.span, "..").into_owned(); - db.span_suggestion( - expr.span, - "Try doing something like: ", - hint, - Applicability::MachineApplicable, // snippet - ); + let mut app = Applicability::MachineApplicable; + let hint = + snippet_with_applicability(cx, block.span, "..", &mut app).into_owned(); + db.span_suggestion(expr.span, "Try doing something like: ", hint, app); } }, ); @@ -425,7 +429,7 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { } }, ExprKind::Unary(UnOp::Neg, ref inner) => { - if let ExprKind::Unary(UnOp::Neg, _) = inner.node { + if let ExprKind::Unary(UnOp::Neg, _) = inner.kind { span_lint( cx, DOUBLE_NEG, @@ -434,7 +438,7 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { ); } }, - ExprKind::Lit(ref lit) => self.check_lit(cx, lit), + ExprKind::Lit(ref lit) => Self::check_lit(cx, lit), _ => (), } } @@ -442,14 +446,14 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) { for w in block.stmts.windows(2) { if_chain! { - if let StmtKind::Local(ref local) = w[0].node; + if let StmtKind::Local(ref local) = w[0].kind; if let Option::Some(ref t) = local.init; - if let ExprKind::Closure(..) = t.node; - if let PatKind::Ident(_, ident, _) = local.pat.node; - if let StmtKind::Semi(ref second) = w[1].node; - if let ExprKind::Assign(_, ref call) = second.node; - if let ExprKind::Call(ref closure, _) = call.node; - if let ExprKind::Path(_, ref path) = closure.node; + if let ExprKind::Closure(..) = t.kind; + if let PatKind::Ident(_, ident, _) = local.pat.kind; + if let StmtKind::Semi(ref second) = w[1].kind; + if let ExprKind::Assign(_, ref call) = second.kind; + if let ExprKind::Call(ref closure, _) = call.kind; + if let ExprKind::Path(_, ref path) = closure.kind; then { if ident == path.segments[0].ident { span_lint( @@ -466,7 +470,7 @@ fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) { } impl MiscEarlyLints { - fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) { + fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) { // We test if first character in snippet is a number, because the snippet could be an expansion // from a built-in macro like `line!()` or a proc-macro like `#[wasm_bindgen]`. // Note that this check also covers special case that `line!()` is eagerly expanded by compiler. @@ -477,10 +481,10 @@ fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) { _ => return, }; - if let LitKind::Int(value, lit_int_type) = lit.node { + if let LitKind::Int(value, lit_int_type) = lit.kind { let suffix = match lit_int_type { - LitIntType::Signed(ty) => ty.ty_to_string(), - LitIntType::Unsigned(ty) => ty.ty_to_string(), + LitIntType::Signed(ty) => ty.name_str(), + LitIntType::Unsigned(ty) => ty.name_str(), LitIntType::Unsuffixed => "", }; @@ -540,8 +544,8 @@ fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) { }, ); } - } else if let LitKind::Float(_, float_ty) = lit.node { - let suffix = float_ty.ty_to_string(); + } else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind { + let suffix = float_ty.name_str(); let maybe_last_sep_idx = lit_snip.len() - suffix.len() - 1; if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' { span_lint_and_sugg( @@ -559,7 +563,7 @@ fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) { } fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) { - if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.node { + if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.kind { fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) { span_lint_and_sugg( cx, @@ -578,7 +582,7 @@ fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) { #[allow(clippy::trivially_copy_pass_by_ref)] fn is_wild>(pat: &&P) -> bool { - if let PatKind::Wild = pat.node { + if let PatKind::Wild = pat.kind { true } else { false