mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
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:
parent
0d651c72ff
commit
cd2e621c60
@ -19,3 +19,4 @@ plugin = true
|
||||
compiletest_rs = "*"
|
||||
regex = "*"
|
||||
regex_macros = "*"
|
||||
lazy_static = "*"
|
||||
|
@ -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> {
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user