Skip to content

Conversation

ada4a
Copy link
Contributor

@ada4a ada4a commented Aug 13, 2025

fixes #15376

changelog: [equatable_if_let]: don't suggest = in const context

@rustbot
Copy link
Collaborator

rustbot commented Aug 13, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
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 Aug 13, 2025
@ada4a ada4a force-pushed the equatable-if-in-consts branch from 4b19a31 to 96838e2 Compare August 14, 2025 14:51
@ada4a ada4a changed the title fix equatable_if_let: FP in const context fix equatable_if_let: don't suggest = in const context Aug 14, 2025
//~^ ERROR: this pattern matching can be expressed using `matches!`

// FIXME:suggests `==`
//const _: u32 = if let Some(NonConstEq) = None { 0 } else { 1 };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

why on earth does this happen

Copy link
Contributor

Choose a reason for hiding this comment

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

is_stable_const_fn doesn't handle this case at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you know of a way to make it to?..

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing this part:

cx.tcx
    .trait_of_assoc(def_id)
    .and_then(|trait_def_id| cx.tcx.lookup_const_stability(trait_def_id))

Needs to be changed to getting the container and not the trait.

Copy link
Member

Choose a reason for hiding this comment

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

I haven't followed this discussion closely, but it might be something similar to what I had to do in 86beecc. Maybe this could get factored into a utility function taking both the trait id and the concrete type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't completely get that code if I'm being honest. If I understand correctly, it checks that:

  1. the given trait:
    1. defines one and only function
    2. and that function is const
  2. the given type has at least one (non-blanket) impl of that trait
  3. if there are multiple such impls, checks that the implementation of the one and only function is const as well

(1.i) is fine for assign_op_patterns, since the traits checked there only ever define one function. And it should be okay for PartialEq as well, since eq is its first method. But I think that is a potential footgun worth mentioning in the docs of this function if we do move it to clippy_utils

But am I understanding correctly that (3) is a bit too strict? After all, only the impl that applies to the given situation should have its constness considered -- but it's probably unrealistic to try and find out which impl that is, from inside Clippy. If my understanding is correct, I'd add this as a comment to the function.

@ada4a ada4a force-pushed the equatable-if-in-consts branch from 43721b0 to d5a3511 Compare August 19, 2025 19:02
@ada4a ada4a force-pushed the equatable-if-in-consts branch from d5a3511 to 194bcde Compare August 19, 2025 21:34
ada4a and others added 7 commits August 22, 2025 15:03
turns out `is_in_const_context` wasn't even supposed to be put in the
main let-chain, since it's only relevant for the
`is_structural_partial_eq` case

Co-authored-by: yanglsh <[email protected]>
we've created it already, so might as well..
before this, the lint would suggest `==` even for types that aren't
`const PartialEq`

still doesn't work on nested types for some reason
@ada4a ada4a force-pushed the equatable-if-in-consts branch from 194bcde to 283f89d Compare August 22, 2025 13:32
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.

equatable-if-let not stable for consts yet
5 participants