Skip to content

Update for rustc 1.19.0-nightly (4bf5c99af 2017-06-10) and some cleanups. #1823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> 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.tables.expr_ty(assignee);
if ty.walk_shallow().next().is_some() {
return; // implements_trait does not work with generics
}
let rty = cx.tables.expr_ty(rhs);
if rty.walk_shallow().next().is_some() {
return; // implements_trait does not work with generics
}
span_lint_and_then(cx,
MISREFACTORED_ASSIGN_OP,
expr.span,
Expand Down Expand Up @@ -120,13 +112,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
#[allow(cyclomatic_complexity)]
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
let ty = cx.tables.expr_ty(assignee);
if ty.walk_shallow().next().is_some() {
return; // implements_trait does not work with generics
}
let rty = cx.tables.expr_ty(rhs);
if rty.walk_shallow().next().is_some() {
return; // implements_trait does not work with generics
}
macro_rules! ops {
($op:expr,
$cx:expr,
Expand All @@ -152,7 +138,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
let hir::Item_::ItemImpl(_, _, _, _, Some(ref trait_ref), _, _) = item.node,
trait_ref.path.def.def_id() == trait_id
], { return; }}
implements_trait($cx, $ty, trait_id, &[$rty], None)
implements_trait($cx, $ty, trait_id, &[$rty])
},)*
_ => false,
}
Expand Down
12 changes: 6 additions & 6 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use reexport::*;
use rustc::lint::*;
use rustc::hir::*;
use rustc::ty;
use rustc::ty::{self, TyCtxt};
use semver::Version;
use syntax::ast::{Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use syntax::codemap::Span;
Expand Down Expand Up @@ -158,22 +158,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
}
}

fn is_relevant_item(tcx: ty::TyCtxt, item: &Item) -> bool {
fn is_relevant_item(tcx: TyCtxt, item: &Item) -> bool {
if let ItemFn(_, _, _, _, _, eid) = item.node {
is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value)
} else {
false
}
}

fn is_relevant_impl(tcx: ty::TyCtxt, item: &ImplItem) -> bool {
fn is_relevant_impl(tcx: TyCtxt, item: &ImplItem) -> bool {
match item.node {
ImplItemKind::Method(_, eid) => is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value),
_ => false,
}
}

