Skip to content

Commit

Permalink
WIP rustup
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarton committed Nov 16, 2016
1 parent bad26a5 commit bbf64f3
Show file tree
Hide file tree
Showing 51 changed files with 209 additions and 222 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl LateLintPass for Arithmetic {
hir::BiShr | hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe | hir::BiGt => return,
_ => (),
}
let (l_ty, r_ty) = (cx.tcx.expr_ty(l), cx.tcx.expr_ty(r));
let (l_ty, r_ty) = (cx.tcx.tables().expr_ty(l), cx.tcx.tables().expr_ty(r));
if l_ty.is_integral() && r_ty.is_integral() {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.span = Some(expr.span);
Expand All @@ -69,7 +69,7 @@ impl LateLintPass for Arithmetic {
}
}
hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
let ty = cx.tcx.expr_ty(arg);
let ty = cx.tcx.tables().expr_ty(arg);
if ty.is_integral() {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.span = Some(expr.span);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/array_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl LateLintPass for ArrayIndexing {
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
if let hir::ExprIndex(ref array, ref index) = e.node {
// Array with known size can be checked statically
let ty = cx.tcx.expr_ty(array);
let ty = cx.tcx.tables().expr_ty(array);
if let ty::TyArray(_, size) = ty.sty {
let size = ConstInt::Infer(size as u64);

Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ impl LateLintPass for AssignOps {
if let hir::ExprBinary(binop, ref l, ref r) = rhs.node {
if op.node == binop.node {
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
let ty = cx.tcx.expr_ty(assignee);
let ty = cx.tcx.tables().expr_ty(assignee);
if ty.walk_shallow().next().is_some() {
return; // implements_trait does not work with generics
}
let rty = cx.tcx.expr_ty(rhs);
let rty = cx.tcx.tables().expr_ty(rhs);
if rty.walk_shallow().next().is_some() {
return; // implements_trait does not work with generics
}
Expand Down Expand Up @@ -116,11 +116,11 @@ impl LateLintPass for AssignOps {
hir::ExprAssign(ref assignee, ref e) => {
if let hir::ExprBinary(op, ref l, ref r) = e.node {
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
let ty = cx.tcx.expr_ty(assignee);
let ty = cx.tcx.tables().expr_ty(assignee);
if ty.walk_shallow().next().is_some() {
return; // implements_trait does not work with generics
}
let rty = cx.tcx.expr_ty(rhs);
let rty = cx.tcx.tables().expr_ty(rhs);
if rty.walk_shallow().next().is_some() {
return; // implements_trait does not work with generics
}
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,24 @@ impl LateLintPass for AttrPass {
}

fn is_relevant_item(cx: &LateContext, item: &Item) -> bool {
if let ItemFn(_, _, _, _, _, ref block) = item.node {
is_relevant_block(cx, block)
if let ItemFn(_, _, _, _, _, ref expr) = item.node {
is_relevant_expr(cx, expr)
} else {
false
}
}

fn is_relevant_impl(cx: &LateContext, item: &ImplItem) -> bool {
match item.node {
ImplItemKind::Method(_, ref block) => is_relevant_block(cx, block),
ImplItemKind::Method(_, ref expr) => is_relevant_expr(cx, expr),
_ => false,
}
}

fn is_relevant_trait(cx: &LateContext, item: &TraitItem) -> bool {
match item.node {
MethodTraitItem(_, None) => true,
MethodTraitItem(_, Some(ref block)) => is_relevant_block(cx, block),
MethodTraitItem(_, Some(ref expr)) => is_relevant_expr(cx, expr),
_ => false,
}
}
Expand Down
12 changes: 2 additions & 10 deletions clippy_lints/src/block_in_if_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,9 @@ struct ExVisitor<'v> {

impl<'v> Visitor<'v> for ExVisitor<'v> {
fn visit_expr(&mut self, expr: &'v Expr) {
if let ExprClosure(_, _, ref block, _) = expr.node {
if let ExprClosure(_, _, ref expr, _) = expr.node {
let complex = {
if block.stmts.is_empty() {
if let Some(ref ex) = block.expr {
matches!(ex.node, ExprBlock(_))
} else {
false
}
} else {
true
}
matches!(expr.node, ExprBlock(_))
};
if complex {
self.found_block = Some(expr);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl<'a, 'v, 'tcx> Visitor<'v> for NonminimalBoolVisitor<'a, 'tcx> {
match e.node {
ExprBinary(binop, _, _) if binop.node == BiOr || binop.node == BiAnd => self.bool_expr(e),
ExprUnary(UnNot, ref inner) => {
if self.0.tcx.node_types()[&inner.id].is_bool() {
if self.0.tcx.tables.borrow().node_types[&inner.id].is_bool() {
self.bool_expr(e);
} else {
walk_expr(self, e);
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ impl LateLintPass for CopyAndPaste {
}

let (conds, blocks) = if_sequence(expr);
lint_same_then_else(cx, blocks.as_slice());
lint_same_cond(cx, conds.as_slice());
lint_same_then_else(cx, &blocks);
lint_same_cond(cx, &conds);
lint_match_arms(cx, expr);
}
}
Expand Down Expand Up @@ -219,8 +219,8 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {
/// Eg. would return `([a, b], [c, d, e])` for the expression
/// `if a { c } else if b { d } else { e }`.
fn if_sequence(mut expr: &Expr) -> (SmallVector<&Expr>, SmallVector<&Block>) {
let mut conds = SmallVector::zero();
let mut blocks = SmallVector::zero();
let mut conds = SmallVector::new();
let mut blocks = SmallVector::new();

while let ExprIf(ref cond, ref then_block, ref else_expr) = expr.node {
conds.push(&**cond);
Expand Down Expand Up @@ -256,7 +256,7 @@ fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> HashMap<Interned
}
PatKind::Binding(_, ref ident, ref as_pat) => {
if let Entry::Vacant(v) = map.entry(ident.node.as_str()) {
v.insert(cx.tcx.pat_ty(pat));
v.insert(cx.tcx.tables().pat_ty(pat));
}
if let Some(ref as_pat) = *as_pat {
bindings_impl(cx, as_pat, map);
Expand Down
22 changes: 11 additions & 11 deletions clippy_lints/src/cyclomatic_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ impl LintPass for CyclomaticComplexity {
}

impl CyclomaticComplexity {
fn check<'a, 'tcx>(&mut self, cx: &'a LateContext<'a, 'tcx>, block: &Block, span: Span) {
fn check<'a, 'tcx>(&mut self, cx: &'a LateContext<'a, 'tcx>, expr: &Expr, span: Span) {
if in_macro(cx, span) {
return;
}

let cfg = CFG::new(cx.tcx, block);
let cfg = CFG::new(cx.tcx, expr);
let n = cfg.graph.len_nodes() as u64;
let e = cfg.graph.len_edges() as u64;
if e + 2 < n {
Expand All @@ -62,9 +62,9 @@ impl CyclomaticComplexity {
returns: 0,
tcx: &cx.tcx,
};
helper.visit_block(block);
helper.visit_expr(expr);
let CCHelper { match_arms, divergence, short_circuits, returns, .. } = helper;
let ret_ty = cx.tcx.node_id_to_type(block.id);
let ret_ty = cx.tcx.tables().node_id_to_type(expr.id);
let ret_adjust = if match_type(cx, ret_ty, &paths::RESULT) {
returns
} else {
Expand Down Expand Up @@ -92,22 +92,22 @@ impl CyclomaticComplexity {

impl LateLintPass for CyclomaticComplexity {
fn check_item(&mut self, cx: &LateContext, item: &Item) {
if let ItemFn(_, _, _, _, _, ref block) = item.node {
if let ItemFn(_, _, _, _, _, ref expr) = item.node {
if !attr::contains_name(&item.attrs, "test") {
self.check(cx, block, item.span);
self.check(cx, expr, item.span);
}
}
}

fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
if let ImplItemKind::Method(_, ref block) = item.node {
self.check(cx, block, item.span);
if let ImplItemKind::Method(_, ref expr) = item.node {
self.check(cx, expr, item.span);
}
}

fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
if let MethodTraitItem(_, Some(ref block)) = item.node {
self.check(cx, block, item.span);
if let MethodTraitItem(_, Some(ref expr)) = item.node {
self.check(cx, expr, item.span);
}
}

Expand Down Expand Up @@ -139,7 +139,7 @@ impl<'a, 'b, 'tcx, 'gcx> Visitor<'a> for CCHelper<'b, 'gcx, 'tcx> {
}
ExprCall(ref callee, _) => {
walk_expr(self, e);
let ty = self.tcx.node_id_to_type(callee.id);
let ty = self.tcx.tables().node_id_to_type(callee.id);
match ty.sty {
ty::TyFnDef(_, _, ty) |
ty::TyFnPtr(ty) if ty.sig.skip_binder().output.sty == ty::TyNever => {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl LintPass for Derive {
impl LateLintPass for Derive {
fn check_item(&mut self, cx: &LateContext, item: &Item) {
if let ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
let ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(item.id)).ty;
let ty = cx.tcx.item_type(cx.tcx.map.local_def_id(item.id));
let is_automatically_derived = is_automatically_derived(&*item.attrs);

check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/drop_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl LateLintPass for Pass {
}

fn check_drop_arg(cx: &LateContext, call_span: Span, arg: &Expr) {
let arg_ty = cx.tcx.expr_ty(arg);
let arg_ty = cx.tcx.tables().expr_ty(arg);
if let ty::TyRef(..) = arg_ty.sty {
span_note_and_lint(cx,
DROP_REF,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn check_cond<'a, 'tcx, 'b>(cx: &'a LateContext<'a, 'tcx>, check: &'b Expr) -> O
let ExprAddrOf(_, ref key) = params[1].node
], {
let map = &params[0];
let obj_ty = walk_ptrs_ty(cx.tcx.expr_ty(map));
let obj_ty = walk_ptrs_ty(cx.tcx.tables().expr_ty(map));

return if match_type(cx, obj_ty, &paths::BTREEMAP) {
Some(("BTreeMap", map, key))
Expand Down
21 changes: 12 additions & 9 deletions clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc::infer::InferCtxt;
use rustc::lint::*;
use rustc::middle::expr_use_visitor::*;
use rustc::middle::mem_categorization::{cmt, Categorization};
use rustc::ty::adjustment::AutoAdjustment;
use rustc::ty::adjustment::Adjustment;
use rustc::ty;
use rustc::ty::layout::TargetDataLayout;
use rustc::util::nodemap::NodeSet;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl LintPass for Pass {
}

impl LateLintPass for Pass {
fn check_fn(&mut self, cx: &LateContext, _: visit::FnKind, decl: &FnDecl, body: &Block, _: Span, id: NodeId) {
fn check_fn(&mut self, cx: &LateContext, _: visit::FnKind, decl: &FnDecl, body: &Expr, _: Span, id: NodeId) {
let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);

let infcx = cx.tcx.borrowck_fake_infer_ctxt(param_env);
Expand Down Expand Up @@ -146,32 +146,35 @@ impl<'a, 'tcx: 'a+'gcx, 'gcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx, 'g
}
fn borrow(&mut self, borrow_id: NodeId, _: Span, cmt: cmt<'tcx>, _: &ty::Region, _: ty::BorrowKind,
loan_cause: LoanCause) {
use rustc::ty::adjustment::Adjust;

if let Categorization::Local(lid) = cmt.cat {
if self.set.contains(&lid) {
if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.tcx
if let Some(&Adjust::DerefRef { autoderefs, .. }) = self.tcx
.tables
.borrow()
.adjustments
.get(&borrow_id) {
.get(&borrow_id)
.map(|a| &a.kind) {
if LoanCause::AutoRef == loan_cause {
// x.foo()
if adj.autoderefs == 0 {
if autoderefs == 0 {
self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
}
} else {
span_bug!(cmt.span, "Unknown adjusted AutoRef");
}
} else if LoanCause::AddrOf == loan_cause {
// &x
if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.tcx
if let Some(&Adjust::DerefRef { autoderefs, .. }) = self.tcx
.tables
.borrow()
.adjustments
.get(&self.tcx
.map
.get_parent_node(borrow_id)) {
if adj.autoderefs <= 1 {
.map
.get_parent_node(borrow_id))
.map(|a| &a.kind) {
if autoderefs <= 1 {
// foo(&x) where no extra autoreffing is happening
self.set.remove(&lid);
}
Expand Down
81 changes: 37 additions & 44 deletions clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,60 +48,53 @@ impl LateLintPass for EtaPass {
}

fn check_closure(cx: &LateContext, expr: &Expr) {
if let ExprClosure(_, ref decl, ref blk, _) = expr.node {
if !blk.stmts.is_empty() {
// || {foo(); bar()}; can't be reduced here
return;
}

if let Some(ref ex) = blk.expr {
if let ExprCall(ref caller, ref args) = ex.node {
if args.len() != decl.inputs.len() {
// Not the same number of arguments, there
// is no way the closure is the same as the function
return;
}
if is_adjusted(cx, ex) || args.iter().any(|arg| is_adjusted(cx, arg)) {
// Are the expression or the arguments type-adjusted? Then we need the closure
return;
if let ExprClosure(_, ref decl, ref ex, _) = expr.node {
if let ExprCall(ref caller, ref args) = ex.node {
if args.len() != decl.inputs.len() {
// Not the same number of arguments, there
// is no way the closure is the same as the function
return;
}
if is_adjusted(cx, ex) || args.iter().any(|arg| is_adjusted(cx, arg)) {
// Are the expression or the arguments type-adjusted? Then we need the closure
return;
}
let fn_ty = cx.tcx.tables().expr_ty(caller);
match fn_ty.sty {
// Is it an unsafe function? They don't implement the closure traits
ty::TyFnDef(_, _, fn_ty) |
ty::TyFnPtr(fn_ty) => {
if fn_ty.unsafety == Unsafety::Unsafe ||
fn_ty.sig.skip_binder().output.sty == ty::TyNever {
return;
}
}
let fn_ty = cx.tcx.expr_ty(caller);
match fn_ty.sty {
// Is it an unsafe function? They don't implement the closure traits
ty::TyFnDef(_, _, fn_ty) |
ty::TyFnPtr(fn_ty) => {
if fn_ty.unsafety == Unsafety::Unsafe ||
fn_ty.sig.skip_binder().output.sty == ty::TyNever {
_ => (),
}
for (a1, a2) in decl.inputs.iter().zip(args) {
if let PatKind::Binding(_, ident, _) = a1.pat.node {
// XXXManishearth Should I be checking the binding mode here?
if let ExprPath(None, ref p) = a2.node {
if p.segments.len() != 1 {
// If it's a proper path, it can't be a local variable
return;
}
}
_ => (),
}
for (a1, a2) in decl.inputs.iter().zip(args) {
if let PatKind::Binding(_, ident, _) = a1.pat.node {
// XXXManishearth Should I be checking the binding mode here?
if let ExprPath(None, ref p) = a2.node {
if p.segments.len() != 1 {
// If it's a proper path, it can't be a local variable
return;
}
if p.segments[0].name != ident.node {
// The two idents should be the same
return;
}
} else {
if p.segments[0].name != ident.node {
// The two idents should be the same
return;
}
} else {
return;
}
} else {
return;
}
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| {
if let Some(snippet) = snippet_opt(cx, caller.span) {
db.span_suggestion(expr.span, "remove closure as shown:", snippet);
}
});
}
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| {
if let Some(snippet) = snippet_opt(cx, caller.span) {
db.span_suggestion(expr.span, "remove closure as shown:", snippet);
}
});
}
}
}
Loading

0 comments on commit bbf64f3

Please sign in to comment.