Skip to content

Fix needless_raw_string_hashes miscounting hashes after two adjacent quotes#17456

Open
shulaoda wants to merge 1 commit into
rust-lang:masterfrom
shulaoda:07-26-fix_needless_raw_string_hashes_miscounting_hashes_after_two_adjacent_quotes
Open

Fix needless_raw_string_hashes miscounting hashes after two adjacent quotes#17456
shulaoda wants to merge 1 commit into
rust-lang:masterfrom
shulaoda:07-26-fix_needless_raw_string_hashes_miscounting_hashes_after_two_adjacent_quotes

Conversation

@shulaoda

Copy link
Copy Markdown
Contributor

Fixes #11737

needless_raw_string_hashes scans the literal for the longest run of hashes following a quote, since that decides how many hashes the literal needs. The arm that starts a run is guarded by if !following_quote, so a second quote in a row falls through to the catch-all arm instead, which ends the run and clears following_quote. The hashes after that quote are then added to nothing, and the run they belong to is never counted.

An empty inner raw string produces exactly that byte sequence, so r##"{r#""#}"## was reported as needing only one hash. Applying the machine-applicable suggestion terminates the literal early at the inner "# and fails to compile with error[E0765]: unterminated double quote string.

The catch-all arm now starts a new run when the byte that ended the previous one is itself a quote.

changelog: [needless_raw_string_hashes]: don't drop the hashes that follow two adjacent quotes

@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 26, 2026
@rustbot

rustbot commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor suggestions, otherwise LGTM

View changes since this review

Comment on lines 146 to 149
b'"' if !following_quote => (following_quote, req) = (true, 1),
b'#' => req += u8::from(following_quote),
_ => {
if following_quote {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated nit: I would appreciate this order, since this way reading why below is happening is simpler.
Took me longer than nessary to understand why below is happening 😉

Suggested change
if following_quote {
b'#' => req += u8::from(following_quote),
b'"' if !following_quote => (following_quote, req) = (true, 1),
_ if following_quote => {

Github won't let me suggest this suggestion correctly, no clue why.
Should be clear enough though

Comment on lines +57 to +58
r##"task test(execute: "") {r#""#}"##;
r##"a quote pair followed by a hash: ""#"##;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets also add the cases with the gap in the middle "just in case" to model the whole problem space in the tests, even though that already worked.

following_quote = false;
let ended = req;
// a quote also starts a new run, e.g. `""#` requires two hashes
(following_quote, req) = (b == b'"', 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is essentially what the first part of the match does currently under the precondition of the match.

Not sure if the code can be simplified, I did not find an clearly better solution 😆
A few sidegrades, but no clearly better one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FP: needless_raw_string_hashes on inner empty raw string

3 participants