Skip to content

Suggest swapping == operands when unsuffixed literals are involved - #166178

Open
Yogeshwaran-Projects wants to merge 1 commit into
rust-lang:mainfrom
Yogeshwaran-Projects:rock/binop-swap-sugg
Open

Yogeshwaran-Projects wants to merge 1 commit into
rust-lang:mainfrom
Yogeshwaran-Projects:rock/binop-swap-sugg

Conversation

@Yogeshwaran-Projects

@Yogeshwaran-Projects Yogeshwaran-Projects commented Nov 19, 2026 •

Copy link
Copy Markdown

Part of #165968

The “consider swapping the equality” suggestion already existed for == / !=, but it could be dropped when an untyped literal made the reverse-PartialEq check return only “maybe” (for example [1, 2, 3] == vec![1, 2, 3]). Cases such as 4 == T(4) already produced the suggestion. This PR assigns the default types (i32 / f64) to those literals before the existing check runs, allowing the swap suggestion and the accompanying note to appear. The default types are used only for the check; the printed message still shows {integer}. The same change is applied on the E0308 path. No suggestion is emitted when an operand type is an error, and other binary operators (such as * and +) are outside the scope of this PR. For assert_eq! the note was previously missing because the source text inside the std macro is not readable; a code suggestion is still omitted on purpose, since there is nothing the user can swap inside the macro (see #139050).

Before:

error[E0277]: can't compare `[{integer}; 3]` with `Vec<{integer}>`
 --> src/main.rs:4:15
  |
4 |     let _ = a == v;
  |               ^^ no implementation for `[{integer}; 3] == Vec<{integer}>`
  |
  = help: the trait `PartialEq<Vec<{integer}>>` is not implemented for `[{integer}; 3]`
help: consider dereferencing here
  |
4 |     let _ = a == *v;
  |                  +

After:

error[E0277]: can't compare `[{integer}; 3]` with `Vec<{integer}>`
  --> $DIR/partialeq_suggest_swap_unsuffixed_literal.rs:24:15
   |
LL |     let _ = a == v;
   |               ^^ no implementation for `[{integer}; 3] == Vec<{integer}>`
   |
   = help: the trait `PartialEq<Vec<{integer}>>` is not implemented for `[{integer}; 3]`
   = note: `Vec<{integer}>` implements `PartialEq<[{integer}; 3]>`
help: consider dereferencing here
   |
LL |     let _ = a == *v;
   |                  +
help: consider swapping the equality
   |
LL -     let _ = a == v;
LL +     let _ = v == a;
   |

For assert_eq!(a, v) only the note is added, with no suggestion:

   = note: `Vec<{integer}>` implements `PartialEq<[{integer}; 3]>`

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 19, 2026
@rustbot

rustbot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @tmiasko (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main 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.

This branch has not been deployed

No deployments
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants