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

fix for x in y unsafe { } #12515

Merged
merged 3 commits into from May 2, 2024
Merged

fix for x in y unsafe { } #12515

merged 3 commits into from May 2, 2024

Conversation

bend-n
Copy link
Contributor

@bend-n bend-n commented Mar 20, 2024

fixes #12514


changelog: [needless_for_each]: unsafe block in for loop body suggestion

@rustbot
Copy link
Collaborator

rustbot commented Mar 20, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) some time within the next two weeks.

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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 20, 2024
@5225225
Copy link
Contributor

5225225 commented Mar 20, 2024

Should we be suppressing the lint here, or just emitting the correct suggestion?

@llogiq
Copy link
Contributor

llogiq commented Mar 20, 2024

I'd be fine with both.

Copy link
Member

@J-ZhengLi J-ZhengLi left a comment

Choose a reason for hiding this comment

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

Well... from what I see 👀, skipping the lint completely when unsafe block is involved doesn't really fits the lint's description, where it says:

for_each may be used after applying iterator transformers like
filter for better readability and performance. It may also be used to fit a simple
operation on one line.
But when none of these apply, a simple for loop is more idiomatic.

It might be a good idea to specify it in the doc as well, of why it's skipping the unsafe.

Also, make sure you put a fixes: with the issue number which you're trying to fix (I believe it's #12514 correct?) in your PR summary, therefore when this got merged, that issue will be closed automatically by bot.

Copy link
Member

@J-ZhengLi J-ZhengLi left a comment

Choose a reason for hiding this comment

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

Oh, one more thing~

&& let ExprKind::Block(..) = body.value.kind
// Skip the lint if the body is not safe, so as not to suggest `for … in … unsafe {}`
// and suggesting `for … in … { unsafe { } }` is a little ugly.
&& let ExprKind::Block(Block { rules: BlockCheckMode::DefaultBlock, .. }, ..) = body.value.kind
Copy link
Member

@J-ZhengLi J-ZhengLi Mar 21, 2024

Choose a reason for hiding this comment

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

In case you ever want to support the ugly suggestion 😂, you may want to take rules out, then offer the suggestion accordingly, such as:

if let BlockCheckMode::UnsafeBlock(_) = rules {
    Sugg::hir_with_applicability(cx, body.value, "..", &mut applicability).blockify()
} else {
    Sugg::hir_with_applicability(cx, body.value, "..", &mut applicability)
}

at line 102.

Which gives the correct suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.blockify()? huh.

@bend-n
Copy link
Contributor Author

bend-n commented Mar 21, 2024

Also, make sure you put a fixes: with the issue number which you're trying to fix (I believe it's #12514 correct?) in your PR summary, therefore when this got merged, that issue will be closed automatically by bot.

huh, i just found the issue on my own, that person seems to have discovered it at nearly the exact same time as me.

@J-ZhengLi
Copy link
Member

J-ZhengLi commented Mar 22, 2024

huh, i just found the issue on my own, that person seems to have discovered it at nearly the exact same time as me.

Well... I saw both that one issue and your PR were the first on the list, and that issue was posted a few hours prior, so I thought you were trying to fix that issue, oops 😅

@llogiq
Copy link
Contributor

llogiq commented Apr 30, 2024

@bend-n I see a warning of:
The head ref may contain hidden characters: "\u{1F980}"

Can you fix that? It's probably some sort of wide space in some comment. Also I'd like to see a "Known Issues" section in the docs that tells people they should expect the lint not to trigger for unsafe blocks.

@bend-n
Copy link
Contributor Author

bend-n commented May 1, 2024

please lookup u+1f980:
image
and the head ref includes the branch name, which you cant easily change.

@llogiq
Copy link
Contributor

llogiq commented May 2, 2024

Thank you both for the clarification and the fix.

@bors r+

@bors
Copy link
Collaborator

bors commented May 2, 2024

📌 Commit a3ef100 has been approved by llogiq

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented May 2, 2024

⌛ Testing commit a3ef100 with merge a2bd02b...

@bors
Copy link
Collaborator

bors commented May 2, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing a2bd02b to master...

@bors bors merged commit a2bd02b into rust-lang:master May 2, 2024
5 checks passed
@bend-n bend-n deleted the 🦀 branch May 2, 2024 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

needless-for-each does not add braces before unsafe block
7 participants