From 0d651c72ff4c8833046dbdad0049893fbc57bef2 Mon Sep 17 00:00:00 2001 From: llogiq Date: Tue, 26 May 2015 01:45:15 +0200 Subject: [PATCH 1/2] made macro test even simpler, added a few tests --- Cargo.toml | 2 ++ src/lib.rs | 1 - src/mut_mut.rs | 6 +++--- src/ptr_arg.rs | 2 +- tests/compile-fail/mut_mut.rs | 9 +++++++++ tests/compile-test.rs | 2 +- tests/run-pass.rs | 11 +++++++++++ 7 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 tests/run-pass.rs diff --git a/Cargo.toml b/Cargo.toml index 95de98e8e..3c7d9be64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,5 @@ plugin = true [dev-dependencies] compiletest_rs = "*" +regex = "*" +regex_macros = "*" diff --git a/src/lib.rs b/src/lib.rs index cf5def800..92cc7ef25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ #![feature(plugin_registrar, box_syntax)] #![feature(rustc_private, collections)] - #![allow(unused_imports)] #[macro_use] diff --git a/src/mut_mut.rs b/src/mut_mut.rs index b1e21def5..160b99a1d 100644 --- a/src/mut_mut.rs +++ b/src/mut_mut.rs @@ -27,7 +27,7 @@ impl LintPass for MutMut { } fn check_expr_expd(cx: &Context, expr: &Expr, info: Option<&ExpnInfo>) { - if in_external_macro(info) { return; } + if in_macro(info) { return; } fn unwrap_addr(expr : &Expr) -> Option<&Expr> { match expr.node { @@ -51,8 +51,8 @@ fn check_expr_expd(cx: &Context, expr: &Expr, info: Option<&ExpnInfo>) { }) } -fn in_external_macro(info: Option<&ExpnInfo>) -> bool { - info.map_or(false, |i| i.callee.span.is_some()) +fn in_macro(info: Option<&ExpnInfo>) -> bool { + info.is_some() } fn unwrap_mut(ty : &Ty) -> Option<&Ty> { diff --git a/src/ptr_arg.rs b/src/ptr_arg.rs index 86b87a942..64c3c84c7 100644 --- a/src/ptr_arg.rs +++ b/src/ptr_arg.rs @@ -27,7 +27,7 @@ impl LintPass for PtrArg { } fn check_item(&mut self, cx: &Context, item: &Item) { - if let &ItemFn(ref decl, _, _, _, _) = &item.node { + if let &ItemFn(ref decl, _, _, _, _, _) = &item.node { check_fn(cx, decl); } } diff --git a/tests/compile-fail/mut_mut.rs b/tests/compile-fail/mut_mut.rs index 65e3762e2..d7adc0677 100644 --- a/tests/compile-fail/mut_mut.rs +++ b/tests/compile-fail/mut_mut.rs @@ -1,11 +1,18 @@ #![feature(plugin)] #![plugin(clippy)] +//#![plugin(regex_macros)] +//extern crate regex; + #[deny(mut_mut)] fn fun(x : &mut &mut u32) -> bool { //~ERROR **x > 0 } +macro_rules! mut_ptr { + ($p:expr) => { &mut $p } +} + #[deny(mut_mut)] #[allow(unused_mut, unused_variables)] fn main() { @@ -22,4 +29,6 @@ fn main() { //~^^^^ ERROR ***y + **x; } + + let mut z = mut_ptr!(&mut 3u32); //~ERROR } diff --git a/tests/compile-test.rs b/tests/compile-test.rs index 04f3fc16b..6fcf71d38 100644 --- a/tests/compile-test.rs +++ b/tests/compile-test.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; fn run_mode(mode: &'static str) { let mut config = compiletest::default_config(); let cfg_mode = mode.parse().ok().expect("Invalid mode"); - config.target_rustcflags = Some("-L target/debug/".to_string()); + config.target_rustcflags = Some("-l regex_macros -L target/debug/".to_string()); config.mode = cfg_mode; config.src_base = PathBuf::from(format!("tests/{}", mode)); diff --git a/tests/run-pass.rs b/tests/run-pass.rs new file mode 100644 index 000000000..bc3927860 --- /dev/null +++ b/tests/run-pass.rs @@ -0,0 +1,11 @@ +#![feature(plugin)] +#![plugin(clippy, regex_macros)] + +extern crate regex; + +#[test] +#[deny(mut_mut)] +fn test_regex() { + let pattern = regex!(r"^(?P[#]+)\s(?P.+)$"); + assert!(pattern.is_match("# headline")); +} From cd2e621c60b897a2f37fab36bc357da30aa9cc54 Mon Sep 17 00:00:00 2001 From: llogiq <bogusandre@gmail.com> Date: Tue, 26 May 2015 13:52:40 +0200 Subject: [PATCH 2/2] 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 3c7d9be64..e1308fb9c 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 160b99a1d..a4c2d3932 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 bc3927860..32fdea3a3 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<level>[#]+)\s(?P<title>.+)$"); 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); +}