Skip to content

Fix never_loop forget to remove break in suggestion #15064

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

profetia
Copy link
Contributor

@profetia profetia commented Jun 16, 2025

Closes #15007

Removes the break stmt and the dead code after it when appropriate.

changelog: [never_loop] Make sure to remove break in suggestions

@rustbot
Copy link
Collaborator

rustbot commented Jun 16, 2025

r? @Alexendoo

rustbot has assigned @Alexendoo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 16, 2025
@profetia profetia force-pushed the issue15007 branch 2 times, most recently from d2ee798 to 4f18d7f Compare June 17, 2025 13:13
@blyxyas
Copy link
Member

blyxyas commented Jul 11, 2025

Alexendoo's a little bit over the top on capacity (32 assigned PRs)
I'll shave off his last uncommented PRs.

r? @blyxyas

@rustbot rustbot assigned blyxyas and unassigned Alexendoo Jul 11, 2025
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Some starting review, mainly on readability.

for_to_if_let_sugg(cx, iterator, pat),
)];
suggestions.extend(inner.iter().map(|span| (*span, String::new())));
Copy link
Member

Choose a reason for hiding this comment

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

(In tandem with the last suggestion)
How about a comment here?

Suggested change
suggestions.extend(inner.iter().map(|span| (*span, String::new())));
// Make sure to clear up the diverging sites when we remove a loopp.
suggestions.extend(break_spans.iter().map(|span| (*span, String::new())));

@@ -20,7 +22,7 @@ pub(super) fn check<'tcx>(
for_loop: Option<&ForLoop<'_>>,
) {
match never_loop_block(cx, block, &mut Vec::new(), loop_id) {
NeverLoopResult::Diverging => {
NeverLoopResult::Diverging(ref inner) => {
Copy link
Member

Choose a reason for hiding this comment

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

A better variable name could help the readability here.

Suggested change
NeverLoopResult::Diverging(ref inner) => {
NeverLoopResult::Diverging(ref break_spans) => {

enum NeverLoopResult {
/// A continue may occur for the main loop.
MayContinueMainLoop,
/// We have not encountered any main loop continue,
/// but we are diverging (subsequent control flow is not reachable)
Diverging,
Diverging(Vec<Span>),
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about?

Suggested change
Diverging(Vec<Span>),
Diverging { break_spans: Vec<Span> },

@@ -118,7 +124,10 @@ fn combine_branches(b1: NeverLoopResult, b2: NeverLoopResult) -> NeverLoopResult
NeverLoopResult::MayContinueMainLoop
},
(NeverLoopResult::Normal, _) | (_, NeverLoopResult::Normal) => NeverLoopResult::Normal,
(NeverLoopResult::Diverging, NeverLoopResult::Diverging) => NeverLoopResult::Diverging,
(NeverLoopResult::Diverging(mut inner1), NeverLoopResult::Diverging(mut inner2)) => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
(NeverLoopResult::Diverging(mut inner1), NeverLoopResult::Diverging(mut inner2)) => {
(NeverLoopResult::Diverging(mut break_spans1), NeverLoopResult::Diverging(mut break_spans2)) => {

@@ -159,6 +168,38 @@ fn stmt_to_expr<'tcx>(stmt: &Stmt<'tcx>) -> Option<(&'tcx Expr<'tcx>, Option<&'t
}
}

fn stmt_source_span(stmt: &Stmt<'_>) -> Span {
let call_span = stmt.span.source_callsite();
// if it is a macro call, the span will missing the trailing semicolon
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// if it is a macro call, the span will missing the trailing semicolon
// if it is a macro call, the span will be missing the trailing semicolon

if stmt.span == call_span {
return call_span;
}
call_span.with_hi(call_span.hi() + BytePos(1))
Copy link
Member

Choose a reason for hiding this comment

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

You should probably check the statement kind, because there are statements with explicitly no semicolon.

call_span.with_hi(call_span.hi() + BytePos(1))
}

fn expr_find_remove_span(cx: &LateContext<'_>, expr: &Expr<'_>) -> Vec<Span> {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fn expr_find_remove_span(cx: &LateContext<'_>, expr: &Expr<'_>) -> Vec<Span> {
/// Returns a Vec of all the individual spans after the highlighted expression in a block
fn all_spans_after_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> Vec<Span> {

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

never_loop should also remove break labels that were inside the loop
4 participants