mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-15 09:27:25 +00:00
commit
a1c3870c4e
6 changed files with 43 additions and 40 deletions
|
@ -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) => "<mut> ",
|
||||
BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
|
||||
_ => "",
|
||||
};
|
||||
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue