2021-06-03 06:41:37 +00:00
|
|
|
use clippy_utils::consts::{constant, Constant};
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
|
|
|
|
use clippy_utils::{match_def_path, paths};
|
2018-11-27 20:14:15 +00:00
|
|
|
use if_chain::if_chain;
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast::{LitKind, StrStyle};
|
2021-05-21 17:35:49 +00:00
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-09-28 17:03:12 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::{BytePos, Span};
|
2016-02-04 23:36:06 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks [regex](https://crates.io/crates/regex) creation
|
2020-11-23 12:51:04 +00:00
|
|
|
/// (with `Regex::new`, `RegexBuilder::new`, or `RegexSet::new`) for correct
|
2019-03-05 16:50:33 +00:00
|
|
|
/// regex syntax.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This will lead to a runtime panic.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 22:23:50 +00:00
|
|
|
/// ```ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// Regex::new("|")
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-02-04 23:36:06 +00:00
|
|
|
pub INVALID_REGEX,
|
2018-03-28 13:24:26 +00:00
|
|
|
correctness,
|
2016-08-06 08:18:36 +00:00
|
|
|
"invalid regular expressions"
|
2016-02-04 23:36:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for trivial [regex](https://crates.io/crates/regex)
|
2020-11-23 12:51:04 +00:00
|
|
|
/// creation (with `Regex::new`, `RegexBuilder::new`, or `RegexSet::new`).
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Matching the regex can likely be replaced by `==` or
|
2019-03-05 16:50:33 +00:00
|
|
|
/// `str::starts_with`, `str::ends_with` or `std::contains` or other `str`
|
|
|
|
/// methods.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// If the same regex is going to be applied to multiple
|
2021-02-11 14:04:38 +00:00
|
|
|
/// inputs, the precomputations done by `Regex` construction can give
|
|
|
|
/// significantly better performance than any of the `str`-based methods.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 22:23:50 +00:00
|
|
|
/// ```ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// Regex::new("^foobar")
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-02-05 22:10:48 +00:00
|
|
|
pub TRIVIAL_REGEX,
|
2021-02-11 14:04:38 +00:00
|
|
|
nursery,
|
2016-08-06 08:18:36 +00:00
|
|
|
"trivial regular expressions"
|
2016-02-05 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 17:03:12 +00:00
|
|
|
declare_lint_pass!(Regex => [INVALID_REGEX, TRIVIAL_REGEX]);
|
2016-02-04 23:36:06 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Regex {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2017-10-23 19:18:02 +00:00
|
|
|
if_chain! {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let ExprKind::Call(fun, args) = expr.kind;
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Path(ref qpath) = fun.kind;
|
2017-10-23 19:18:02 +00:00
|
|
|
if args.len() == 1;
|
2020-06-26 02:55:23 +00:00
|
|
|
if let Some(def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id();
|
2017-10-23 19:18:02 +00:00
|
|
|
then {
|
2019-05-17 21:53:54 +00:00
|
|
|
if match_def_path(cx, def_id, &paths::REGEX_NEW) ||
|
|
|
|
match_def_path(cx, def_id, &paths::REGEX_BUILDER_NEW) {
|
2017-10-23 19:18:02 +00:00
|
|
|
check_regex(cx, &args[0], true);
|
2019-05-17 21:53:54 +00:00
|
|
|
} else if match_def_path(cx, def_id, &paths::REGEX_BYTES_NEW) ||
|
|
|
|
match_def_path(cx, def_id, &paths::REGEX_BYTES_BUILDER_NEW) {
|
2017-10-23 19:18:02 +00:00
|
|
|
check_regex(cx, &args[0], false);
|
2019-05-17 21:53:54 +00:00
|
|
|
} else if match_def_path(cx, def_id, &paths::REGEX_SET_NEW) {
|
2017-10-23 19:18:02 +00:00
|
|
|
check_set(cx, &args[0], true);
|
2019-05-17 21:53:54 +00:00
|
|
|
} else if match_def_path(cx, def_id, &paths::REGEX_BYTES_SET_NEW) {
|
2017-10-23 19:18:02 +00:00
|
|
|
check_set(cx, &args[0], false);
|
|
|
|
}
|
2016-02-05 15:48:35 +00:00
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2016-02-04 23:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-05 15:48:35 +00:00
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2022-03-25 14:05:27 +00:00
|
|
|
fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u8) -> Span {
|
2018-04-19 06:30:07 +00:00
|
|
|
let offset = u32::from(offset);
|
2018-10-10 06:35:10 +00:00
|
|
|
let end = base.lo() + BytePos(u32::try_from(c.end.offset).expect("offset too large") + offset);
|
|
|
|
let start = base.lo() + BytePos(u32::try_from(c.start.offset).expect("offset too large") + offset);
|
2018-03-13 14:02:40 +00:00
|
|
|
assert!(start <= end);
|
2021-04-18 12:27:04 +00:00
|
|
|
Span::new(start, end, base.ctxt(), base.parent())
|
2016-02-05 15:48:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn const_str<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<String> {
|
2020-07-17 08:47:04 +00:00
|
|
|
constant(cx, cx.typeck_results(), e).and_then(|(c, _)| match c {
|
2018-03-13 10:38:11 +00:00
|
|
|
Constant::Str(s) => Some(s),
|
2016-02-24 16:38:57 +00:00
|
|
|
_ => None,
|
2018-03-13 10:38:11 +00:00
|
|
|
})
|
2016-02-05 15:48:35 +00:00
|
|
|
}
|
2016-02-05 22:10:48 +00:00
|
|
|
|
2018-03-13 14:02:40 +00:00
|
|
|
fn is_trivial_regex(s: ®ex_syntax::hir::Hir) -> Option<&'static str> {
|
2020-02-21 08:39:38 +00:00
|
|
|
use regex_syntax::hir::Anchor::{EndText, StartText};
|
|
|
|
use regex_syntax::hir::HirKind::{Alternation, Anchor, Concat, Empty, Literal};
|
2018-03-13 14:02:40 +00:00
|
|
|
|
2020-07-14 12:59:59 +00:00
|
|
|
let is_literal = |e: &[regex_syntax::hir::Hir]| e.iter().all(|e| matches!(*e.kind(), Literal(_)));
|
2018-03-13 14:02:40 +00:00
|
|
|
|
|
|
|
match *s.kind() {
|
2018-11-27 20:14:15 +00:00
|
|
|
Empty | Anchor(_) => Some("the regex is unlikely to be useful as it is"),
|
2018-03-13 14:02:40 +00:00
|
|
|
Literal(_) => Some("consider using `str::contains`"),
|
2018-11-27 20:14:15 +00:00
|
|
|
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
|
|
|
|
}
|
2018-03-13 14:02:40 +00:00
|
|
|
},
|
|
|
|
Concat(ref exprs) => match (exprs[0].kind(), exprs[exprs.len() - 1].kind()) {
|
2018-11-27 20:14:15 +00:00
|
|
|
(&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")
|
|
|
|
},
|
2018-03-13 14:02:40 +00:00
|
|
|
(&Anchor(StartText), &Literal(_)) if is_literal(&exprs[1..]) => Some("consider using `str::starts_with`"),
|
2018-11-27 20:14:15 +00:00
|
|
|
(&Literal(_), &Anchor(EndText)) if is_literal(&exprs[1..(exprs.len() - 1)]) => {
|
|
|
|
Some("consider using `str::ends_with`")
|
|
|
|
},
|
2018-03-13 14:02:40 +00:00
|
|
|
_ if is_literal(exprs) => Some("consider using `str::contains`"),
|
2017-09-05 09:33:04 +00:00
|
|
|
_ => None,
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-06 17:06:39 +00:00
|
|
|
_ => None,
|
2016-02-05 22:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 15:15:19 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_set<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
|
2017-10-23 19:18:02 +00:00
|
|
|
if_chain! {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let ExprKind::AddrOf(BorrowKind::Ref, _, expr) = expr.kind;
|
2019-12-27 07:12:26 +00:00
|
|
|
if let ExprKind::Array(exprs) = expr.kind;
|
2017-10-23 19:18:02 +00:00
|
|
|
then {
|
|
|
|
for expr in exprs {
|
|
|
|
check_regex(cx, expr, utf8);
|
|
|
|
}
|
2016-05-25 15:15:19 +00:00
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2016-05-25 15:15:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
|
2018-04-07 20:18:51 +00:00
|
|
|
let mut parser = regex_syntax::ParserBuilder::new()
|
2020-10-09 10:45:29 +00:00
|
|
|
.unicode(true)
|
2018-04-07 20:18:51 +00:00
|
|
|
.allow_invalid_utf8(!utf8)
|
|
|
|
.build();
|
2016-05-25 15:15:19 +00:00
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Lit(ref lit) = expr.kind {
|
2018-01-23 15:52:14 +00:00
|
|
|
if let LitKind::Str(ref r, style) = lit.node {
|
2021-12-15 03:39:23 +00:00
|
|
|
let r = r.as_str();
|
2018-03-13 14:02:40 +00:00
|
|
|
let offset = if let StrStyle::Raw(n) = style { 2 + n } else { 1 };
|
|
|
|
match parser.parse(r) {
|
2018-11-27 20:14:15 +00:00
|
|
|
Ok(r) => {
|
|
|
|
if let Some(repl) = is_trivial_regex(&r) {
|
2020-04-18 10:28:29 +00:00
|
|
|
span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", None, repl);
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
2018-03-13 14:02:40 +00:00
|
|
|
},
|
|
|
|
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 09:33:04 +00:00
|
|
|
);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-05-25 15:15:19 +00:00
|
|
|
Err(e) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
span_lint(cx, INVALID_REGEX, expr.span, &format!("regex syntax error: {}", e));
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-05-25 15:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(r) = const_str(cx, expr) {
|
2018-03-13 14:02:40 +00:00
|
|
|
match parser.parse(&r) {
|
2018-11-27 20:14:15 +00:00
|
|
|
Ok(r) => {
|
|
|
|
if let Some(repl) = is_trivial_regex(&r) {
|
2020-04-18 10:28:29 +00:00
|
|
|
span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", None, repl);
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
2018-03-13 14:02:40 +00:00
|
|
|
},
|
|
|
|
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 09:33:04 +00:00
|
|
|
);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-05-25 15:15:19 +00:00
|
|
|
Err(e) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
span_lint(cx, INVALID_REGEX, expr.span, &format!("regex syntax error: {}", e));
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-05-25 15:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|