From cd2e621c60b897a2f37fab36bc357da30aa9cc54 Mon Sep 17 00:00:00 2001 From: llogiq Date: Tue, 26 May 2015 13:52:40 +0200 Subject: [PATCH] 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 --- Cargo.toml | 1 + src/mut_mut.rs | 14 ++++++++++---- tests/run-pass.rs | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3c7d9be64e3..e1308fb9cb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ plugin = true compiletest_rs = "*" regex = "*" regex_macros = "*" +lazy_static = "*" diff --git a/src/mut_mut.rs b/src/mut_mut.rs index 160b99a1d15..a4c2d3932a3 100644 --- a/src/mut_mut.rs +++ b/src/mut_mut.rs @@ -2,7 +2,7 @@ use syntax::ptr::P; use syntax::ast::*; use rustc::lint::{Context, LintPass, LintArray, Lint}; 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, "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>) { - if in_macro(info) { return; } + if in_macro(cx, info) { return; } fn unwrap_addr(expr : &Expr) -> Option<&Expr> { 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 { - info.is_some() +fn in_macro(cx: &Context, opt_info: Option<&ExpnInfo>) -> bool { + 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> { diff --git a/tests/run-pass.rs b/tests/run-pass.rs index bc39278606c..32fdea3a340 100644 --- a/tests/run-pass.rs +++ b/tests/run-pass.rs @@ -1,11 +1,31 @@ #![feature(plugin)] #![plugin(clippy, regex_macros)] +#[macro_use] +extern crate lazy_static; extern crate regex; +use std::collections::HashMap; + #[test] #[deny(mut_mut)] fn test_regex() { let pattern = regex!(r"^(?P[#]+)\s(?P.+)$"); 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); +}