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};
declare_clippy_lint! {
/// **What it does:** Checks for tuple patterns with a wildcard
- /// pattern (`_`) is next to a rest pattern (`..`) pattern.
+ /// 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.
///
/// ```
pub UNNEEDED_WILDCARD_PATTERN,
complexity,
- "tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`) pattern"
+ "tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`)"
}
declare_lint_pass!(MiscEarlyLints => [
}
impl ReturnVisitor {
+ #[must_use]
fn new() -> Self {
Self { found_return: false }
}
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;
}
}
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
.name;
for field in pfields {
- if let PatKind::Wild = field.pat.node {
+ if let PatKind::Wild = field.pat.kind {
wilds += 1;
}
}
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) {
}
}
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(
}
}
- 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,
let mut registered_names: FxHashMap<String, Span> = 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('_') {
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 {
"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);
}
},
);
}
},
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,
);
}
},
- ExprKind::Lit(ref lit) => self.check_lit(cx, lit),
+ ExprKind::Lit(ref lit) => Self::check_lit(cx, lit),
_ => (),
}
}
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(
}
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.
_ => 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 => "",
};
},
);
}
- } 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(
}
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,
);
}
- fn is_rest<P: std::ops::Deref<Target = Pat>>(pat: &P) -> bool {
- if let PatKind::Rest = pat.node {
- true
- } else {
- false
- }
- }
-
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_wild<P: std::ops::Deref<Target = Pat>>(pat: &&P) -> bool {
- if let PatKind::Wild = pat.node {
+ if let PatKind::Wild = pat.kind {
true
} else {
false
}
}
- if let Some(rest_index) = patterns.iter().position(is_rest) {
+ if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) {
if let Some((left_index, left_pat)) = patterns[..rest_index]
.iter()
.rev()