2018-10-06 11:18:06 -05:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
2016-02-04 17:36:06 -06:00
|
|
|
use regex_syntax;
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::rustc::hir::*;
|
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
|
|
|
use crate::rustc_data_structures::fx::FxHashSet;
|
2018-07-19 03:00:54 -05:00
|
|
|
use if_chain::if_chain;
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::syntax::ast::{LitKind, NodeId, StrStyle};
|
|
|
|
use crate::syntax::source_map::{BytePos, Span};
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::{is_expn_of, match_def_path, match_type, opt_def_id, paths, span_help_and_lint, span_lint};
|
|
|
|
use crate::consts::{constant, Constant};
|
2018-10-08 23:40:21 -05:00
|
|
|
use std::convert::TryInto;
|
2016-02-04 17:36:06 -06:00
|
|
|
|
2017-09-01 15:43:34 -05:00
|
|
|
/// **What it does:** Checks [regex](https://crates.io/crates/regex) creation
|
|
|
|
/// (with `Regex::new`,`RegexBuilder::new` or `RegexSet::new`) for correct
|
|
|
|
/// regex syntax.
|
2016-07-15 17:25:44 -05:00
|
|
|
///
|
2016-02-04 17:36:06 -06:00
|
|
|
/// **Why is this bad?** This will lead to a runtime panic.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
2016-07-15 17:25:44 -05:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// Regex::new("|")
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2016-02-04 17:36:06 -06:00
|
|
|
pub INVALID_REGEX,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2016-08-06 03:18:36 -05:00
|
|
|
"invalid regular expressions"
|
2016-02-04 17:36:06 -06:00
|
|
|
}
|
|
|
|
|
2017-09-12 11:04:57 -05:00
|
|
|
/// **What it does:** Checks for trivial [regex](https://crates.io/crates/regex)
|
|
|
|
/// creation (with `Regex::new`, `RegexBuilder::new` or `RegexSet::new`).
|
2016-07-15 17:25:44 -05:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Why is this bad?** Matching the regex can likely be replaced by `==` or
|
|
|
|
/// `str::starts_with`, `str::ends_with` or `std::contains` or other `str`
|
|
|
|
/// methods.
|
2016-02-05 16:10:48 -06:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
2016-07-15 17:25:44 -05:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// Regex::new("^foobar")
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2016-02-05 16:10:48 -06:00
|
|
|
pub TRIVIAL_REGEX,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2016-08-06 03:18:36 -05:00
|
|
|
"trivial regular expressions"
|
2016-02-05 16:10:48 -06:00
|
|
|
}
|
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for usage of `regex!(_)` which (as of now) is
|
|
|
|
/// usually slower than `Regex::new(_)` unless called in a loop (which is a bad
|
|
|
|
/// idea anyway).
|
2016-02-07 15:50:54 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Why is this bad?** Performance, at least for now. The macro version is
|
|
|
|
/// likely to catch up long-term, but for now the dynamic version is faster.
|
2016-02-07 15:50:54 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Known problems:** None.
|
2016-02-07 15:50:54 -06:00
|
|
|
///
|
2016-07-15 17:25:44 -05:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// regex!("foo|bar")
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2016-02-07 15:50:54 -06:00
|
|
|
pub REGEX_MACRO,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2016-08-06 03:18:36 -05:00
|
|
|
"use of `regex!(_)` instead of `Regex::new(_)`"
|
2016-02-07 15:50:54 -06:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:55:02 -06:00
|
|
|
#[derive(Clone, Default)]
|
2016-06-10 09:17:20 -05:00
|
|
|
pub struct Pass {
|
2018-09-11 18:34:52 -05:00
|
|
|
spans: FxHashSet<Span>,
|
2016-02-24 10:38:57 -06:00
|
|
|
last: Option<NodeId>,
|
2016-02-14 09:55:02 -06:00
|
|
|
}
|
2016-02-04 17:36:06 -06:00
|
|
|
|
2016-06-10 09:17:20 -05:00
|
|
|
impl LintPass for Pass {
|
2016-02-04 17:36:06 -06:00
|
|
|
fn get_lints(&self) -> LintArray {
|
2016-02-07 15:50:54 -06:00
|
|
|
lint_array!(INVALID_REGEX, REGEX_MACRO, TRIVIAL_REGEX)
|
2016-02-04 17:36:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|
|
|
fn check_crate(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx Crate) {
|
2016-02-14 09:55:02 -06:00
|
|
|
self.spans.clear();
|
2016-02-07 15:50:54 -06:00
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx Block) {
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
|
|
|
if self.last.is_none();
|
|
|
|
if let Some(ref expr) = block.expr;
|
|
|
|
if match_type(cx, cx.tables.expr_ty(expr), &paths::REGEX);
|
|
|
|
if let Some(span) = is_expn_of(expr.span, "regex");
|
|
|
|
then {
|
|
|
|
if !self.spans.contains(&span) {
|
|
|
|
span_lint(cx,
|
|
|
|
REGEX_MACRO,
|
|
|
|
span,
|
|
|
|
"`regex!(_)` found. \
|
|
|
|
Please use `Regex::new(_)`, which is faster for now.");
|
|
|
|
self.spans.insert(span);
|
|
|
|
}
|
|
|
|
self.last = Some(block.id);
|
2016-02-14 09:55:02 -06:00
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
2016-02-14 09:55:02 -06:00
|
|
|
}
|
2016-02-24 10:38:57 -06:00
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_block_post(&mut self, _: &LateContext<'a, 'tcx>, block: &'tcx Block) {
|
2016-02-14 09:55:02 -06:00
|
|
|
if self.last.map_or(false, |id| block.id == id) {
|
2016-02-24 10:38:57 -06:00
|
|
|
self.last = None;
|
2016-02-14 09:55:02 -06:00
|
|
|
}
|
|
|
|
}
|
2016-02-07 15:50:54 -06:00
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Call(ref fun, ref args) = expr.node;
|
|
|
|
if let ExprKind::Path(ref qpath) = fun.node;
|
2017-10-23 14:18:02 -05:00
|
|
|
if args.len() == 1;
|
|
|
|
if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, fun.hir_id));
|
|
|
|
then {
|
|
|
|
if match_def_path(cx.tcx, def_id, &paths::REGEX_NEW) ||
|
|
|
|
match_def_path(cx.tcx, def_id, &paths::REGEX_BUILDER_NEW) {
|
|
|
|
check_regex(cx, &args[0], true);
|
|
|
|
} else if match_def_path(cx.tcx, def_id, &paths::REGEX_BYTES_NEW) ||
|
|
|
|
match_def_path(cx.tcx, def_id, &paths::REGEX_BYTES_BUILDER_NEW) {
|
|
|
|
check_regex(cx, &args[0], false);
|
|
|
|
} else if match_def_path(cx.tcx, def_id, &paths::REGEX_SET_NEW) {
|
|
|
|
check_set(cx, &args[0], true);
|
|
|
|
} else if match_def_path(cx.tcx, def_id, &paths::REGEX_BYTES_SET_NEW) {
|
|
|
|
check_set(cx, &args[0], false);
|
|
|
|
}
|
2016-02-05 09:48:35 -06:00
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
2016-02-04 17:36:06 -06:00
|
|
|
}
|
|
|
|
}
|
2016-02-05 09:48:35 -06:00
|
|
|
|
2018-04-19 01:30:07 -05:00
|
|
|
fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
|
|
|
|
let offset = u32::from(offset);
|
2018-10-08 23:40:21 -05:00
|
|
|
let end = base.lo() + BytePos(
|
|
|
|
c.end
|
|
|
|
.offset
|
|
|
|
.try_into()
|
|
|
|
.ok()
|
|
|
|
.and_then(|o: u32| o.checked_add(offset))
|
|
|
|
.expect("offset too large"),
|
|
|
|
);
|
|
|
|
let start = base.lo() + BytePos(
|
|
|
|
c.start
|
|
|
|
.offset
|
|
|
|
.try_into()
|
|
|
|
.ok()
|
|
|
|
.and_then(|o: u32| o.checked_add(offset))
|
|
|
|
.expect("offset too large"),
|
|
|
|
);
|
2018-03-13 09:02:40 -05:00
|
|
|
assert!(start <= end);
|
|
|
|
Span::new(start, end, base.ctxt())
|
2016-02-05 09:48:35 -06:00
|
|
|
}
|
|
|
|
|
2018-03-13 05:38:11 -05:00
|
|
|
fn const_str<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) -> Option<String> {
|
2018-05-13 06:16:31 -05:00
|
|
|
constant(cx, cx.tables, e).and_then(|(c, _)| match c {
|
2018-03-13 05:38:11 -05:00
|
|
|
Constant::Str(s) => Some(s),
|
2016-02-24 10:38:57 -06:00
|
|
|
_ => None,
|
2018-03-13 05:38:11 -05:00
|
|
|
})
|
2016-02-05 09:48:35 -06:00
|
|
|
}
|
2016-02-05 16:10:48 -06:00
|
|
|
|
2018-03-13 09:02:40 -05:00
|
|
|
fn is_trivial_regex(s: ®ex_syntax::hir::Hir) -> Option<&'static str> {
|
|
|
|
use regex_syntax::hir::HirKind::*;
|
|
|
|
use regex_syntax::hir::Anchor::*;
|
|
|
|
|
|
|
|
let is_literal = |e: &[regex_syntax::hir::Hir]| e.iter().all(|e| match *e.kind() {
|
|
|
|
Literal(_) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
|
|
|
|
match *s.kind() {
|
|
|
|
Empty |
|
|
|
|
Anchor(_) => Some("the regex is unlikely to be useful as it is"),
|
|
|
|
Literal(_) => Some("consider using `str::contains`"),
|
|
|
|
Alternation(ref exprs) => if exprs.iter().all(|e| e.kind().is_empty()) {
|
|
|
|
Some("the regex is unlikely to be useful as it is")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
Concat(ref exprs) => match (exprs[0].kind(), exprs[exprs.len() - 1].kind()) {
|
|
|
|
(&Anchor(StartText), &Anchor(EndText)) if exprs[1..(exprs.len() - 1)].is_empty() => Some("consider using `str::is_empty`"),
|
|
|
|
(&Anchor(StartText), &Anchor(EndText)) if is_literal(&exprs[1..(exprs.len() - 1)]) => Some("consider using `==` on `str`s"),
|
|
|
|
(&Anchor(StartText), &Literal(_)) if is_literal(&exprs[1..]) => Some("consider using `str::starts_with`"),
|
|
|
|
(&Literal(_), &Anchor(EndText)) if is_literal(&exprs[1..(exprs.len() - 1)]) => Some("consider using `str::ends_with`"),
|
|
|
|
_ if is_literal(exprs) => Some("consider using `str::contains`"),
|
2017-09-05 04:33:04 -05:00
|
|
|
_ => None,
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-02-06 11:06:39 -06:00
|
|
|
_ => None,
|
2016-02-05 16:10:48 -06:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 10:15:19 -05:00
|
|
|
|
2017-09-13 08:34:04 -05:00
|
|
|
fn check_set<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, utf8: bool) {
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::AddrOf(_, ref expr) = expr.node;
|
|
|
|
if let ExprKind::Array(ref exprs) = expr.node;
|
2017-10-23 14:18:02 -05:00
|
|
|
then {
|
|
|
|
for expr in exprs {
|
|
|
|
check_regex(cx, expr, utf8);
|
|
|
|
}
|
2016-05-25 10:15:19 -05:00
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
2016-05-25 10:15:19 -05:00
|
|
|
}
|
|
|
|
|
2017-09-13 08:34:04 -05:00
|
|
|
fn check_regex<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, utf8: bool) {
|
2018-04-07 15:18:51 -05:00
|
|
|
let mut parser = regex_syntax::ParserBuilder::new()
|
|
|
|
.unicode(utf8)
|
|
|
|
.allow_invalid_utf8(!utf8)
|
|
|
|
.build();
|
2016-05-25 10:15:19 -05:00
|
|
|
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Lit(ref lit) = expr.node {
|
2018-01-23 09:52:14 -06:00
|
|
|
if let LitKind::Str(ref r, style) = lit.node {
|
2017-03-30 03:21:13 -05:00
|
|
|
let r = &r.as_str();
|
2018-03-13 09:02:40 -05:00
|
|
|
let offset = if let StrStyle::Raw(n) = style { 2 + n } else { 1 };
|
|
|
|
match parser.parse(r) {
|
2017-09-05 04:33:04 -05:00
|
|
|
Ok(r) => if let Some(repl) = is_trivial_regex(&r) {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
TRIVIAL_REGEX,
|
|
|
|
expr.span,
|
|
|
|
"trivial regex",
|
2018-03-13 09:02:40 -05:00
|
|
|
repl,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Err(regex_syntax::Error::Parse(e)) => {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
INVALID_REGEX,
|
|
|
|
str_span(expr.span, *e.span(), offset),
|
|
|
|
&format!("regex syntax error: {}", e.kind()),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Err(regex_syntax::Error::Translate(e)) => {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
INVALID_REGEX,
|
|
|
|
str_span(expr.span, *e.span(), offset),
|
|
|
|
&format!("regex syntax error: {}", e.kind()),
|
2017-09-05 04:33:04 -05:00
|
|
|
);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-05-25 10:15:19 -05:00
|
|
|
Err(e) => {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
INVALID_REGEX,
|
2018-03-13 09:02:40 -05:00
|
|
|
expr.span,
|
|
|
|
&format!("regex syntax error: {}", e),
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-05-25 10:15:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(r) = const_str(cx, expr) {
|
2018-03-13 09:02:40 -05:00
|
|
|
match parser.parse(&r) {
|
2017-09-05 04:33:04 -05:00
|
|
|
Ok(r) => if let Some(repl) = is_trivial_regex(&r) {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
TRIVIAL_REGEX,
|
|
|
|
expr.span,
|
|
|
|
"trivial regex",
|
2018-03-13 09:02:40 -05:00
|
|
|
repl,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Err(regex_syntax::Error::Parse(e)) => {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
INVALID_REGEX,
|
|
|
|
expr.span,
|
|
|
|
&format!("regex syntax error on position {}: {}", e.span().start.offset, e.kind()),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Err(regex_syntax::Error::Translate(e)) => {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
INVALID_REGEX,
|
|
|
|
expr.span,
|
|
|
|
&format!("regex syntax error on position {}: {}", e.span().start.offset, e.kind()),
|
2017-09-05 04:33:04 -05:00
|
|
|
);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-05-25 10:15:19 -05:00
|
|
|
Err(e) => {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
INVALID_REGEX,
|
|
|
|
expr.span,
|
2018-03-13 09:02:40 -05:00
|
|
|
&format!("regex syntax error: {}", e),
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-05-25 10:15:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|