aboutsummaryrefslogtreecommitdiffstats
path: root/components/macros/lib.rs
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2014-09-10 09:00:24 +0530
committerManish Goregaokar <manishsmail@gmail.com>2014-09-10 09:11:44 +0530
commitd765d65b7a3afa630f1d0c084b5dc94deca541da (patch)
tree63c2a751e980e5c4bae8efaea7f3fe7cd40f11c4 /components/macros/lib.rs
parentb1305bb7d051f83850c51bb0da0ccc86a5e07922 (diff)
downloadservo-d765d65b7a3afa630f1d0c084b5dc94deca541da.tar.gz
servo-d765d65b7a3afa630f1d0c084b5dc94deca541da.zip
Add lint for type-checking transmutes
Diffstat (limited to 'components/macros/lib.rs')
-rw-r--r--components/macros/lib.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/components/macros/lib.rs b/components/macros/lib.rs
index 20304ffe806..ec6b0ebed47 100644
--- a/components/macros/lib.rs
+++ b/components/macros/lib.rs
@@ -6,9 +6,59 @@
//! Exports macros for use in other Servo crates.
+extern crate syntax;
+
+#[phase(plugin, link)]
+extern crate rustc;
#[cfg(test)]
extern crate sync;
+use syntax::ast;
+use syntax::parse::token;
+use rustc::lint::{Context, LintPass, LintPassObject, LintArray};
+use rustc::plugin::Registry;
+use rustc::middle::ty::{expr_ty, get};
+use rustc::middle::typeck::astconv::AstConv;
+use rustc::util::ppaux::Repr;
+
+declare_lint!(TRANSMUTE_TYPE_LINT, Allow,
+ "Warn and report types being transmuted")
+
+struct Pass;
+
+impl LintPass for Pass {
+ fn get_lints(&self) -> LintArray {
+ lint_array!(TRANSMUTE_TYPE_LINT)
+ }
+
+ fn check_expr(&mut self, cx: &Context, ex: &ast::Expr) {
+ match ex.node {
+ ast::ExprCall(ref expr, ref args) => {
+ match expr.node {
+ ast::ExprPath(ref path) => {
+ if path.segments.last()
+ .map_or(false, |ref segment| segment.identifier.name.as_str() == "transmute")
+ && args.len() == 1 {
+ let tcx = cx.tcx();
+ cx.span_lint(TRANSMUTE_TYPE_LINT, ex.span,
+ format!("Transmute from {} to {} detected",
+ expr_ty(tcx, ex).repr(tcx),
+ expr_ty(tcx, &**args.get(0)).repr(tcx)
+ ).as_slice());
+ }
+ }
+ _ => {}
+ }
+ }
+ _ => {}
+ }
+ }
+}
+
+#[plugin_registrar]
+pub fn plugin_registrar(reg: &mut Registry) {
+ reg.register_lint_pass(box Pass as LintPassObject);
+}
#[macro_export]
macro_rules! bitfield(