Skip to content

Commit

Permalink
Rustup
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarton committed Sep 23, 2016
1 parent fc9f2b9 commit e877de1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 61 deletions.
3 changes: 1 addition & 2 deletions clippy_lints/src/enum_glob_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rustc::hir::*;
use rustc::hir::def::Def;
use rustc::hir::map::Node::NodeItem;
use rustc::lint::{LateLintPass, LintPass, LateContext, LintArray, LintContext};
use rustc::middle::cstore::DefLike;
use syntax::ast::NodeId;
use syntax::codemap::Span;
use utils::span_lint;
Expand Down Expand Up @@ -61,7 +60,7 @@ impl EnumGlobUse {
} else {
let child = cx.sess().cstore.item_children(def.full_def().def_id());
if let Some(child) = child.first() {
if let DefLike::DlDef(Def::Variant(..)) = child.def {
if let Some(Def::Variant(..)) = cx.tcx.sess.cstore.describe_def(child.def_id) {
span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
}
}
Expand Down
32 changes: 14 additions & 18 deletions clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc::lint::*;
use rustc::hir::def_id::DefId;
use rustc::ty::{self, MethodTraitItemId, ImplOrTraitItemId};
use rustc::ty::{self, ImplOrTraitItem};
use rustc::hir::*;
use syntax::ast::{Lit, LitKind, Name};
use syntax::codemap::{Span, Spanned};
Expand Down Expand Up @@ -184,37 +184,33 @@ fn check_len_zero(cx: &LateContext, span: Span, name: &Name, args: &[P<Expr>], l
/// Check if this type has an `is_empty` method.
fn has_is_empty(cx: &LateContext, expr: &Expr) -> bool {
/// Get an `ImplOrTraitItem` and return true if it matches `is_empty(self)`.
fn is_is_empty(cx: &LateContext, id: &ImplOrTraitItemId) -> bool {
if let MethodTraitItemId(def_id) = *id {
if let ty::MethodTraitItem(ref method) = cx.tcx.impl_or_trait_item(def_id) {
method.name.as_str() == "is_empty" && method.fty.sig.skip_binder().inputs.len() == 1
} else {
false
}
fn is_is_empty(item: &ImplOrTraitItem) -> bool {
if let ty::MethodTraitItem(ref method) = *item {
method.name.as_str() == "is_empty" && method.fty.sig.skip_binder().inputs.len() == 1
} else {
false
}
}

/// Check the inherent impl's items for an `is_empty(self)` method.
fn has_is_empty_impl(cx: &LateContext, id: &DefId) -> bool {
let impl_items = cx.tcx.impl_items.borrow();
cx.tcx.inherent_impls.borrow().get(id).map_or(false, |ids| {
ids.iter().any(|iid| impl_items.get(iid).map_or(false, |iids| iids.iter().any(|i| is_is_empty(cx, i))))
fn has_is_empty_impl(cx: &LateContext, id: DefId) -> bool {
cx.tcx.inherent_impls.borrow()[&id].iter().any(|imp| {
cx.tcx.impl_or_trait_items(*imp).iter().any(|item| {
is_is_empty(&cx.tcx.impl_or_trait_item(*item))
})
})
}

let ty = &walk_ptrs_ty(cx.tcx.expr_ty(expr));
match ty.sty {
ty::TyTrait(_) => {
cx.tcx
.trait_item_def_ids
.borrow()
.get(&ty.ty_to_def_id().expect("trait impl not found"))
.map_or(false, |ids| ids.iter().any(|i| is_is_empty(cx, i)))
.impl_or_trait_items(ty.ty_to_def_id().expect("trait impl not found"))
.iter()
.any(|item| is_is_empty(&cx.tcx.impl_or_trait_item(*item)))
}
ty::TyProjection(_) => ty.ty_to_def_id().map_or(false, |id| has_is_empty_impl(cx, &id)),
ty::TyAdt(id, _) => has_is_empty_impl(cx, &id.did),
ty::TyProjection(_) => ty.ty_to_def_id().map_or(false, |id| has_is_empty_impl(cx, id)),
ty::TyAdt(id, _) => has_is_empty_impl(cx, id.did),
ty::TyArray(..) | ty::TyStr => true,
_ => false,
}
Expand Down
8 changes: 6 additions & 2 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,10 @@ impl<'v, 't> Visitor<'v> for VarVisitor<'v, 't> {
if let Some(def) = def_map.get(&seqexpr.id) {
match def.base_def {
Def::Local(..) | Def::Upvar(..) => {
let extent = self.cx.tcx.region_maps.var_scope(def.base_def.var_id());
let def_id = def.base_def.def_id();
let node_id = self.cx.tcx.map.as_local_node_id(def_id).unwrap();

let extent = self.cx.tcx.region_maps.var_scope(node_id);
self.indexed.insert(seqvar.segments[0].name, Some(extent));
return; // no need to walk further
}
Expand Down Expand Up @@ -1040,7 +1043,8 @@ impl<'v, 't> Visitor<'v> for InitializeVisitor<'v, 't> {

fn var_def_id(cx: &LateContext, expr: &Expr) -> Option<NodeId> {
if let Some(path_res) = cx.tcx.def_map.borrow().get(&expr.id) {
if let Def::Local(_, node_id) = path_res.base_def {
if let Def::Local(def_id) = path_res.base_def {
let node_id = cx.tcx.map.as_local_node_id(def_id).expect("That DefId should be valid");
return Some(node_id);
}
}
Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,9 @@ fn in_attributes_expansion(cx: &LateContext, expr: &Expr) -> bool {
/// Test whether `def` is a variable defined outside a macro.
fn non_macro_local(cx: &LateContext, def: &def::Def) -> bool {
match *def {
def::Def::Local(_, id) | def::Def::Upvar(_, id, _, _) => {
def::Def::Local(id) | def::Def::Upvar(id, _, _) => {
let id = cx.tcx.map.as_local_node_id(id).expect("That DefId should be valid");

if let Some(span) = cx.tcx.map.opt_span(id) {
!in_macro(cx, span)
} else {
Expand Down
54 changes: 31 additions & 23 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,21 @@ declare_lint! {
fn check_let_unit(cx: &LateContext, decl: &Decl) {
if let DeclLocal(ref local) = decl.node {
let bindtype = &cx.tcx.pat_ty(&local.pat).sty;
if *bindtype == ty::TyTuple(&[]) {
if in_external_macro(cx, decl.span) || in_macro(cx, local.pat.span) {
return;
}
if higher::is_from_for_desugar(decl) {
return;
match *bindtype {
ty::TyTuple(slice) if slice.is_empty() => {
if in_external_macro(cx, decl.span) || in_macro(cx, local.pat.span) {
return;
}
if higher::is_from_for_desugar(decl) {
return;
}
span_lint(cx,
LET_UNIT_VALUE,
decl.span,
&format!("this let-binding has unit value. Consider omitting `let {} =`",
snippet(cx, local.pat.span, "..")));
}
span_lint(cx,
LET_UNIT_VALUE,
decl.span,
&format!("this let-binding has unit value. Consider omitting `let {} =`",
snippet(cx, local.pat.span, "..")));
_ => (),
}
}
}
Expand Down Expand Up @@ -193,18 +196,23 @@ impl LateLintPass for UnitCmp {
}
if let ExprBinary(ref cmp, ref left, _) = expr.node {
let op = cmp.node;
let sty = &cx.tcx.expr_ty(left).sty;
if *sty == ty::TyTuple(&[]) && op.is_comparison() {
let result = match op {
BiEq | BiLe | BiGe => "true",
_ => "false",
};
span_lint(cx,
UNIT_CMP,
expr.span,
&format!("{}-comparison of unit values detected. This will always be {}",
op.as_str(),
result));
if op.is_comparison() {
let sty = &cx.tcx.expr_ty(left).sty;
match *sty {
ty::TyTuple(slice) if slice.is_empty() => {
let result = match op {
BiEq | BiLe | BiGe => "true",
_ => "false",
};
span_lint(cx,
UNIT_CMP,
expr.span,
&format!("{}-comparison of unit values detected. This will always be {}",
op.as_str(),
result));
}
_ => ()
}
}
}
}
Expand Down
20 changes: 7 additions & 13 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use reexport::*;
use rustc::hir::*;
use rustc::hir::def_id::DefId;
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc::hir::map::Node;
use rustc::lint::{LintContext, LateContext, Level, Lint};
use rustc::middle::cstore;
use rustc::session::Session;
use rustc::traits::Reveal;
use rustc::traits;
Expand Down Expand Up @@ -216,13 +215,14 @@ pub fn match_path_ast(path: &ast::Path, segments: &[&str]) -> bool {

/// Get the definition associated to a path.
/// TODO: investigate if there is something more efficient for that.
pub fn path_to_def(cx: &LateContext, path: &[&str]) -> Option<cstore::DefLike> {
pub fn path_to_def(cx: &LateContext, path: &[&str]) -> Option<def::Def> {
let cstore = &cx.tcx.sess.cstore;

let crates = cstore.crates();
let krate = crates.iter().find(|&&krate| cstore.crate_name(krate) == path[0]);
if let Some(krate) = krate {
let mut items = cstore.crate_top_level_items(*krate);
let krate = DefId { krate: *krate, index: CRATE_DEF_INDEX };
let mut items = cstore.item_children(krate);
let mut path_it = path.iter().skip(1).peekable();

loop {
Expand All @@ -234,16 +234,10 @@ pub fn path_to_def(cx: &LateContext, path: &[&str]) -> Option<cstore::DefLike> {
for item in &mem::replace(&mut items, vec![]) {
if item.name.as_str() == *segment {
if path_it.peek().is_none() {
return Some(item.def);
return cx.tcx.sess.cstore.describe_def(item.def_id);
}

let def_id = match item.def {
cstore::DefLike::DlDef(def) => def.def_id(),
cstore::DefLike::DlImpl(def_id) => def_id,
_ => panic!("Unexpected {:?}", item.def),
};

items = cstore.item_children(def_id);
items = cstore.item_children(item.def_id);
break;
}
}
Expand All @@ -261,7 +255,7 @@ pub fn get_trait_def_id(cx: &LateContext, path: &[&str]) -> Option<DefId> {
};

match def {
cstore::DlDef(def::Def::Trait(trait_id)) => Some(trait_id),
def::Def::Trait(trait_id) => Some(trait_id),
_ => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate syntax;
use clippy_lints::consts::{constant_simple, Constant, FloatWidth};
use rustc_const_math::ConstInt;
use rustc::hir::*;
use syntax::ast::{LitIntType, LitKind, StrStyle};
use syntax::ast::{LitIntType, LitKind, NodeId, StrStyle};
use syntax::codemap::{Spanned, COMMAND_LINE_SP};
use syntax::parse::token::InternedString;
use syntax::ptr::P;
Expand All @@ -24,7 +24,7 @@ fn spanned<T>(t: T) -> Spanned<T> {

fn expr(n: Expr_) -> Expr {
Expr {
id: 1,
id: NodeId::new(1),
node: n,
span: COMMAND_LINE_SP,
attrs: ThinVec::new(),
Expand Down

0 comments on commit e877de1

Please sign in to comment.