Skip to content

feat: new lint non_binding_let_else#17136

Open
uhiovk wants to merge 2 commits into
rust-lang:masterfrom
uhiovk:non-binding-let-else
Open

feat: new lint non_binding_let_else#17136
uhiovk wants to merge 2 commits into
rust-lang:masterfrom
uhiovk:non-binding-let-else

Conversation

@uhiovk

@uhiovk uhiovk commented Jun 1, 2026

Copy link
Copy Markdown

changelog: [non_binding_let_else]: add new lint

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 1, 2026
@rustbot

rustbot commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

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 (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
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@uhiovk uhiovk force-pushed the non-binding-let-else branch from 126e538 to 3e10f37 Compare June 1, 2026 21:12
@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Jun 1, 2026
@uhiovk uhiovk force-pushed the non-binding-let-else branch 2 times, most recently from 09839f8 to cfc42c2 Compare June 1, 2026 21:47
@uhiovk

uhiovk commented Jun 1, 2026

Copy link
Copy Markdown
Author

Sorry, I messed up with #16932. I'm not very familiar with Git and GitHub operations, hope this isn't causing any trouble.

@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown

Lintcheck changes for 12571df

Lint Added Removed Changed
clippy::non_binding_let_else 1 0 0

This comment will be updated if you push new changes

@uhiovk uhiovk force-pushed the non-binding-let-else branch from 91bffa7 to e5318ed Compare June 1, 2026 22:44

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should look at this discussion for another lint converting between matches! and equality (or assignment in your case).

View changes since this review

Comment on lines +80 to +101
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
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That would be 1.98.0 or later.

/// if !matches!(result, Ok(_)) { panic!() }
/// ```
#[clippy::version = "1.97.0"]
pub NON_BINDING_LET_ELSE,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure this is sufficiently frowned upon to be in style. Maybe restriction.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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.

@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 Jun 2, 2026
@rustbot

rustbot commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@samueltardieu

Copy link
Copy Markdown
Member

Also, you should include tests where the let else, or part of it, comes from declarative or procedural macros.

@uhiovk uhiovk force-pushed the non-binding-let-else branch from e5318ed to 12571df Compare June 4, 2026 23:27
@uhiovk uhiovk requested a review from samueltardieu June 4, 2026 23:52

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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

View changes since this review

@rustbot rustbot assigned samueltardieu and unassigned Jarcho Jun 5, 2026
@rustbot

rustbot commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly f1c1d90) made this pull request unmergeable. Please resolve the merge conflicts.

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

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP 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.

4 participants