2023-02-10 07:01:19 -06:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
2021-06-03 01:41:37 -05:00
|
|
|
use clippy_utils::consts::{constant, Constant};
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
|
2023-02-10 07:01:19 -06:00
|
|
|
use clippy_utils::source::snippet_opt;
|
2023-07-17 03:19:29 -05:00
|
|
|
use clippy_utils::{def_path_def_ids, path_def_id, paths};
|
2020-02-29 21:23:33 -06:00
|
|
|
use rustc_ast::ast::{LitKind, StrStyle};
|
2023-07-17 03:19:29 -05:00
|
|
|
use rustc_hir::def_id::DefIdMap;
|
2021-05-21 12:35:49 -05:00
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-07-17 03:19:29 -05:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2023-11-01 22:10:12 -05:00
|
|
|
use rustc_span::{BytePos, Span};
|
2016-02-04 17:36:06 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks [regex](https://crates.io/crates/regex) creation
|
2020-11-23 06:51:04 -06:00
|
|
|
/// (with `Regex::new`, `RegexBuilder::new`, or `RegexSet::new`) for correct
|
2019-03-05 10:50:33 -06:00
|
|
|
/// regex syntax.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This will lead to a runtime panic.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 16:23:50 -06:00
|
|
|
/// ```ignore
|
2022-07-18 02:39:37 -05:00
|
|
|
/// Regex::new("(")
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
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
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for trivial [regex](https://crates.io/crates/regex)
|
2020-11-23 06:51:04 -06:00
|
|
|
/// creation (with `Regex::new`, `RegexBuilder::new`, or `RegexSet::new`).
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Matching the regex can likely be replaced by `==` or
|
2019-03-05 10:50:33 -06:00
|
|
|
/// `str::starts_with`, `str::ends_with` or `std::contains` or other `str`
|
|
|
|
/// methods.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// If the same regex is going to be applied to multiple
|
2021-02-11 08:04:38 -06:00
|
|
|
/// inputs, the precomputations done by `Regex` construction can give
|
|
|
|
/// significantly better performance than any of the `str`-based methods.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 16:23:50 -06:00
|
|
|
/// ```ignore
|
2019-03-05 10:50:33 -06:00
|
|
|
/// Regex::new("^foobar")
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-02-05 16:10:48 -06:00
|
|
|
pub TRIVIAL_REGEX,
|
2021-02-11 08:04:38 -06:00
|
|
|
nursery,
|
2016-08-06 03:18:36 -05:00
|
|
|
"trivial regular expressions"
|
2016-02-05 16:10:48 -06:00
|
|
|
}
|
|
|
|
|
2023-07-17 03:19:29 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum RegexKind {
|
|
|
|
Unicode,
|
|
|
|
UnicodeSet,
|
|
|
|
Bytes,
|
|
|
|
BytesSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Regex {
|
|
|
|
definitions: DefIdMap<RegexKind>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(Regex => [INVALID_REGEX, TRIVIAL_REGEX]);
|
2016-02-04 17:36:06 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Regex {
|
2023-07-17 03:19:29 -05:00
|
|
|
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
|
|
|
|
// We don't use `match_def_path` here because that relies on matching the exact path, which changed
|
|
|
|
// between regex 1.8 and 1.9
|
|
|
|
//
|
|
|
|
// `def_path_def_ids` will resolve through re-exports but is relatively heavy, so we only perform
|
|
|
|
// the operation once and store the results
|
|
|
|
let mut resolve = |path, kind| {
|
|
|
|
for id in def_path_def_ids(cx, path) {
|
|
|
|
self.definitions.insert(id, kind);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resolve(&paths::REGEX_NEW, RegexKind::Unicode);
|
|
|
|
resolve(&paths::REGEX_BUILDER_NEW, RegexKind::Unicode);
|
|
|
|
resolve(&paths::REGEX_SET_NEW, RegexKind::UnicodeSet);
|
|
|
|
resolve(&paths::REGEX_BYTES_NEW, RegexKind::Bytes);
|
|
|
|
resolve(&paths::REGEX_BYTES_BUILDER_NEW, RegexKind::Bytes);
|
|
|
|
resolve(&paths::REGEX_BYTES_SET_NEW, RegexKind::BytesSet);
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2023-07-17 03:19:29 -05:00
|
|
|
if let ExprKind::Call(fun, [arg]) = expr.kind
|
|
|
|
&& let Some(def_id) = path_def_id(cx, fun)
|
|
|
|
&& let Some(regex_kind) = self.definitions.get(&def_id)
|
|
|
|
{
|
|
|
|
match regex_kind {
|
|
|
|
RegexKind::Unicode => check_regex(cx, arg, true),
|
|
|
|
RegexKind::UnicodeSet => check_set(cx, arg, true),
|
|
|
|
RegexKind::Bytes => check_regex(cx, arg, false),
|
|
|
|
RegexKind::BytesSet => check_set(cx, arg, 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
|
|
|
|
2023-02-10 07:01:19 -06:00
|
|
|
fn lint_syntax_error(cx: &LateContext<'_>, error: ®ex_syntax::Error, unescaped: &str, base: Span, offset: u8) {
|
|
|
|
let parts: Option<(_, _, &dyn Display)> = match &error {
|
|
|
|
regex_syntax::Error::Parse(e) => Some((e.span(), e.auxiliary_span(), e.kind())),
|
|
|
|
regex_syntax::Error::Translate(e) => Some((e.span(), None, e.kind())),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let convert_span = |regex_span: ®ex_syntax::ast::Span| {
|
|
|
|
let offset = u32::from(offset);
|
|
|
|
let start = base.lo() + BytePos(u32::try_from(regex_span.start.offset).expect("offset too large") + offset);
|
|
|
|
let end = base.lo() + BytePos(u32::try_from(regex_span.end.offset).expect("offset too large") + offset);
|
|
|
|
|
|
|
|
Span::new(start, end, base.ctxt(), base.parent())
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some((primary, auxiliary, kind)) = parts
|
|
|
|
&& let Some(literal_snippet) = snippet_opt(cx, base)
|
|
|
|
&& let Some(inner) = literal_snippet.get(offset as usize..)
|
|
|
|
// Only convert to native rustc spans if the parsed regex matches the
|
|
|
|
// source snippet exactly, to ensure the span offsets are correct
|
|
|
|
&& inner.get(..unescaped.len()) == Some(unescaped)
|
|
|
|
{
|
|
|
|
let spans = if let Some(auxiliary) = auxiliary {
|
|
|
|
vec![convert_span(primary), convert_span(auxiliary)]
|
|
|
|
} else {
|
|
|
|
vec![convert_span(primary)]
|
|
|
|
};
|
|
|
|
|
|
|
|
span_lint(cx, INVALID_REGEX, spans, &format!("regex syntax error: {kind}"));
|
|
|
|
} else {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
INVALID_REGEX,
|
|
|
|
base,
|
|
|
|
&error.to_string(),
|
|
|
|
None,
|
|
|
|
"consider using a raw string literal: `r\"..\"`",
|
|
|
|
);
|
|
|
|
}
|
2016-02-05 09:48:35 -06:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn const_str<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<String> {
|
2023-05-20 08:39:26 -05:00
|
|
|
constant(cx, cx.typeck_results(), 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> {
|
2023-05-20 08:39:26 -05:00
|
|
|
use regex_syntax::hir::HirKind::{Alternation, Concat, Empty, Literal, Look};
|
|
|
|
use regex_syntax::hir::Look as HirLook;
|
2018-03-13 09:02:40 -05:00
|
|
|
|
2020-07-14 07:59:59 -05:00
|
|
|
let is_literal = |e: &[regex_syntax::hir::Hir]| e.iter().all(|e| matches!(*e.kind(), Literal(_)));
|
2018-03-13 09:02:40 -05:00
|
|
|
|
|
|
|
match *s.kind() {
|
2023-05-20 08:39:26 -05:00
|
|
|
Empty | Look(_) => Some("the regex is unlikely to be useful as it is"),
|
2018-03-13 09:02:40 -05:00
|
|
|
Literal(_) => Some("consider using `str::contains`"),
|
2018-11-27 14:14:15 -06:00
|
|
|
Alternation(ref exprs) => {
|
2023-05-20 08:39:26 -05:00
|
|
|
if exprs.iter().all(|e| matches!(e.kind(), Empty)) {
|
2018-11-27 14:14:15 -06:00
|
|
|
Some("the regex is unlikely to be useful as it is")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-03-13 09:02:40 -05:00
|
|
|
},
|
|
|
|
Concat(ref exprs) => match (exprs[0].kind(), exprs[exprs.len() - 1].kind()) {
|
2023-05-20 08:39:26 -05:00
|
|
|
(&Look(HirLook::Start), &Look(HirLook::End)) if exprs[1..(exprs.len() - 1)].is_empty() => {
|
2018-11-27 14:14:15 -06:00
|
|
|
Some("consider using `str::is_empty`")
|
|
|
|
},
|
2023-05-20 08:39:26 -05:00
|
|
|
(&Look(HirLook::Start), &Look(HirLook::End)) if is_literal(&exprs[1..(exprs.len() - 1)]) => {
|
2018-11-27 14:14:15 -06:00
|
|
|
Some("consider using `==` on `str`s")
|
|
|
|
},
|
2023-05-20 08:39:26 -05:00
|
|
|
(&Look(HirLook::Start), &Literal(_)) if is_literal(&exprs[1..]) => {
|
|
|
|
Some("consider using `str::starts_with`")
|
|
|
|
},
|
|
|
|
(&Literal(_), &Look(HirLook::End)) if is_literal(&exprs[1..(exprs.len() - 1)]) => {
|
2018-11-27 14:14:15 -06:00
|
|
|
Some("consider using `str::ends_with`")
|
|
|
|
},
|
2018-03-13 09:02:40 -05:00
|
|
|
_ 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
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_set<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
|
2023-11-16 12:13:24 -06:00
|
|
|
if let ExprKind::AddrOf(BorrowKind::Ref, _, expr) = expr.kind
|
|
|
|
&& let ExprKind::Array(exprs) = expr.kind
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
|
2023-06-02 04:41:57 -05:00
|
|
|
let mut parser = regex_syntax::ParserBuilder::new().unicode(true).utf8(utf8).build();
|
2016-05-25 10:15:19 -05:00
|
|
|
|
2023-05-05 10:45:49 -05:00
|
|
|
if let ExprKind::Lit(lit) = expr.kind {
|
2018-01-23 09:52:14 -06:00
|
|
|
if let LitKind::Str(ref r, style) = lit.node {
|
2021-12-14 21:39:23 -06: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) {
|
2018-11-27 14:14:15 -06:00
|
|
|
Ok(r) => {
|
|
|
|
if let Some(repl) = is_trivial_regex(&r) {
|
2020-04-18 05:28:29 -05:00
|
|
|
span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", None, repl);
|
2018-11-27 14:14:15 -06:00
|
|
|
}
|
2018-03-13 09:02:40 -05:00
|
|
|
},
|
2023-02-10 07:01:19 -06:00
|
|
|
Err(e) => lint_syntax_error(cx, &e, r, expr.span, offset),
|
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) {
|
2018-11-27 14:14:15 -06:00
|
|
|
Ok(r) => {
|
|
|
|
if let Some(repl) = is_trivial_regex(&r) {
|
2020-04-18 05:28:29 -05:00
|
|
|
span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", None, repl);
|
2018-11-27 14:14:15 -06:00
|
|
|
}
|
2018-03-13 09:02:40 -05:00
|
|
|
},
|
2023-02-10 07:01:19 -06:00
|
|
|
Err(e) => span_lint(cx, INVALID_REGEX, expr.span, &e.to_string()),
|
2016-05-25 10:15:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|