Skip to content

Diagnostics: Suggest replacing (problematic) assert!(x, y) with assert_eq!(x, y) #57507

Open

Description

@matthiaskrgr
fn main() {
    let x = 3;
    assert!(x, 3);
}

This currently gives an error like so:

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
3 |     assert!(x, 3);
  |     ^^^^^^^^^^^^^^ expected bool, found integer
  |
  = note: expected type `bool`
             found type `{integer}`

error: aborting due to previous error

If arguments 1 and 2 are the same type, it would be great if we could instead suggest using assert_eq! instead (preferably with rustfix applicable hint)!

Alternatively we could suggest transforming x, 3 to x == 3 but I guess assert_eq!() is more idiomatic.

Activity

  1. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Sep 26, 2019
  2. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Oct 18, 2019
  3. changed the title [-]diagnostics: suggets assert_eq!() instead of assert!() where appropriate[/-] [+]Diagnostics: Suggest `assert_eq!(x, y)` for bad `assert!(x, y)`[/+] on Jul 24, 2025
  4. added
    D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.
    and removed
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    on Jul 24, 2025
  5. changed the title [-]Diagnostics: Suggest `assert_eq!(x, y)` for bad `assert!(x, y)`[/-] [+]Diagnostics: Suggest replacing (problematic) `assert!(x, y)` with `assert_eq!(x, y)`[/+] on Jul 24, 2025
  6. bend-n commented on Jul 25, 2025

    @bend-n
    Contributor

    with #44838, would it be better to suggest x == 3?

  7. estebank commented on Aug 5, 2025

    @estebank
    Contributor

    CC #122661, as it might require the approach to detect this case to be different.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions