fixed span position and README
This commit is contained in:
parent
3d85cc24e7
commit
a14514f7c8
|
@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
|
|||
[Jump to usage instructions](#usage)
|
||||
|
||||
##Lints
|
||||
There are 111 lints included in this crate:
|
||||
There are 110 lints included in this crate:
|
||||
|
||||
name | default | meaning
|
||||
---------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
@ -86,7 +86,6 @@ name
|
|||
[range_zip_with_len](https://github.com/Manishearth/rust-clippy/wiki#range_zip_with_len) | warn | zipping iterator with a range when enumerate() would do
|
||||
[redundant_closure](https://github.com/Manishearth/rust-clippy/wiki#redundant_closure) | warn | using redundant closures, i.e. `|a| foo(a)` (which can be written as just `foo`)
|
||||
[redundant_pattern](https://github.com/Manishearth/rust-clippy/wiki#redundant_pattern) | warn | using `name @ _` in a pattern
|
||||
[regex_macro](https://github.com/Manishearth/rust-clippy/wiki#regex_macro) | allow | finds use of `regex!(_)`, suggests `Regex::new(_)` instead
|
||||
[result_unwrap_used](https://github.com/Manishearth/rust-clippy/wiki#result_unwrap_used) | allow | using `Result.unwrap()`, which might be better handled
|
||||
[reverse_range_loop](https://github.com/Manishearth/rust-clippy/wiki#reverse_range_loop) | warn | Iterating over an empty range, such as `10..0` or `5..5`
|
||||
[search_is_some](https://github.com/Manishearth/rust-clippy/wiki#search_is_some) | warn | using an iterator search followed by `is_some()`, which is more succinctly expressed as a call to `any()`
|
||||
|
|
|
@ -167,6 +167,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
shadow::SHADOW_REUSE,
|
||||
shadow::SHADOW_SAME,
|
||||
shadow::SHADOW_UNRELATED,
|
||||
strings::STRING_ADD,
|
||||
strings::STRING_ADD_ASSIGN,
|
||||
types::CAST_POSSIBLE_TRUNCATION,
|
||||
types::CAST_POSSIBLE_WRAP,
|
||||
|
|
56
src/regex.rs
56
src/regex.rs
|
@ -1,6 +1,8 @@
|
|||
use regex_syntax;
|
||||
use std::error::Error;
|
||||
use syntax::codemap::{Span, BytePos, Pos};
|
||||
use syntax::ast::Lit_::LitStr;
|
||||
use syntax::codemap::{Span, BytePos};
|
||||
use syntax::parse::token::InternedString;
|
||||
use rustc_front::hir::*;
|
||||
use rustc::middle::const_eval::{eval_const_expr_partial, ConstVal};
|
||||
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
|
||||
|
@ -35,19 +37,47 @@ impl LateLintPass for RegexPass {
|
|||
if_let_chain!{[
|
||||
let ExprCall(ref fun, ref args) = expr.node,
|
||||
let ExprPath(_, ref path) = fun.node,
|
||||
match_path(path, ®EX_NEW_PATH) && args.len() == 1,
|
||||
let Ok(ConstVal::Str(r)) = eval_const_expr_partial(cx.tcx,
|
||||
&*args[0],
|
||||
ExprTypeChecked,
|
||||
None),
|
||||
let Err(e) = regex_syntax::Expr::parse(&r)
|
||||
match_path(path, ®EX_NEW_PATH) && args.len() == 1
|
||||
], {
|
||||
let lo = args[0].span.lo + BytePos::from_usize(e.position());
|
||||
let span = Span{ lo: lo, hi: lo, expn_id: args[0].span.expn_id };
|
||||
span_lint(cx,
|
||||
INVALID_REGEX,
|
||||
span,
|
||||
&format!("Regex syntax error: {}", e.description()));
|
||||
if let ExprLit(ref lit) = args[0].node {
|
||||
if let LitStr(ref r, _) = lit.node {
|
||||
if let Err(e) = regex_syntax::Expr::parse(r) {
|
||||
span_lint(cx,
|
||||
INVALID_REGEX,
|
||||
str_span(args[0].span, &r, e.position()),
|
||||
&format!("Regex syntax error: {}",
|
||||
e.description()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if_let_chain!{[
|
||||
let Some(r) = const_str(cx, &*args[0]),
|
||||
let Err(e) = regex_syntax::Expr::parse(&r)
|
||||
], {
|
||||
span_lint(cx,
|
||||
INVALID_REGEX,
|
||||
args[0].span,
|
||||
&format!("Regex syntax error on position {}: {}",
|
||||
e.position(),
|
||||
e.description()));
|
||||
}}
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(cast_possible_truncation)]
|
||||
fn str_span(base: Span, s: &str, c: usize) -> Span {
|
||||
let lo = match s.char_indices().nth(c) {
|
||||
Some((b, _)) => base.lo + BytePos(b as u32),
|
||||
_ => base.hi
|
||||
};
|
||||
Span{ lo: lo, hi: lo, ..base }
|
||||
}
|
||||
|
||||
fn const_str(cx: &LateContext, e: &Expr) -> Option<InternedString> {
|
||||
match eval_const_expr_partial(cx.tcx, e, ExprTypeChecked, None) {
|
||||
Ok(ConstVal::Str(r)) => Some(r),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,9 +8,17 @@ extern crate regex;
|
|||
|
||||
use regex::Regex;
|
||||
|
||||
const OPENING_PAREN : &'static str = "(";
|
||||
|
||||
fn main() {
|
||||
let pipe_in_wrong_position = Regex::new("|");
|
||||
//~^ERROR: Regex syntax error: empty alternate
|
||||
let wrong_char_range = Regex::new("[z-a]");
|
||||
let wrong_char_ranice = Regex::new("[z-a]");
|
||||
//~^ERROR: Regex syntax error: invalid character class range
|
||||
|
||||
let some_regex = Regex::new(OPENING_PAREN);
|
||||
//~^ERROR: Regex syntax error on position 0: unclosed
|
||||
|
||||
let closing_paren = ")";
|
||||
let not_linted = Regex::new(closing_paren);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue