diff options
Diffstat (limited to 'components/plugins')
-rw-r--r-- | components/plugins/lib.rs | 17 | ||||
-rw-r--r-- | components/plugins/lints/inheritance_integrity.rs | 27 | ||||
-rw-r--r-- | components/plugins/lints/transmute_type.rs | 8 | ||||
-rw-r--r-- | components/plugins/lints/unrooted_must_root.rs | 25 | ||||
-rw-r--r-- | components/plugins/utils.rs | 29 |
5 files changed, 51 insertions, 55 deletions
diff --git a/components/plugins/lib.rs b/components/plugins/lib.rs index 5ebb54aba31..4fd80252c34 100644 --- a/components/plugins/lib.rs +++ b/components/plugins/lib.rs @@ -29,7 +29,7 @@ extern crate syntax; use rustc_plugin::Registry; use syntax::ext::base::*; use syntax::feature_gate::AttributeType::Whitelisted; -use syntax::parse::token::intern; +use syntax::symbol::Symbol; // Public for documentation to show up /// Handles the auto-deriving for `#[derive(JSTraceable)]` @@ -42,17 +42,24 @@ mod utils; #[plugin_registrar] pub fn plugin_registrar(reg: &mut Registry) { - reg.register_syntax_extension(intern("dom_struct"), MultiModifier(box jstraceable::expand_dom_struct)); - reg.register_syntax_extension(intern("_generate_reflector"), - MultiDecorator(box reflector::expand_reflector)); + reg.register_syntax_extension( + Symbol::intern("dom_struct"), + MultiModifier(box jstraceable::expand_dom_struct)); + + reg.register_syntax_extension( + Symbol::intern("_generate_reflector"), + MultiDecorator(box reflector::expand_reflector)); + reg.register_late_lint_pass(box lints::unrooted_must_root::UnrootedPass::new()); reg.register_late_lint_pass(box lints::privatize::PrivatizePass); reg.register_late_lint_pass(box lints::inheritance_integrity::InheritancePass); reg.register_late_lint_pass(box lints::transmute_type::TransmutePass); reg.register_early_lint_pass(box lints::ban::BanPass); + reg.register_attribute("_dom_struct_marker".to_string(), Whitelisted); + reg.register_attribute("allow_unrooted_interior".to_string(), Whitelisted); reg.register_attribute("must_root".to_string(), Whitelisted); + reg.register_attribute("privatize".to_string(), Whitelisted); reg.register_attribute("servo_lang".to_string(), Whitelisted); - reg.register_attribute("allow_unrooted_interior".to_string(), Whitelisted); register_clippy(reg); } diff --git a/components/plugins/lints/inheritance_integrity.rs b/components/plugins/lints/inheritance_integrity.rs index 96a93dec809..a47b6313db0 100644 --- a/components/plugins/lints/inheritance_integrity.rs +++ b/components/plugins/lints/inheritance_integrity.rs @@ -44,22 +44,19 @@ impl LateLintPass for InheritancePass { .map(|(_, f)| f.span); // Find all #[dom_struct] fields let dom_spans: Vec<_> = def.fields().iter().enumerate().filter_map(|(ctr, f)| { - if let hir::TyPath(..) = f.ty.node { - if let Some(&def::PathResolution { base_def: def, .. }) = - cx.tcx.def_map.borrow().get(&f.ty.id) { - if let def::Def::PrimTy(_) = def { - return None; - } - if cx.tcx.has_attr(def.def_id(), "_dom_struct_marker") { - // If the field is not the first, it's probably - // being misused (a) - if ctr > 0 { - cx.span_lint(INHERITANCE_INTEGRITY, f.span, - "Bare DOM structs should only be used as the first field of a \ - DOM struct. Consider using JS<T> instead."); - } - return Some(f.span) + if let hir::TyPath(hir::QPath::Resolved(_, ref path)) = f.ty.node { + if let def::Def::PrimTy(_) = path.def { + return None; + } + if cx.tcx.has_attr(path.def.def_id(), "_dom_struct_marker") { + // If the field is not the first, it's probably + // being misused (a) + if ctr > 0 { + cx.span_lint(INHERITANCE_INTEGRITY, f.span, + "Bare DOM structs should only be used as the first field of a \ + DOM struct. Consider using JS<T> instead."); } + return Some(f.span) } } None diff --git a/components/plugins/lints/transmute_type.rs b/components/plugins/lints/transmute_type.rs index cc7acf5c64f..6cc1b3818a6 100644 --- a/components/plugins/lints/transmute_type.rs +++ b/components/plugins/lints/transmute_type.rs @@ -25,15 +25,15 @@ impl LateLintPass for TransmutePass { match ex.node { hir::ExprCall(ref expr, ref args) => { match expr.node { - hir::ExprPath(_, ref path) => { + hir::ExprPath(hir::QPath::Resolved(_, ref path)) => { if path.segments.last() - .map_or(false, |ref segment| segment.name.as_str() == "transmute") && + .map_or(false, |ref segment| &*segment.name.as_str() == "transmute") && args.len() == 1 { let tcx = cx.tcx; cx.span_lint(TRANSMUTE_TYPE_LINT, ex.span, &format!("Transmute to {:?} from {:?} detected", - tcx.expr_ty(ex), - tcx.expr_ty(&**args.get(0).unwrap()) + tcx.tables().expr_ty(ex), + tcx.tables().expr_ty(&args.get(0).unwrap()) )); } } diff --git a/components/plugins/lints/unrooted_must_root.rs b/components/plugins/lints/unrooted_must_root.rs index 351817f2310..a2ce68c522b 100644 --- a/components/plugins/lints/unrooted_must_root.rs +++ b/components/plugins/lints/unrooted_must_root.rs @@ -90,7 +90,8 @@ impl LateLintPass for UnrootedPass { }; if item.attrs.iter().all(|a| !a.check_name("must_root")) { for ref field in def.fields() { - if is_unrooted_ty(cx, cx.tcx.node_id_to_type(field.id), false) { + let def_id = cx.tcx.map.local_def_id(field.id); + if is_unrooted_ty(cx, cx.tcx.item_type(def_id), false) { cx.span_lint(UNROOTED_MUST_ROOT, field.span, "Type must be rooted, use #[must_root] on the struct definition to propagate") } @@ -105,7 +106,8 @@ impl LateLintPass for UnrootedPass { match var.node.data { hir::VariantData::Tuple(ref fields, _) => { for ref field in fields { - if is_unrooted_ty(cx, cx.tcx.node_id_to_type(field.id), false) { + let def_id = cx.tcx.map.local_def_id(field.id); + if is_unrooted_ty(cx, cx.tcx.item_type(def_id), false) { cx.span_lint(UNROOTED_MUST_ROOT, field.ty.span, "Type must be rooted, use #[must_root] on \ the enum definition to propagate") @@ -118,17 +120,18 @@ impl LateLintPass for UnrootedPass { } /// Function arguments that are #[must_root] types are not allowed fn check_fn(&mut self, cx: &LateContext, kind: visit::FnKind, decl: &hir::FnDecl, - block: &hir::Block, span: codemap::Span, id: ast::NodeId) { + body: &hir::Expr, span: codemap::Span, id: ast::NodeId) { let in_new_function = match kind { visit::FnKind::ItemFn(n, _, _, _, _, _, _) | visit::FnKind::Method(n, _, _, _) => { - n.as_str() == "new" || n.as_str().starts_with("new_") + &*n.as_str() == "new" || n.as_str().starts_with("new_") } visit::FnKind::Closure(_) => return, }; if !in_derive_expn(cx, span) { - let ty = cx.tcx.node_id_to_type(id); + let def_id = cx.tcx.map.local_def_id(id); + let ty = cx.tcx.item_type(def_id); for (arg, ty) in decl.inputs.iter().zip(ty.fn_args().0.iter()) { if is_unrooted_ty(cx, ty, false) { @@ -147,7 +150,7 @@ impl LateLintPass for UnrootedPass { cx: cx, in_new_function: in_new_function, }; - visit::walk_block(&mut visitor, block); + visit::walk_expr(&mut visitor, body); } } @@ -161,7 +164,7 @@ impl<'a, 'b: 'a, 'tcx: 'a+'b> visit::Visitor<'a> for FnDefVisitor<'a, 'b, 'tcx> let cx = self.cx; fn require_rooted(cx: &LateContext, in_new_function: bool, subexpr: &hir::Expr) { - let ty = cx.tcx.expr_ty(&*subexpr); + let ty = cx.tcx.tables().expr_ty(&subexpr); if is_unrooted_ty(cx, ty, in_new_function) { cx.span_lint(UNROOTED_MUST_ROOT, subexpr.span, @@ -194,8 +197,8 @@ impl<'a, 'b: 'a, 'tcx: 'a+'b> visit::Visitor<'a> for FnDefVisitor<'a, 'b, 'tcx> fn visit_pat(&mut self, pat: &'a hir::Pat) { let cx = self.cx; - if let hir::PatKind::Binding(hir::BindingMode::BindByValue(_), _, _) = pat.node { - let ty = cx.tcx.pat_ty(pat); + if let hir::PatKind::Binding(hir::BindingMode::BindByValue(_), _, _, _) = pat.node { + let ty = cx.tcx.tables().pat_ty(pat); if is_unrooted_ty(cx, ty, self.in_new_function) { cx.span_lint(UNROOTED_MUST_ROOT, pat.span, @@ -207,9 +210,9 @@ impl<'a, 'b: 'a, 'tcx: 'a+'b> visit::Visitor<'a> for FnDefVisitor<'a, 'b, 'tcx> } fn visit_fn(&mut self, kind: visit::FnKind<'a>, decl: &'a hir::FnDecl, - block: &'a hir::Block, span: codemap::Span, id: ast::NodeId) { + body: &'a hir::Expr, span: codemap::Span, id: ast::NodeId) { if let visit::FnKind::Closure(_) = kind { - visit::walk_fn(self, kind, decl, block, span, id); + visit::walk_fn(self, kind, decl, body, span, id); } } diff --git a/components/plugins/utils.rs b/components/plugins/utils.rs index 186236aa458..f0d8a0edea8 100644 --- a/components/plugins/utils.rs +++ b/components/plugins/utils.rs @@ -21,7 +21,7 @@ pub fn match_ty_unwrap<'a>(ty: &'a ast::Ty, segments: &[&str]) -> Option<&'a [P< // I could muck around with the maps and find the full path // however the more efficient way is to simply reverse the iterators and zip them // which will compare them in reverse until one of them runs out of segments - if seg.iter().rev().zip(segments.iter().rev()).all(|(a, b)| a.identifier.name.as_str() == *b) { + if seg.iter().rev().zip(segments.iter().rev()).all(|(a, b)| &*a.identifier.name.as_str() == *b) { match seg.last() { Some(&ast::PathSegment { parameters: ast::PathParameters::AngleBracketed(ref a), .. }) => { Some(&a.types) @@ -38,13 +38,8 @@ pub fn match_ty_unwrap<'a>(ty: &'a ast::Ty, segments: &[&str]) -> Option<&'a [P< /// Checks if a type has a #[servo_lang = "str"] attribute pub fn match_lang_ty(cx: &LateContext, ty: &hir::Ty, value: &str) -> bool { - match ty.node { - hir::TyPath(..) => {}, - _ => return false, - } - - let def = match cx.tcx.def_map.borrow().get(&ty.id) { - Some(&def::PathResolution { base_def: def, .. }) => def, + let def = match ty.node { + hir::TyPath(hir::QPath::Resolved(_, ref path)) => path.def, _ => return false, }; @@ -57,17 +52,11 @@ pub fn match_lang_ty(cx: &LateContext, ty: &hir::Ty, value: &str) -> bool { pub fn match_lang_did(cx: &LateContext, did: DefId, value: &str) -> bool { cx.tcx.get_attrs(did).iter().any(|attr| { - match attr.node.value.node { - ast::MetaItemKind::NameValue(ref name, ref val) if &**name == "servo_lang" => { - match val.node { - ast::LitKind::Str(ref v, _) if &**v == value => { - mark_used(attr); - true - }, - _ => false, - } - } - _ => false, + if attr.check_name("servo_lang") && attr.value_str().map_or(false, |v| v == value) { + mark_used(attr); + true + } else { + false } }) } @@ -91,7 +80,7 @@ pub fn match_def_path(cx: &LateContext, def_id: DefId, path: &[&str]) -> bool { other.into_iter() .map(|e| e.data) .zip(path) - .all(|(nm, p)| nm.as_interned_str() == *p) + .all(|(nm, p)| &*nm.as_interned_str() == *p) } pub fn in_derive_expn(cx: &LateContext, span: Span) -> bool { |