made in_macro distinguish intra-crate and extra-crate macros, as the latter have no working source (note: may fail in the face of compiler plugins doing whatever they like with spans), also one more run-pass test

This commit is contained in:
llogiq 2015-05-26 13:52:40 +02:00
parent 0d651c72ff
commit cd2e621c60
3 changed files with 31 additions and 4 deletions

View File

@ -19,3 +19,4 @@ plugin = true
compiletest_rs = "*" compiletest_rs = "*"
regex = "*" regex = "*"
regex_macros = "*" regex_macros = "*"
lazy_static = "*"

View File

@ -2,7 +2,7 @@ use syntax::ptr::P;
use syntax::ast::*; use syntax::ast::*;
use rustc::lint::{Context, LintPass, LintArray, Lint}; use rustc::lint::{Context, LintPass, LintArray, Lint};
use rustc::middle::ty::{expr_ty, sty, ty_ptr, ty_rptr, mt}; use rustc::middle::ty::{expr_ty, sty, ty_ptr, ty_rptr, mt};
use syntax::codemap::ExpnInfo; use syntax::codemap::{BytePos, ExpnInfo, MacroFormat, Span};
declare_lint!(pub MUT_MUT, Warn, declare_lint!(pub MUT_MUT, Warn,
"Warn on usage of double-mut refs, e.g. '&mut &mut ...'"); "Warn on usage of double-mut refs, e.g. '&mut &mut ...'");
@ -27,7 +27,7 @@ impl LintPass for MutMut {
} }
fn check_expr_expd(cx: &Context, expr: &Expr, info: Option<&ExpnInfo>) { fn check_expr_expd(cx: &Context, expr: &Expr, info: Option<&ExpnInfo>) {
if in_macro(info) { return; } if in_macro(cx, info) { return; }
fn unwrap_addr(expr : &Expr) -> Option<&Expr> { fn unwrap_addr(expr : &Expr) -> Option<&Expr> {
match expr.node { match expr.node {
@ -51,8 +51,14 @@ fn check_expr_expd(cx: &Context, expr: &Expr, info: Option<&ExpnInfo>) {
}) })
} }
fn in_macro(info: Option<&ExpnInfo>) -> bool { fn in_macro(cx: &Context, opt_info: Option<&ExpnInfo>) -> bool {
info.is_some() opt_info.map_or(false, |info| {
info.callee.span.map_or(true, |span| {
cx.sess().codemap().span_to_snippet(span).ok().map_or(true, |code|
!code.starts_with("macro_rules")
)
})
})
} }
fn unwrap_mut(ty : &Ty) -> Option<&Ty> { fn unwrap_mut(ty : &Ty) -> Option<&Ty> {

View File

@ -1,11 +1,31 @@
#![feature(plugin)] #![feature(plugin)]
#![plugin(clippy, regex_macros)] #![plugin(clippy, regex_macros)]
#[macro_use]
extern crate lazy_static;
extern crate regex; extern crate regex;
use std::collections::HashMap;
#[test] #[test]
#[deny(mut_mut)] #[deny(mut_mut)]
fn test_regex() { fn test_regex() {
let pattern = regex!(r"^(?P<level>[#]+)\s(?P<title>.+)$"); let pattern = regex!(r"^(?P<level>[#]+)\s(?P<title>.+)$");
assert!(pattern.is_match("# headline")); assert!(pattern.is_match("# headline"));
} }
#[test]
#[deny(mut_mut)]
#[allow(unused_variables, unused_mut)]
fn test_lazy_static() {
lazy_static! {
static ref MUT_MAP : HashMap<usize, &'static str> = {
let mut m = HashMap::new();
let mut zero = &mut &mut "zero";
m.insert(0, "zero");
m
};
static ref MUT_COUNT : usize = MUT_MAP.len();
}
assert!(*MUT_COUNT == 1);
}