feat: new lint non_binding_let_else#17136
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Jarcho (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:
|
126e538 to
3e10f37
Compare
09839f8 to
cfc42c2
Compare
|
Sorry, I messed up with #16932. I'm not very familiar with Git and GitHub operations, hope this isn't causing any trouble. |
|
Lintcheck changes for 12571df
This comment will be updated if you push new changes |
91bffa7 to
e5318ed
Compare
There was a problem hiding this comment.
You should look at this discussion for another lint converting between matches! and equality (or assignment in your case).
| fn can_compare(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool { | ||
| let is_expr_like = pat.walk_short(|p| { | ||
| matches!( | ||
| p.kind, | ||
| PatKind::Expr(..) | ||
| | PatKind::Ref(..) | ||
| | PatKind::Slice(..) | ||
| | PatKind::Tuple(..) | ||
| | PatKind::Struct(..) | ||
| | PatKind::TupleStruct(..) | ||
| ) | ||
| }); | ||
|
|
||
| let ty = cx.typeck_results().pat_ty(pat); | ||
| let type_impl_partialeq = cx | ||
| .tcx | ||
| .lang_items() | ||
| .eq_trait() | ||
| .is_some_and(|id| implements_trait(cx, ty, id, &[ty.into()])); | ||
|
|
||
| is_expr_like && type_impl_partialeq | ||
| } |
There was a problem hiding this comment.
You should check that PartialEq is, recursively, automatically derived. Otherwise nothing guarantees that it will give the same result as a structural match.
See [https://github.com//pull/17018#issuecomment-4467692343](this discussion) in another PR thread.
There was a problem hiding this comment.
I'm not sure how to implement this effeciently as you pointed in that PR. For now, I just lint with MaybeIncorrect for all cases where PartialEq is implemented.
| "let pattern doesn't have bindings", | ||
| help, | ||
| sugg, | ||
| Applicability::MachineApplicable, |
There was a problem hiding this comment.
You can't use MachineApplicable with snippet(), as it may have failed in retrieving the correct source.
|
|
||
| let _ = CELL; | ||
| let _ = &CELL; //~ borrow_interior_mutable_const | ||
| #[allow(clippy::non_binding_let_else)] |
There was a problem hiding this comment.
Please use #[expect] if you know that the lint will trigger. Ditto in other instances.
| /// if opt != Some(42) { unreachable!() } | ||
| /// if !matches!(result, Ok(_)) { panic!() } | ||
| /// ``` | ||
| #[clippy::version = "1.97.0"] |
There was a problem hiding this comment.
That would be 1.98.0 or later.
| /// if !matches!(result, Ok(_)) { panic!() } | ||
| /// ``` | ||
| #[clippy::version = "1.97.0"] | ||
| pub NON_BINDING_LET_ELSE, |
There was a problem hiding this comment.
I'm not sure this is sufficiently frowned upon to be in style. Maybe restriction.
There was a problem hiding this comment.
I think pedantic is fine. This pattern is quite unidiomatic, or at least confusing, so warning about it should not be a kind of restriction.
There was a problem hiding this comment.
I think
pedanticis fine. This pattern is quite unidiomatic, or at least confusing, so warning about it should not be a kind of restriction.
The proposed solution (matches(funcall(), Ok(_))) is not idiomatic either, that would be funcall().is_ok(). We'll see what others will see about the category when we enter the FCP.
|
Reminder, once the PR becomes ready for a review, use |
|
Also, you should include tests where the |
e5318ed to
12571df
Compare
There was a problem hiding this comment.
I'm not sure how to implement this effeciently as you pointed in that PR. For now, I just lint with
MaybeIncorrectfor all cases wherePartialEqis implemented.
This is not enough: when applied, the fix would compile, but it might produce different results. That would be a I-suggestion-causes-bug kind of bug, which is very severe.
Moreover, the implementation of this check could be shared with the other PR in clippy_utils (Cc @GuillaumeGomez).
r? samueltardieu
|
☔ The latest upstream changes (possibly f1c1d90) made this pull request unmergeable. Please resolve the merge conflicts. |
changelog: [
non_binding_let_else]: add new lint