Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unrecognized escape sequence #4170

Closed
ThomasdenH opened this issue Jun 3, 2019 · 6 comments · Fixed by #10231
Closed

Unrecognized escape sequence #4170

ThomasdenH opened this issue Jun 3, 2019 · 6 comments · Fixed by #10231
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@ThomasdenH
Copy link

Clippy version: clippy 0.0.212 (265318d 2019-05-17)

I get the following error when linting code, and I have a feeling it might be a bug. If it is not, it maybe clippy can give a correct suggestion?

error: regex syntax error: unrecognized escape sequence
   --> src/compiler/utilities.rs:187:52
    |
187 |         static ref RE: Regex = Regex::new("^..?($|[\\/])").unwrap();
    |                                                    ^^
    |
    = note: #[deny(clippy::invalid_regex)] on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#invalid_regex
@flip1995
Copy link
Member

flip1995 commented Jun 3, 2019

This indeed looks like valid regex. But Clippy uses the parser of rust-lang/regex to detect invalid regex. This means the regex parser fails to parse what seems to be valid regex. You should probably open an issue in rust-lang/regex.

Building a suggestion for this lint is hard or even impossible, since Clippy can't really guess what the correct regex would be.

@ThomasdenH
Copy link
Author

ThomasdenH commented Jun 3, 2019

I opened an issue there, thanks.

@ThomasdenH
Copy link
Author

I found the solution here. The reason it fails is because it should be escaped once for the string literal, and once for the regex. If possible, a better description of the problem would be nice.

@flip1995
Copy link
Member

flip1995 commented Jun 3, 2019

Yeah, I was just about to write this, after testing this.

You could also use a raw string:

-static ref RE: Regex = Regex::new("^..?($|[\\/])").unwrap();
+static ref RE: Regex = Regex::new(r"^..?($|[\\/])").unwrap();

@flip1995
Copy link
Member

flip1995 commented Jun 3, 2019

Ok in this case, a (multipart) suggestion would be possible:

"Try escaping the \ twice: \\\\"

"Or use a raw string: r"regex""

@flip1995 flip1995 added L-suggestion Lint: Improving, adding or fixing lint suggestions good-first-issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Jun 3, 2019
@mr-cheff
Copy link

Hm... I recently had this problem. My solution was to just remove the \\ since apparently, it can handle / in a raw string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants