Skip to content

Suboptimal diagnostic for assert_eq! mistyped as assert! #148476

Closed as duplicate

Description

@ada4a

Code

fn foo(a: i32, b: i32) {
    assert!(a, b);
}

Current output

error: format argument must be a string literal
 --> src/lib.rs:5:16
  |
5 |     assert!(a, b);
  |                ^
  |
help: you might be missing a string literal to format with
  |
5 |     assert!(a, "{}", b);
  |                +++++

error[E0308]: mismatched types
 --> src/lib.rs:5:5
  |
5 |     assert!(a, b);
  |     ^^^^^^^^^^^^^ expected `bool`, found `i32`

Desired output

error: format argument must be a string literal
 --> src/lib.rs:5:16
  |
5 |     assert!(a, b);
  |                ^
  |
help: you might have meant to use `assert_eq!`
  |
5 |     assert_eq!(a, b);
  |           +++

error[E0308]: mismatched types
 --> src/lib.rs:5:5
  |
5 |     assert!(a, b);
  |     ^^^^^^^^^^^^^ expected `bool`, found `i32`

Rationale and extra context

When the two arguments have the same types (that is not bool), it's imo more likely that the user meant to use assert_eq!. In my case, this happened when I wanted to change an assert! to assert_eq!, but only replaced == with ,, forgetting to change the name as well.

Other cases

Rust Version

rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: x86_64-unknown-linux-gnu
release: 1.91.0
LLVM version: 21.1.2

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Nov 4, 2025
  2. added
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Nov 4, 2025
  3. fmease commented on Nov 5, 2025

    @fmease
    Member
  4. ada4a commented on Nov 5, 2025

    @ada4a
    ContributorAuthor

    Looks like a complete duplicate... Sorry, I did try to search through existing issues, but it's a bit hard with keywords like this

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 lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.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