Skip to content

Warning about "panic message is not a string literal" on assert! could be improved #84656

Closed
@Geobert

Description

@Geobert

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f8bcc7c1d5be93360f283c8662411b59

fn main(){
let version = "12.112a0-0";
assert!(version == "1.0", format!("test failure text: {}", version));
}

The current output is:

warning: panic message is not a string literal
 --> src/main.rs:3:27
  |
3 | assert!(version == "1.0", format!("test failure text: {}", version));
  |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(non_fmt_panic)]` on by default
  = note: this is no longer accepted in Rust 2021
  = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

warning: 1 warning emitted

Ideally the output should look like:

warning: dynamic panic message created using format!
  --> my_project\src\api\tests\test_endpoint.rs:46:9
   |
46 |         format!("BODY: {:#?}", body)
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: The macros `assert!` and [whichever else] can take format arguments,
   = note: so you can instead write this more directly as:
45 |        assert!(whatever,
46 |         "BODY: {:#?}", body
47 |         )
   |

idea from https://old.reddit.com/r/rust/comments/mzpr3p/panic_message_is_not_a_string_literal/gw57qz8/

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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