Auto merge of #10682 - J-ZhengLi:issue10680, r=dswij

fix [`invalid_regex`] not recognizing new syntax introduced after regex-1.8.0

fixes: #10680

---

changelog: fix [`invalid_regex`] not recognizing new syntax introduced after regex-1.8.0

bump up `regex-syntax` dependency version to 0.7.0
This commit is contained in:
bors 2023-05-18 01:46:01 +00:00
commit 223f5184ba
4 changed files with 28 additions and 39 deletions

View File

@ -17,7 +17,7 @@ if_chain = "1.0"
itertools = "0.10.1" itertools = "0.10.1"
pulldown-cmark = { version = "0.9", default-features = false } pulldown-cmark = { version = "0.9", default-features = false }
quine-mc_cluskey = "0.2" quine-mc_cluskey = "0.2"
regex-syntax = "0.6" regex-syntax = "0.7"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true }
tempfile = { version = "3.3.0", optional = true } tempfile = { version = "3.3.0", optional = true }

View File

@ -129,30 +129,32 @@ fn const_str<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<String>
} }
fn is_trivial_regex(s: &regex_syntax::hir::Hir) -> Option<&'static str> { fn is_trivial_regex(s: &regex_syntax::hir::Hir) -> Option<&'static str> {
use regex_syntax::hir::Anchor::{EndText, StartText}; use regex_syntax::hir::HirKind::{Alternation, Concat, Empty, Literal, Look};
use regex_syntax::hir::HirKind::{Alternation, Anchor, Concat, Empty, Literal}; use regex_syntax::hir::Look as HirLook;
let is_literal = |e: &[regex_syntax::hir::Hir]| e.iter().all(|e| matches!(*e.kind(), Literal(_))); let is_literal = |e: &[regex_syntax::hir::Hir]| e.iter().all(|e| matches!(*e.kind(), Literal(_)));
match *s.kind() { match *s.kind() {
Empty | Anchor(_) => Some("the regex is unlikely to be useful as it is"), Empty | Look(_) => Some("the regex is unlikely to be useful as it is"),
Literal(_) => Some("consider using `str::contains`"), Literal(_) => Some("consider using `str::contains`"),
Alternation(ref exprs) => { Alternation(ref exprs) => {
if exprs.iter().all(|e| e.kind().is_empty()) { if exprs.iter().all(|e| matches!(e.kind(), Empty)) {
Some("the regex is unlikely to be useful as it is") Some("the regex is unlikely to be useful as it is")
} else { } else {
None None
} }
}, },
Concat(ref exprs) => match (exprs[0].kind(), exprs[exprs.len() - 1].kind()) { Concat(ref exprs) => match (exprs[0].kind(), exprs[exprs.len() - 1].kind()) {
(&Anchor(StartText), &Anchor(EndText)) if exprs[1..(exprs.len() - 1)].is_empty() => { (&Look(HirLook::Start), &Look(HirLook::End)) if exprs[1..(exprs.len() - 1)].is_empty() => {
Some("consider using `str::is_empty`") Some("consider using `str::is_empty`")
}, },
(&Anchor(StartText), &Anchor(EndText)) if is_literal(&exprs[1..(exprs.len() - 1)]) => { (&Look(HirLook::Start), &Look(HirLook::End)) if is_literal(&exprs[1..(exprs.len() - 1)]) => {
Some("consider using `==` on `str`s") Some("consider using `==` on `str`s")
}, },
(&Anchor(StartText), &Literal(_)) if is_literal(&exprs[1..]) => Some("consider using `str::starts_with`"), (&Look(HirLook::Start), &Literal(_)) if is_literal(&exprs[1..]) => {
(&Literal(_), &Anchor(EndText)) if is_literal(&exprs[1..(exprs.len() - 1)]) => { Some("consider using `str::starts_with`")
},
(&Literal(_), &Look(HirLook::End)) if is_literal(&exprs[1..(exprs.len() - 1)]) => {
Some("consider using `str::ends_with`") Some("consider using `str::ends_with`")
}, },
_ if is_literal(exprs) => Some("consider using `str::contains`"), _ if is_literal(exprs) => Some("consider using `str::contains`"),
@ -175,10 +177,7 @@ fn check_set<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
} }
fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) { fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
let mut parser = regex_syntax::ParserBuilder::new() let mut parser = regex_syntax::ParserBuilder::new().unicode(true).utf8(!utf8).build();
.unicode(true)
.allow_invalid_utf8(!utf8)
.build();
if let ExprKind::Lit(lit) = expr.kind { if let ExprKind::Lit(lit) = expr.kind {
if let LitKind::Str(ref r, style) = lit.node { if let LitKind::Str(ref r, style) = lit.node {

View File

@ -34,8 +34,10 @@ fn syntax_error() {
let set_error = RegexSet::new(&[OPENING_PAREN, r"[a-z]+\.(com|org|net)"]); let set_error = RegexSet::new(&[OPENING_PAREN, r"[a-z]+\.(com|org|net)"]);
let bset_error = BRegexSet::new(&[OPENING_PAREN, r"[a-z]+\.(com|org|net)"]); let bset_error = BRegexSet::new(&[OPENING_PAREN, r"[a-z]+\.(com|org|net)"]);
// These following three cases are considering valid since regex-1.8.0
let raw_string_error = Regex::new(r"[...\/...]"); let raw_string_error = Regex::new(r"[...\/...]");
let raw_string_error = Regex::new(r#"[...\/...]"#); let raw_string_error = Regex::new(r#"[...\/...]"#);
let _ = Regex::new(r"(?<hi>hi)").unwrap();
let escaped_string_span = Regex::new("\\b\\c"); let escaped_string_span = Regex::new("\\b\\c");

View File

@ -82,23 +82,11 @@ error: regex parse error:
LL | let bset_error = BRegexSet::new(&[OPENING_PAREN, r"[a-z]+/.(com|org|net)"]); LL | let bset_error = BRegexSet::new(&[OPENING_PAREN, r"[a-z]+/.(com|org|net)"]);
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
error: regex syntax error: unrecognized escape sequence
--> $DIR/regex.rs:37:45
|
LL | let raw_string_error = Regex::new(r"[...//...]");
| ^^
error: regex syntax error: unrecognized escape sequence
--> $DIR/regex.rs:38:46
|
LL | let raw_string_error = Regex::new(r#"[...//...]"#);
| ^^
error: regex parse error: error: regex parse error:
/b/c /b/c
^^ ^^
error: unrecognized escape sequence error: unrecognized escape sequence
--> $DIR/regex.rs:40:42 --> $DIR/regex.rs:42:42
| |
LL | let escaped_string_span = Regex::new("/b/c"); LL | let escaped_string_span = Regex::new("/b/c");
| ^^^^^^^^ | ^^^^^^^^
@ -106,13 +94,13 @@ LL | let escaped_string_span = Regex::new("/b/c");
= help: consider using a raw string literal: `r".."` = help: consider using a raw string literal: `r".."`
error: regex syntax error: duplicate flag error: regex syntax error: duplicate flag
--> $DIR/regex.rs:42:34 --> $DIR/regex.rs:44:34
| |
LL | let aux_span = Regex::new("(?ixi)"); LL | let aux_span = Regex::new("(?ixi)");
| ^ ^ | ^ ^
error: trivial regex error: trivial regex
--> $DIR/regex.rs:46:33 --> $DIR/regex.rs:48:33
| |
LL | let trivial_eq = Regex::new("^foobar$"); LL | let trivial_eq = Regex::new("^foobar$");
| ^^^^^^^^^^ | ^^^^^^^^^^
@ -120,7 +108,7 @@ LL | let trivial_eq = Regex::new("^foobar$");
= help: consider using `==` on `str`s = help: consider using `==` on `str`s
error: trivial regex error: trivial regex
--> $DIR/regex.rs:48:48 --> $DIR/regex.rs:50:48
| |
LL | let trivial_eq_builder = RegexBuilder::new("^foobar$"); LL | let trivial_eq_builder = RegexBuilder::new("^foobar$");
| ^^^^^^^^^^ | ^^^^^^^^^^
@ -128,7 +116,7 @@ LL | let trivial_eq_builder = RegexBuilder::new("^foobar$");
= help: consider using `==` on `str`s = help: consider using `==` on `str`s
error: trivial regex error: trivial regex
--> $DIR/regex.rs:50:42 --> $DIR/regex.rs:52:42
| |
LL | let trivial_starts_with = Regex::new("^foobar"); LL | let trivial_starts_with = Regex::new("^foobar");
| ^^^^^^^^^ | ^^^^^^^^^
@ -136,7 +124,7 @@ LL | let trivial_starts_with = Regex::new("^foobar");
= help: consider using `str::starts_with` = help: consider using `str::starts_with`
error: trivial regex error: trivial regex
--> $DIR/regex.rs:52:40 --> $DIR/regex.rs:54:40
| |
LL | let trivial_ends_with = Regex::new("foobar$"); LL | let trivial_ends_with = Regex::new("foobar$");
| ^^^^^^^^^ | ^^^^^^^^^
@ -144,7 +132,7 @@ LL | let trivial_ends_with = Regex::new("foobar$");
= help: consider using `str::ends_with` = help: consider using `str::ends_with`
error: trivial regex error: trivial regex
--> $DIR/regex.rs:54:39 --> $DIR/regex.rs:56:39
| |
LL | let trivial_contains = Regex::new("foobar"); LL | let trivial_contains = Regex::new("foobar");
| ^^^^^^^^ | ^^^^^^^^
@ -152,7 +140,7 @@ LL | let trivial_contains = Regex::new("foobar");
= help: consider using `str::contains` = help: consider using `str::contains`
error: trivial regex error: trivial regex
--> $DIR/regex.rs:56:39 --> $DIR/regex.rs:58:39
| |
LL | let trivial_contains = Regex::new(NOT_A_REAL_REGEX); LL | let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
@ -160,7 +148,7 @@ LL | let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
= help: consider using `str::contains` = help: consider using `str::contains`
error: trivial regex error: trivial regex
--> $DIR/regex.rs:58:40 --> $DIR/regex.rs:60:40
| |
LL | let trivial_backslash = Regex::new("a/.b"); LL | let trivial_backslash = Regex::new("a/.b");
| ^^^^^^^ | ^^^^^^^
@ -168,7 +156,7 @@ LL | let trivial_backslash = Regex::new("a/.b");
= help: consider using `str::contains` = help: consider using `str::contains`
error: trivial regex error: trivial regex
--> $DIR/regex.rs:61:36 --> $DIR/regex.rs:63:36
| |
LL | let trivial_empty = Regex::new(""); LL | let trivial_empty = Regex::new("");
| ^^ | ^^
@ -176,7 +164,7 @@ LL | let trivial_empty = Regex::new("");
= help: the regex is unlikely to be useful as it is = help: the regex is unlikely to be useful as it is
error: trivial regex error: trivial regex
--> $DIR/regex.rs:63:36 --> $DIR/regex.rs:65:36
| |
LL | let trivial_empty = Regex::new("^"); LL | let trivial_empty = Regex::new("^");
| ^^^ | ^^^
@ -184,7 +172,7 @@ LL | let trivial_empty = Regex::new("^");
= help: the regex is unlikely to be useful as it is = help: the regex is unlikely to be useful as it is
error: trivial regex error: trivial regex
--> $DIR/regex.rs:65:36 --> $DIR/regex.rs:67:36
| |
LL | let trivial_empty = Regex::new("^$"); LL | let trivial_empty = Regex::new("^$");
| ^^^^ | ^^^^
@ -192,12 +180,12 @@ LL | let trivial_empty = Regex::new("^$");
= help: consider using `str::is_empty` = help: consider using `str::is_empty`
error: trivial regex error: trivial regex
--> $DIR/regex.rs:67:44 --> $DIR/regex.rs:69:44
| |
LL | let binary_trivial_empty = BRegex::new("^$"); LL | let binary_trivial_empty = BRegex::new("^$");
| ^^^^ | ^^^^
| |
= help: consider using `str::is_empty` = help: consider using `str::is_empty`
error: aborting due to 25 previous errors error: aborting due to 23 previous errors