From 9539df06c6c54179a4e703aadb0b95cbe9489679 Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Tue, 1 Aug 2017 17:11:05 +1000 Subject: [PATCH] use BindingAnnotation instead of BindingMode --- clippy_lints/src/let_if_seq.rs | 3 +- clippy_lints/src/matches.rs | 2 +- clippy_lints/src/misc.rs | 65 ++++++++++++---------- clippy_lints/src/needless_borrow.rs | 4 +- clippy_lints/src/needless_borrowed_ref.rs | 4 +- clippy_lints/src/needless_pass_by_value.rs | 5 +- 6 files changed, 43 insertions(+), 40 deletions(-) diff --git a/clippy_lints/src/let_if_seq.rs b/clippy_lints/src/let_if_seq.rs index adda97bbc50..93b981a2b4a 100644 --- a/clippy_lints/src/let_if_seq.rs +++ b/clippy_lints/src/let_if_seq.rs @@ -1,5 +1,6 @@ use rustc::lint::*; use rustc::hir; +use rustc::hir::BindingAnnotation; use syntax_pos::{Span, NO_EXPANSION}; use utils::{snippet, span_lint_and_then}; @@ -94,7 +95,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq { }; let mutability = match mode { - hir::BindByRef(hir::MutMutable) | hir::BindByValue(hir::MutMutable) => " ", + BindingAnnotation::RefMut | BindingAnnotation::Mutable => " ", _ => "", }; diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 5f92ca7b19a..ec06e72749e 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -243,7 +243,7 @@ fn check_single_match_opt_like(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: } print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)) }, - PatKind::Binding(BindByValue(MutImmutable), _, ident, None) => ident.node.to_string(), + PatKind::Binding(BindingAnnotation::Unannotated, _, ident, None) => ident.node.to_string(), PatKind::Path(ref path) => print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)), _ => return, }; diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index dfdf00cb4e7..7fa2075b41e 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -225,11 +225,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { return; } for arg in iter_input_pats(decl, body) { - if let PatKind::Binding(BindByRef(_), _, _, _) = arg.pat.node { - span_lint(cx, - TOPLEVEL_REF_ARG, - arg.pat.span, - "`ref` directly on a function argument is ignored. Consider using a reference type instead."); + match arg.pat.node { + PatKind::Binding(BindingAnnotation::Ref, _, _, _) | PatKind::Binding(BindingAnnotation::RefMut, _, _, _) => { + span_lint(cx, + TOPLEVEL_REF_ARG, + arg.pat.span, + "`ref` directly on a function argument is ignored. Consider using a reference type instead."); + }, + _ => {} } } } @@ -238,33 +241,35 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { if_let_chain! {[ let StmtDecl(ref d, _) = s.node, let DeclLocal(ref l) = d.node, - let PatKind::Binding(BindByRef(mt), _, i, None) = l.pat.node, + let PatKind::Binding(an, _, i, None) = l.pat.node, let Some(ref init) = l.init ], { - let init = Sugg::hir(cx, init, ".."); - let (mutopt,initref) = if mt == Mutability::MutMutable { - ("mut ", init.mut_addr()) - } else { - ("", init.addr()) - }; - let tyopt = if let Some(ref ty) = l.ty { - format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty.span, "_")) - } else { - "".to_owned() - }; - span_lint_and_then(cx, - TOPLEVEL_REF_ARG, - l.pat.span, - "`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead", - |db| { - db.span_suggestion(s.span, - "try", - format!("let {name}{tyopt} = {initref};", - name=snippet(cx, i.span, "_"), - tyopt=tyopt, - initref=initref)); - } - ); + if an == BindingAnnotation::Ref || an == BindingAnnotation::RefMut { + let init = Sugg::hir(cx, init, ".."); + let (mutopt,initref) = if an == BindingAnnotation::RefMut { + ("mut ", init.mut_addr()) + } else { + ("", init.addr()) + }; + let tyopt = if let Some(ref ty) = l.ty { + format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty.span, "_")) + } else { + "".to_owned() + }; + span_lint_and_then(cx, + TOPLEVEL_REF_ARG, + l.pat.span, + "`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead", + |db| { + db.span_suggestion(s.span, + "try", + format!("let {name}{tyopt} = {initref};", + name=snippet(cx, i.span, "_"), + tyopt=tyopt, + initref=initref)); + } + ); + } }}; if_let_chain! {[ let StmtSemi(ref expr, _) = s.node, diff --git a/clippy_lints/src/needless_borrow.rs b/clippy_lints/src/needless_borrow.rs index c1965be2c04..385fcc86adb 100644 --- a/clippy_lints/src/needless_borrow.rs +++ b/clippy_lints/src/needless_borrow.rs @@ -3,7 +3,7 @@ //! This lint is **warn** by default use rustc::lint::*; -use rustc::hir::{ExprAddrOf, Expr, MutImmutable, Pat, PatKind, BindingMode}; +use rustc::hir::{ExprAddrOf, Expr, MutImmutable, Pat, PatKind, BindingAnnotation}; use rustc::ty; use rustc::ty::adjustment::{Adjustment, Adjust}; use utils::{span_lint, in_macro}; @@ -63,7 +63,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow { return; } if_let_chain! {[ - let PatKind::Binding(BindingMode::BindByRef(MutImmutable), _, _, _) = pat.node, + let PatKind::Binding(BindingAnnotation::Ref, _, _, _) = pat.node, let ty::TyRef(_, ref tam) = cx.tables.pat_ty(pat).sty, tam.mutbl == MutImmutable, let ty::TyRef(_, ref tam) = tam.ty.sty, diff --git a/clippy_lints/src/needless_borrowed_ref.rs b/clippy_lints/src/needless_borrowed_ref.rs index 5318163f8a0..bfe52a41214 100644 --- a/clippy_lints/src/needless_borrowed_ref.rs +++ b/clippy_lints/src/needless_borrowed_ref.rs @@ -3,7 +3,7 @@ //! This lint is **warn** by default use rustc::lint::*; -use rustc::hir::{MutImmutable, Pat, PatKind, BindingMode}; +use rustc::hir::{MutImmutable, Pat, PatKind, BindingAnnotation}; use rustc::ty; use utils::{span_lint, in_macro}; @@ -47,7 +47,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef { if_let_chain! {[ // Pat is a pattern whose node // is a binding which "involves" a immutable reference... - let PatKind::Binding(BindingMode::BindByRef(MutImmutable), ..) = pat.node, + let PatKind::Binding(BindingAnnotation::Ref, ..) = pat.node, // Pattern's type is a reference. Get the type and mutability of referenced value (tam: TypeAndMut). let ty::TyRef(_, ref tam) = cx.tables.pat_ty(pat).sty, // This is an immutable reference. diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 286083d6849..0740ca483f7 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -123,10 +123,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { !moved_vars.contains(&defid), ], { // Note: `toplevel_ref_arg` warns if `BindByRef` - let m = match mode { - BindingMode::BindByRef(m) | BindingMode::BindByValue(m) => m, - }; - if m == Mutability::MutMutable { + if mode == BindingAnnotation::Mutable || mode == BindingAnnotation::RefMut { continue; }