-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add lints for raw string literals #112373
Conversation
Proof-of-concept, lots more could be done, eg. suggestions and short-circuiting search
Many improvements still possible, eg. suggestions and narrower span
(rustbot has picked a reviewer for you, use r? to override) |
The job Click to see the possible cause of the failure (guessed by this bot)
|
@@ -541,6 +541,22 @@ lint_unused_op = unused {$op} that must be used | |||
.label = the {$op} produces a value | |||
.suggestion = use `let _ = ...` to ignore the resulting value | |||
|
|||
lint_unused_raw_string = string literal does not need to be raw. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the Rust Compiler Development Guide, diagnostics should not have trailing punctuation.
lint_unused_raw_string = string literal does not need to be raw. | |
lint_unused_raw_string = string literal does not need to be raw |
} would result in the same value | ||
|
||
lint_unused_raw_string_hash = | ||
raw string literal uses more hashes than it needs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise
raw string literal uses more hashes than it needs. | |
raw string literal uses more hashes than it needs |
Hmm, it's not obvious to me that extra hashes is really a problem that needs to be warned about. They remind me of the parens in If I always write my regexes with raw strings even if they don't happen to contain To me, this is the kind of "hey, did you know that you could ________" thing that's wonderful as a subtle hint from R-A in an IDE, but I'm not convinced that it's something I'd ever want to see as a warning on the command line while I'm working on something. Is there some particular harm you're hoping to avoid with these lints that I'm missing? |
I was under the impression that that lints do not necessarily have to indicate "problems" in the code (eg while_true, redundant_semicolons). Is there some criteria somewhere for lints that I missed?
Yeah, I see what you mean. In this case, the braces are useful because they make the implicit associativity strength explicit through the braces, so it can improve readability. I'd argue extra hashes are more of a
My main idea was to prevent code with hundreds of hashes from going through. A while ago I reduced the hash count maximum to 255 from 2^16, I'm reminded by a justification someone (oh, I looked it up, it was you! small world) gave for this: 255 is an order of magnitude more than any plausible situation. In general, if someone finds themselves needing/wanting hundreds of hashes, they are doing something rather questionable and may very easily find themselves crossing the 255 limit, which would be an issue. I would imagine most people aren't aware 256 hashes will be rejected by the lexer. I mentioned in my previous message, the hash warning criterion may be too strict. It could, for instance, be applied to messages which have 50 hashes too many. Or perhaps just any literal with more than 50 hashes, too many or not. In the current state, the warning would presumably encourage people to use fewer hashes if possible, keeping them away from the 255 bound. |
Switched to
S-waiting-on-team
|
The expression precedence table is quite complicated and one can't expect people to keep it in their head. For example take something like If you lint about extra |
Maybe this can go into clippy first? |
fyi: there is also a PR to add such a lint to clippy: rust-lang/rust-clippy#10884 |
Looks like I was a couple days late, shame. My |
The goal is to teach people that writing pub fn foo() -> i32 {
while true {
// ...
return 4;
}
unreachable!()
} is a poor way to go about it, since it would be better as
It's definitely fair that it's not a clearly-delineated line, though. Basically, my thought here was that if someone wants to do const BOOK_SNIPPIT: &str = r#################"
Then, in your new *variables* directory, open *src/main.rs* and replace its
code with the following code, which won’t compile just yet:
<span class="filename">Filename: src/main.rs</span>
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/src/main.rs}}
```
Save and run the program using `cargo run`. You should receive an error message
regarding an immutability error, as shown in this output:
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/output.txt}}
```
This example shows how the compiler helps you find errors in your programs.
Compiler errors can be frustrating, but really they only mean your program
isn’t safely doing what you want it to do yet; they do *not* mean that you’re
not a good programmer! Experienced Rustaceans still get compiler errors
"#################; Then I think I'm fine with that, even though it's definitely more I guess maybe I'm thinking of it kinda like people using
as separators. |
Thanks, rust-lang#112373, for the snippet at line 75!
This PR introduces two lints related to raw {ε, byte, c} string literals. Though these literals currently allow up to 255 hashes to surround them, my main thought was that lines like
let x = r#^{200}" normal string "#^{200};
should never occur (without a warning) in code.
The first lint,
unused_raw_string_hash
, triggers when a raw string literal is surrounded by more hashes than it needs. In a way, it's similar to theunused_parens
lint, but for hashes.The second lint expands on the idea of unnessesary raw string syntax. When a string literal contains no escapes and no quotes, prefixing it with an
r
has no impact on its value. Theunused_raw_string
lint triggers when a string literal is raw without needing to be.With the current implementation, a file containing
emits
Some related thoughts:
unused_raw_string
will also triggerunused_raw_string_hash
. The latter would be redundant information, so perhaps should be supressed.MachineApplicable
without issue.unused_raw_string_hash
lint is too strict. It could be amended to trigger ifn
too many hashes, or perhapsn
hashesthough this treatment is not given to
unused_parens
.Please let me know if this is worth pursuing futher, if any code/ideas should be adapted, or if anything else comes to mind :)
@rustbot label: +T-lang