-
Notifications
You must be signed in to change notification settings - Fork 1.6k
non_ascii_literal should not be machine-applicable to raw strings #9805
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I believe avoiding the false positive for raw strings is enough, as there would be no suggestion in that case. 🙃 @rustbot label +good-first-issue |
That'd be the easiest fix but there's a few things going on as I see them: First is whether or not to report for raw strings - maybe someone is trying to enforce an all-ASCII codebase with this lint. Second, if it is still reported, there could be a hint along the lines of "Clippy can replace non-ASCII characters with substitutions, except for raw strings". I suppose there's also a potential restriction lint which would convert raw strings to normal strings - in which case a subsequent fix could apply the unicode substitution. I went looking and wow, this goes back to #85 and #299, looks like the original basis of this lint was trying to do something about zero-width spaces and visually similar unicode characters. |
Good point, in that case it's probably the best to make the suggestion not machine applicable if it's a raw string. And add a note to the lint description. We could potentially also add a config value, but that might be overkill. Thank you for the comment :) |
I've been having a read through https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/unicode.rs The lint As for I've got a minimal repo up here: https://github.com/alexjago/clippy_unicode_tests |
I tried getting started on working on this and wrote this function, used your examples in the testfile fn is_raw_str(expr: &'_ Expr<'_>) -> bool {
if let ExprKind::Lit(ref lit) = expr.kind {
if let LitKind::Str(_, style) = lit.node {
if matches!(style, StrStyle::Raw(_)) {
return true;
}
}
}
false
} Do you know why this function might not work on the example raw string literals? |
Hmmm, I'm sadly not an expert, when it comes to the |
Your intuition is good here. Something about the
If I do this without the format link and try on a raw string literal itself, the code above works as expected. Do you have an opinion about what I should do next about this? |
Hey, @nyurik and @Alexendoo, I believe you're the best |
I'm more of an apprentice :) Note that just a few days ago rust-lang/rust#106745 was merged - which will require a significant rework of all the format-related lint code, so the above might no longer be relevant... |
Tricky one, It looks like the lint does take a snippet to examine already, so checking if that starts with The suggestion to still lint raw strings but change the suggestion to mention it'd have to be converted to a normal string sounds good to me |
Summary
non_ascii_literal
is machine-applied by substituting unicode escapes. However, those are incompatible with raw strings.(And if like me you try using raw strings as formatting targets, you'll then get errors about invalid references to positional arguments.)
Reproducer
This code should trigger the lint:
cargo clippy --fix
reported the following:Version
Additional Labels
@rustbot +label I-suggestion-causes-error I-false-positive
The text was updated successfully, but these errors were encountered: