fmt clippy

This commit is contained in:
Manish Goregaokar 2016-01-04 09:56:12 +05:30
parent 839ad09689
commit c9342d0121
41 changed files with 2187 additions and 1488 deletions

View file

@ -45,13 +45,13 @@ impl LintPass for ShadowPass {
fn get_lints(&self) -> LintArray {
lint_array!(SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED)
}
}
impl LateLintPass for ShadowPass {
fn check_fn(&mut self, cx: &LateContext, _: FnKind, decl: &FnDecl,
block: &Block, _: Span, _: NodeId) {
if in_external_macro(cx, block.span) { return; }
fn check_fn(&mut self, cx: &LateContext, _: FnKind, decl: &FnDecl, block: &Block, _: Span, _: NodeId) {
if in_external_macro(cx, block.span) {
return;
}
check_fn(cx, decl, block);
}
}
@ -71,20 +71,27 @@ fn check_block(cx: &LateContext, block: &Block, bindings: &mut Vec<(Name, Span)>
for stmt in &block.stmts {
match stmt.node {
StmtDecl(ref decl, _) => check_decl(cx, decl, bindings),
StmtExpr(ref e, _) | StmtSemi(ref e, _) =>
check_expr(cx, e, bindings)
StmtExpr(ref e, _) | StmtSemi(ref e, _) => check_expr(cx, e, bindings),
}
}
if let Some(ref o) = block.expr { check_expr(cx, o, bindings); }
if let Some(ref o) = block.expr {
check_expr(cx, o, bindings);
}
bindings.truncate(len);
}
fn check_decl(cx: &LateContext, decl: &Decl, bindings: &mut Vec<(Name, Span)>) {
if in_external_macro(cx, decl.span) { return; }
if is_from_for_desugar(decl) { return; }
if in_external_macro(cx, decl.span) {
return;
}
if is_from_for_desugar(decl) {
return;
}
if let DeclLocal(ref local) = decl.node {
let Local{ ref pat, ref ty, ref init, span, .. } = **local;
if let Some(ref t) = *ty { check_ty(cx, t, bindings) }
if let Some(ref t) = *ty {
check_ty(cx, t, bindings)
}
if let Some(ref o) = *init {
check_expr(cx, o, bindings);
check_pat(cx, pat, &Some(o), span, bindings);
@ -97,13 +104,12 @@ fn check_decl(cx: &LateContext, decl: &Decl, bindings: &mut Vec<(Name, Span)>) {
fn is_binding(cx: &LateContext, pat: &Pat) -> bool {
match cx.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def()) {
Some(DefVariant(..)) | Some(DefStruct(..)) => false,
_ => true
_ => true,
}
}
fn check_pat(cx: &LateContext, pat: &Pat, init: &Option<&Expr>, span: Span,
bindings: &mut Vec<(Name, Span)>) {
//TODO: match more stuff / destructuring
fn check_pat(cx: &LateContext, pat: &Pat, init: &Option<&Expr>, span: Span, bindings: &mut Vec<(Name, Span)>) {
// TODO: match more stuff / destructuring
match pat.node {
PatIdent(_, ref ident, ref inner) => {
let name = ident.node.unhygienic_name;
@ -121,17 +127,19 @@ fn check_pat(cx: &LateContext, pat: &Pat, init: &Option<&Expr>, span: Span,
bindings.push((name, ident.span));
}
}
if let Some(ref p) = *inner { check_pat(cx, p, init, span, bindings); }
if let Some(ref p) = *inner {
check_pat(cx, p, init, span, bindings);
}
}
//PatEnum(Path, Option<Vec<P<Pat>>>),
PatStruct(_, ref pfields, _) =>
// PatEnum(Path, Option<Vec<P<Pat>>>),
PatStruct(_, ref pfields, _) => {
if let Some(ref init_struct) = *init {
if let ExprStruct(_, ref efields, _) = init_struct.node {
for field in pfields {
let name = field.node.name;
let efield = efields.iter()
.find(|ref f| f.name.node == name)
.map(|f| &*f.expr);
.find(|ref f| f.name.node == name)
.map(|f| &*f.expr);
check_pat(cx, &field.node.pat, &efield, span, bindings);
}
} else {
@ -143,8 +151,9 @@ fn check_pat(cx: &LateContext, pat: &Pat, init: &Option<&Expr>, span: Span,
for field in pfields {
check_pat(cx, &field.node.pat, &None, span, bindings);
}
},
PatTup(ref inner) =>
}
}
PatTup(ref inner) => {
if let Some(ref init_tup) = *init {
if let ExprTup(ref tup) = init_tup.node {
for (i, p) in inner.iter().enumerate() {
@ -159,7 +168,8 @@ fn check_pat(cx: &LateContext, pat: &Pat, init: &Option<&Expr>, span: Span,
for p in inner {
check_pat(cx, p, &None, span, bindings);
}
},
}
}
PatBox(ref inner) => {
if let Some(ref initp) = *init {
if let ExprBox(ref inner_init) = initp.node {
@ -171,15 +181,15 @@ fn check_pat(cx: &LateContext, pat: &Pat, init: &Option<&Expr>, span: Span,
check_pat(cx, inner, init, span, bindings);
}
}
PatRegion(ref inner, _) =>
check_pat(cx, inner, init, span, bindings),
//PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
PatRegion(ref inner, _) => check_pat(cx, inner, init, span, bindings),
// PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
_ => (),
}
}
fn lint_shadow<T>(cx: &LateContext, name: Name, span: Span, lspan: Span, init:
&Option<T>, prev_span: Span) where T: Deref<Target=Expr> {
fn lint_shadow<T>(cx: &LateContext, name: Name, span: Span, lspan: Span, init: &Option<T>, prev_span: Span)
where T: Deref<Target = Expr>
{
fn note_orig(cx: &LateContext, mut db: DiagnosticWrapper, lint: &'static Lint, span: Span) {
if cx.current_level(lint) != Level::Allow {
db.span_note(span, "previous binding is here");
@ -187,51 +197,69 @@ fn lint_shadow<T>(cx: &LateContext, name: Name, span: Span, lspan: Span, init:
}
if let Some(ref expr) = *init {
if is_self_shadow(name, expr) {
let db = span_lint(cx, SHADOW_SAME, span, &format!(
"{} is shadowed by itself in {}",
snippet(cx, lspan, "_"),
snippet(cx, expr.span, "..")));
note_orig(cx, db, SHADOW_SAME, prev_span);
let db = span_lint(cx,
SHADOW_SAME,
span,
&format!("{} is shadowed by itself in {}",
snippet(cx, lspan, "_"),
snippet(cx, expr.span, "..")));
note_orig(cx, db, SHADOW_SAME, prev_span);
} else {
if contains_self(name, expr) {
let db = span_note_and_lint(cx, SHADOW_REUSE, lspan, &format!(
"{} is shadowed by {} which reuses the original value",
snippet(cx, lspan, "_"),
snippet(cx, expr.span, "..")),
expr.span, "initialization happens here");
let db = span_note_and_lint(cx,
SHADOW_REUSE,
lspan,
&format!("{} is shadowed by {} which reuses the original value",
snippet(cx, lspan, "_"),
snippet(cx, expr.span, "..")),
expr.span,
"initialization happens here");
note_orig(cx, db, SHADOW_REUSE, prev_span);
} else {
let db = span_note_and_lint(cx, SHADOW_UNRELATED, lspan, &format!(
"{} is shadowed by {}",
snippet(cx, lspan, "_"),
snippet(cx, expr.span, "..")),
expr.span, "initialization happens here");
let db = span_note_and_lint(cx,
SHADOW_UNRELATED,
lspan,
&format!("{} is shadowed by {}",
snippet(cx, lspan, "_"),
snippet(cx, expr.span, "..")),
expr.span,
"initialization happens here");
note_orig(cx, db, SHADOW_UNRELATED, prev_span);
}
}
} else {
let db = span_lint(cx, SHADOW_UNRELATED, span, &format!(
"{} shadows a previous declaration", snippet(cx, lspan, "_")));
let db = span_lint(cx,
SHADOW_UNRELATED,
span,
&format!("{} shadows a previous declaration", snippet(cx, lspan, "_")));
note_orig(cx, db, SHADOW_UNRELATED, prev_span);
}
}
fn check_expr(cx: &LateContext, expr: &Expr, bindings: &mut Vec<(Name, Span)>) {
if in_external_macro(cx, expr.span) { return; }
if in_external_macro(cx, expr.span) {
return;
}
match expr.node {
ExprUnary(_, ref e) | ExprField(ref e, _) |
ExprTupField(ref e, _) | ExprAddrOf(_, ref e) | ExprBox(ref e)
=> { check_expr(cx, e, bindings) }
ExprBlock(ref block) | ExprLoop(ref block, _) =>
{ check_block(cx, block, bindings) }
//ExprCall
//ExprMethodCall
ExprVec(ref v) | ExprTup(ref v) =>
for ref e in v { check_expr(cx, e, bindings) },
ExprUnary(_, ref e) |
ExprField(ref e, _) |
ExprTupField(ref e, _) |
ExprAddrOf(_, ref e) |
ExprBox(ref e) => check_expr(cx, e, bindings),
ExprBlock(ref block) | ExprLoop(ref block, _) => check_block(cx, block, bindings),
// ExprCall
// ExprMethodCall
ExprVec(ref v) | ExprTup(ref v) => {
for ref e in v {
check_expr(cx, e, bindings)
}
}
ExprIf(ref cond, ref then, ref otherwise) => {
check_expr(cx, cond, bindings);
check_block(cx, then, bindings);
if let Some(ref o) = *otherwise { check_expr(cx, o, bindings); }
if let Some(ref o) = *otherwise {
check_expr(cx, o, bindings);
}
}
ExprWhile(ref cond, ref block, _) => {
check_expr(cx, cond, bindings);
@ -243,7 +271,7 @@ fn check_expr(cx: &LateContext, expr: &Expr, bindings: &mut Vec<(Name, Span)>) {
for ref arm in arms {
for ref pat in &arm.pats {
check_pat(cx, &pat, &Some(&**init), pat.span, bindings);
//This is ugly, but needed to get the right type
// This is ugly, but needed to get the right type
if let Some(ref guard) = arm.guard {
check_expr(cx, guard, bindings);
}
@ -252,7 +280,7 @@ fn check_expr(cx: &LateContext, expr: &Expr, bindings: &mut Vec<(Name, Span)>) {
}
}
}
_ => ()
_ => (),
}
}
@ -266,7 +294,11 @@ fn check_ty(cx: &LateContext, ty: &Ty, bindings: &mut Vec<(Name, Span)>) {
}
TyPtr(MutTy{ ty: ref mty, .. }) |
TyRptr(_, MutTy{ ty: ref mty, .. }) => check_ty(cx, mty, bindings),
TyTup(ref tup) => { for ref t in tup { check_ty(cx, t, bindings) } }
TyTup(ref tup) => {
for ref t in tup {
check_ty(cx, t, bindings)
}
}
TyTypeof(ref expr) => check_expr(cx, expr, bindings),
_ => (),
}
@ -276,23 +308,22 @@ fn is_self_shadow(name: Name, expr: &Expr) -> bool {
match expr.node {
ExprBox(ref inner) |
ExprAddrOf(_, ref inner) => is_self_shadow(name, inner),
ExprBlock(ref block) => block.stmts.is_empty() && block.expr.as_ref().
map_or(false, |ref e| is_self_shadow(name, e)),
ExprUnary(op, ref inner) => (UnDeref == op) &&
is_self_shadow(name, inner),
ExprBlock(ref block) => {
block.stmts.is_empty() && block.expr.as_ref().map_or(false, |ref e| is_self_shadow(name, e))
}
ExprUnary(op, ref inner) => (UnDeref == op) && is_self_shadow(name, inner),
ExprPath(_, ref path) => path_eq_name(name, path),
_ => false,
}
}
fn path_eq_name(name: Name, path: &Path) -> bool {
!path.global && path.segments.len() == 1 &&
path.segments[0].identifier.unhygienic_name == name
!path.global && path.segments.len() == 1 && path.segments[0].identifier.unhygienic_name == name
}
struct ContainsSelf {
name: Name,
result: bool
result: bool,
}
impl<'v> Visitor<'v> for ContainsSelf {
@ -304,7 +335,10 @@ impl<'v> Visitor<'v> for ContainsSelf {
}
fn contains_self(name: Name, expr: &Expr) -> bool {
let mut cs = ContainsSelf { name: name, result: false };
let mut cs = ContainsSelf {
name: name,
result: false,
};
cs.visit_expr(expr);
cs.result
}