rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`invalid_regex` example is wrong

Open mejrs opened this issue 1 month ago • 4 comments

See https://clippy.rust-lang.org/master/#invalid_regex

Use instead:

Regex::new("\(")

produces:

error: unknown character escape: `(`
 --> src/lib.rs:1:14
  |
1 | Regex::new("\(")
  |              ^ unknown character escape
  |
  = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
  |
1 | Regex::new(r"\(")
  |            +

mejrs avatar Oct 26 '26 22:10 mejrs

Proposed Fix

In clippy_lints/src/regex.rs, the INVALID_REGEX documentation example suggested Regex::new("\("). In standard Rust string literals, \( is not a valid escape sequence and causes error: unknown character escape: \(.

We updated the example to use an idiomatic raw string literal:

/// Use instead:
/// ```ignore
/// Regex::new(r"\(")
/// ```

Verification

  • cargo dev fmt --check: Clean (no diff).
  • cargo check -p clippy_lints: Clean (zero errors, zero warnings).

Committed and pushed to fork branch: duongynhi000005-oss/rust-clippy:fix/issue-17926-invalid-regex-doc-example (commit e235e50).

Happy to submit a PR if you would like!

duongynhi000005-oss avatar Oct 26 '26 23:10 duongynhi000005-oss

Hi, I’d like to work on this documentation fix. Is anyone already preparing a PR for it? If not, I’m happy to submit one with the corrected raw string example and verify the documentation build.

Bimal-ray99 avatar Oct 27 '26 01:10 Bimal-ray99

@Bimal-ray99 you could claim the issue by typing the @rustbot claim text.

SzilvasiPeter avatar Oct 27 '26 08:10 SzilvasiPeter

@rustbot claim

Vamsi-2581 avatar Oct 27 '26 15:10 Vamsi-2581