Enhance never loop#17145
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @samueltardieu (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 (
Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
7296f2a to
1a84f8b
Compare
|
|
||
| let result = combine_seq(result, || { | ||
| if cx.typeck_results().expr_ty(expr).is_never() { | ||
| let non_obvious_spans = find_non_obvious_spans(cx, expr); |
There was a problem hiding this comment.
This is too much work before we even know that the lint will trigger.
Instead of storing the non-obvious spans, you should store expr.hir_id (and rename the field to non_obvious_exprs). Then, when you know that the lint will trigger, you can convert each HirId to &Expr<'_> (by calling cx.tcx.hir_expect_expr(hir_id)) and compute the spans. This way, the lookup for non obvious spans only happens if the lint triggers.
|
Reminder, once the PR becomes ready for a review, use |
1a84f8b to
255a287
Compare
This comment has been minimized.
This comment has been minimized.
| let non_obvious_spans: Vec<Span> = non_obvious_exprs | ||
| .iter() | ||
| .map(|hir_id| cx.tcx.hir_expect_expr(*hir_id)) | ||
| .flat_map(|expr| find_non_obvious_spans(cx, expr)) | ||
| .collect(); |
There was a problem hiding this comment.
No need to materialize a Vec here, an Iterator will do:
| let non_obvious_spans: Vec<Span> = non_obvious_exprs | |
| .iter() | |
| .map(|hir_id| cx.tcx.hir_expect_expr(*hir_id)) | |
| .flat_map(|expr| find_non_obvious_spans(cx, expr)) | |
| .collect(); | |
| let non_obvious_spans = non_obvious_exprs | |
| .iter() | |
| .map(|hir_id| cx.tcx.hir_expect_expr(*hir_id)) | |
| .flat_map(|expr| find_non_obvious_spans(cx, expr)); |
| let non_obvious_exprs = vec![expr.hir_id]; | ||
|
|
||
| NeverLoopResult::Diverging { | ||
| break_spans: vec![], | ||
| never_spans: all_spans_after_expr(cx, expr), | ||
| non_obvious_exprs, | ||
| } |
There was a problem hiding this comment.
Creating a variable is a bit overkill here, it doesn't make the code clearer and breaks symmetry with never_spans.
| let non_obvious_exprs = vec![expr.hir_id]; | |
| NeverLoopResult::Diverging { | |
| break_spans: vec![], | |
| never_spans: all_spans_after_expr(cx, expr), | |
| non_obvious_exprs, | |
| } | |
| NeverLoopResult::Diverging { | |
| break_spans: vec![], | |
| never_spans: all_spans_after_expr(cx, expr), | |
| non_obvious_exprs: vec![expr.hir_id], | |
| } |
|
|
||
| for_each_expr_without_closures(e, |expr: &'tcx Expr<'tcx>| -> ControlFlow<(), Descend> { | ||
| if cx.typeck_results().expr_ty(expr).is_never() && !expr.span.from_expansion() { | ||
| match expr.kind { |
There was a problem hiding this comment.
How did you choose the list here? Can you add a comment so that future kinds can be added if needed?
I think I would have preferred if you listed the 27 missing variants instead of _ so that if a new ExprKind is added it has to be handled properly here.
| loop { | ||
| //~^ never_loop | ||
|
|
||
| // clippy::never_loop |
255a287 to
c3edca1
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
c3edca1 to
13915be
Compare
changelog: [
never_loop]: add notes for non trivial cases to indicate why a loop is detected as non terminatingWith these changes the lint is able to report non trivial diverging expressions that do not allow the loop to ever execute one iteration. I interpreted “non-trivial” expressions as those not matched by the previous cases in
never_loop_expr.This is my first contribution to Clippy. I'm sorry if I misunderstood the issue or if the code is not good enough. Any feedback is appreciated, and I'm ready to implement any changes needed.
fixes #16056