fn is_relevant_trait(tcx: ty::TyCtxt, item: &TraitItem) -> bool {
fn is_relevant_trait(tcx: TyCtxt, item: &TraitItem) -> bool {
match item.node {
TraitItemKind::Method(_, TraitMethod::Required(_)) => true,
TraitItemKind::Method(_, TraitMethod::Provided(eid)) => {
Expand All @@ -183,7 +183,7 @@ fn is_relevant_trait(tcx: ty::TyCtxt, item: &TraitItem) -> bool {
}
}

fn is_relevant_block(tcx: ty::TyCtxt, tables: &ty::TypeckTables, block: &Block) -> bool {
fn is_relevant_block(tcx: TyCtxt, tables: &ty::TypeckTables, block: &Block) -> bool {
for stmt in &block.stmts {
match stmt.node {
StmtDecl(_, _) => return true,
Expand All @@ -196,7 +196,7 @@ fn is_relevant_block(tcx: ty::TyCtxt, tables: &ty::TypeckTables, block: &Block)
block.expr.as_ref().map_or(false, |e| is_relevant_expr(tcx, tables, e))
}

fn is_relevant_expr(tcx: ty::TyCtxt, tables: &ty::TypeckTables, expr: &Expr) -> bool {
fn is_relevant_expr(tcx: TyCtxt, tables: &ty::TypeckTables, expr: &Expr) -> bool {
match expr.node {
ExprBlock(ref block) => is_relevant_block(tcx, tables, block),
ExprRet(Some(ref e)) => is_relevant_expr(tcx, tables, e),
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc::hir::def::Def;
use rustc_const_eval::lookup_const_by_id;
use rustc_const_math::ConstInt;
use rustc::hir::*;
use rustc::ty::{self, TyCtxt};
use rustc::ty::{self, TyCtxt, Ty};
use rustc::ty::subst::{Substs, Subst};
use std::cmp::Ordering::{self, Equal};
use std::cmp::PartialOrd;
Expand Down Expand Up @@ -161,7 +161,7 @@ impl PartialOrd for Constant {

/// parse a `LitKind` to a `Constant`
#[allow(cast_possible_wrap)]
pub fn lit_to_constant<'a, 'tcx>(lit: &LitKind, tcx: TyCtxt<'a, 'tcx, 'tcx>, mut ty: ty::Ty<'tcx>) -> Constant {
pub fn lit_to_constant<'a, 'tcx>(lit: &LitKind, tcx: TyCtxt<'a, 'tcx, 'tcx>, mut ty: Ty<'tcx>) -> Constant {
use syntax::ast::*;
use syntax::ast::LitIntType::*;
use rustc::ty::util::IntTypeExt;
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc::lint::*;
use rustc::ty;
use rustc::ty::Ty;
use rustc::hir::*;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -251,11 +251,11 @@ fn if_sequence(mut expr: &Expr) -> (SmallVector<&Expr>, SmallVector<&Block>) {
}

/// Return the list of bindings in a pattern.
fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> HashMap<InternedString, ty::Ty<'tcx>> {
fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> HashMap<InternedString, Ty<'tcx>> {
fn bindings_impl<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
pat: &Pat,
map: &mut HashMap<InternedString, ty::Ty<'tcx>>
map: &mut HashMap<InternedString, Ty<'tcx>>
) {
match pat.node {
PatKind::Box(ref pat) |
Expand Down
20 changes: 9 additions & 11 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rustc::lint::*;
use rustc::ty::TypeVariants;
use rustc::ty;
use rustc::ty::{self, Ty};
use rustc::hir::*;
use syntax::codemap::Span;
use utils::paths;
Expand Down Expand Up @@ -89,7 +88,7 @@ fn check_hash_peq<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
span: Span,
trait_ref: &TraitRef,
ty: ty::Ty<'tcx>,
ty: Ty<'tcx>,
hash_is_automatically_derived: bool
) {
if_let_chain! {[
Expand Down Expand Up @@ -134,28 +133,27 @@ fn check_hash_peq<'a, 'tcx>(
}

/// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref: &TraitRef, ty: ty::Ty<'tcx>) {
fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref: &TraitRef, ty: Ty<'tcx>) {
if match_path_old(&trait_ref.path, &paths::CLONE_TRAIT) {
let def_id = cx.tcx.hir.local_def_id(item.id);
if !is_copy(cx, ty, def_id) {
if !is_copy(cx, ty) {
return;
}

match ty.sty {
TypeVariants::TyAdt(def, _) if def.is_union() => return,
ty::TyAdt(def, _) if def.is_union() => return,

// Some types are not Clone by default but could be cloned “by hand” if necessary
TypeVariants::TyAdt(def, substs) => {
ty::TyAdt(def, substs) => {
for variant in &def.variants {
for field in &variant.fields {
match field.ty(cx.tcx, substs).sty {
TypeVariants::TyArray(_, size) if size > 32 => {
ty::TyArray(_, size) if size > 32 => {
return;
},
TypeVariants::TyFnPtr(..) => {
ty::TyFnPtr(..) => {
return;
},
TypeVariants::TyTuple(tys, _) if tys.len() > 12 => {
ty::TyTuple(tys, _) if tys.len() > 12 => {
return;
},
_ => (),
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/drop_forget_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
expr.span,
&msg,
arg.span,
&format!("argument has type {}", arg_ty.sty));
} else if is_copy(cx, arg_ty, cx.tcx.hir.local_def_id(cx.tcx.hir.get_parent(arg.id))) {
&format!("argument has type {}", arg_ty));
} else if is_copy(cx, arg_ty) {
if match_def_path(cx.tcx, def_id, &paths::DROP) {
lint = DROP_COPY;
msg = DROP_COPY_SUMMARY.to_string();
Expand All @@ -151,7 +151,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
expr.span,
&msg,
arg.span,
&format!("argument has type {}", arg_ty.sty));
&format!("argument has type {}", arg_ty));
}
}}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/enum_glob_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: NodeId) {
// only check top level `use` statements
for item in &m.item_ids {
self.lint_item(cx, cx.krate.item(item.id));
self.lint_item(cx, cx.tcx.hir.expect_item(item.id));
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions clippy_lints/src/eq_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
BiNe | BiEq => (cx.tcx.lang_items.eq_trait(), true),
BiLt | BiLe | BiGe | BiGt => (cx.tcx.lang_items.ord_trait(), true),
};
let parent = cx.tcx.hir.get_parent(e.id);
let parent = cx.tcx.hir.local_def_id(parent);
if let Some(trait_id) = trait_id {
#[allow(match_same_arms)]
match (&left.node, &right.node) {
Expand All @@ -87,10 +85,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
(&ExprAddrOf(_, ref l), &ExprAddrOf(_, ref r)) => {
let lty = cx.tables.expr_ty(l);
let rty = cx.tables.expr_ty(r);
let lcpy = is_copy(cx, lty, parent);
let rcpy = is_copy(cx, rty, parent);
let lcpy = is_copy(cx, lty);
let rcpy = is_copy(cx, rty);
// either operator autorefs or both args are copyable
if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty], None) {
if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty]) {
span_lint_and_then(cx,
OP_REF,
e.span,
Expand All @@ -104,13 +102,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
(right.span, rsnip)]);
})
} else if lcpy && !rcpy &&
implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)], None) {
implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)]) {
span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
let lsnip = snippet(cx, l.span, "...").to_string();
db.span_suggestion(left.span, "use the left value directly", lsnip);
})
} else if !lcpy && rcpy &&
implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty], None) {
implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty]) {
span_lint_and_then(cx,
OP_REF,
e.span,
Expand All @@ -124,9 +122,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
// &foo == bar
(&ExprAddrOf(_, ref l), _) => {
let lty = cx.tables.expr_ty(l);
let lcpy = is_copy(cx, lty, parent);
let lcpy = is_copy(cx, lty);
if (requires_ref || lcpy) &&
implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)], None) {
implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)]) {
span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
let lsnip = snippet(cx, l.span, "...").to_string();
db.span_suggestion(left.span, "use the left value directly", lsnip);
Expand All @@ -136,9 +134,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
// foo == &bar
(_, &ExprAddrOf(_, ref r)) => {
let rty = cx.tables.expr_ty(r);
let rcpy = is_copy(cx, rty, parent);
let rcpy = is_copy(cx, rty);
if (requires_ref || rcpy) &&
implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty], None) {
implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty]) {
span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
let rsnip = snippet(cx, r.span, "...").to_string();
db.span_suggestion(right.span, "use the right value directly", rsnip);
Expand Down
39 changes: 15 additions & 24 deletions clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use rustc::hir::map::Node::{NodeExpr, NodeStmt};
use rustc::lint::*;
use rustc::middle::expr_use_visitor::*;
use rustc::middle::mem_categorization::{cmt, Categorization};
use rustc::ty;
use rustc::ty::{self, Ty};
use rustc::util::nodemap::NodeSet;
use syntax::ast::NodeId;
use syntax::codemap::Span;
use utils::span_lint;
use utils::{span_lint, type_size};

pub struct Pass {
pub too_large_for_stack: u64,
Expand Down Expand Up @@ -37,14 +37,13 @@ declare_lint! {
"using `Box<T>` where unnecessary"
}

fn is_non_trait_box(ty: ty::Ty) -> bool {
fn is_non_trait_box(ty: Ty) -> bool {
ty.is_box() && !ty.boxed_ty().is_trait()
}

struct EscapeDelegate<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
set: NodeSet,
tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
too_large_for_stack: u64,
}

Expand All @@ -65,19 +64,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
node_id: NodeId
) {
let fn_def_id = cx.tcx.hir.local_def_id(node_id);
let param_env = cx.tcx.param_env(fn_def_id).reveal_all();
let mut v = EscapeDelegate {
cx: cx,
set: NodeSet(),
tcx: cx.tcx,
param_env: param_env,
too_large_for_stack: self.too_large_for_stack,
};

cx.tcx.infer_ctxt(body.id()).enter(|infcx| {
let region_maps = &cx.tcx.region_maps(fn_def_id);
let mut vis = ExprUseVisitor::new(&mut v, region_maps, &infcx, param_env);
vis.consume_body(body);
});
let region_maps = &cx.tcx.region_maps(fn_def_id);
ExprUseVisitor::new(&mut v, cx.tcx, cx.param_env, region_maps, cx.tables)
.consume_body(body);

for node in v.set {
span_lint(cx,
Expand All @@ -88,7 +83,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
}
}

impl<'a, 'gcx: 'tcx, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'gcx> {
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
if let Categorization::Local(lid) = cmt.cat {
if let Move(DirectRefMove) = mode {
Expand All @@ -99,7 +94,7 @@ impl<'a, 'gcx: 'tcx, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'gcx> {
}
fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
let map = &self.tcx.hir;
let map = &self.cx.tcx.hir;
if map.is_argument(consume_pat.id) {
// Skip closure arguments
if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
Expand Down Expand Up @@ -172,18 +167,14 @@ impl<'a, 'gcx: 'tcx, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'gcx> {
fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}
}

impl<'a, 'tcx: 'a> EscapeDelegate<'a, 'tcx> {
fn is_large_box(&self, ty: ty::Ty) -> bool {
impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
// Large types need to be boxed to avoid stack
// overflows.
if ty.is_box() {
if let Some(inner) = self.tcx.lift(&ty.boxed_ty()) {
if let Ok(layout) = inner.layout(self.tcx, self.param_env) {
return layout.size(self.tcx).bytes() > self.too_large_for_stack;
}
}
type_size(self.cx, ty.boxed_ty()).unwrap_or(0) > self.too_large_for_stack
} else {
false
}

false
}
}
4 changes: 2 additions & 2 deletions clippy_lints/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc::hir::*;
use rustc::hir::map::Node::NodeItem;
use rustc::lint::*;
use rustc::ty::TypeVariants;
use rustc::ty;
use syntax::ast::LitKind;
use syntax::symbol::InternedString;
use utils::paths;
Expand Down Expand Up @@ -132,7 +132,7 @@ fn check_arg_is_display(cx: &LateContext, expr: &Expr) -> bool {
], {
let ty = walk_ptrs_ty(cx.tables.pat_ty(&pat[0]));

return ty.sty == TypeVariants::TyStr || match_type(cx, ty, &paths::STRING);
return ty.sty == ty::TyStr || match_type(cx, ty, &paths::STRING);
}}

false
Expand Down
Loading