Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Geobert opened this issue Apr 28, 2021 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Geobert
Copy link

Geobert commented Apr 28, 2021

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/

@Geobert Geobert added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 28, 2021
@ptrojahn
Copy link
Contributor

ptrojahn commented May 8, 2021

This was mostly fixed by a428ab1 already:

warning: panic message is not a string literal
  --> scratchpad.rs:23:27
   |
23 | 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: the panic!() macro supports formatting, so there's no need for the format!() macro here
help: remove the `format!(..)` macro call
   |
23 | assert!(version == "1.0", "test failure text: {}", version);
   |                          --                              --

ptrojahn added a commit to ptrojahn/rust that referenced this issue May 9, 2021
This warning always referenced panic! even in case of an
assert. Related to rust-lang#84656
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 9, 2021
Improve "panic message is not a string literal" warning

This warning always referenced panic! even in case of an
assert. Related to rust-lang#84656
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 10, 2021
Improve "panic message is not a string literal" warning

This warning always referenced panic! even in case of an
assert. Related to rust-lang#84656
@ptrojahn
Copy link
Contributor

@jackh726 I think we can close this now.

@jackh726
Copy link
Member

Fixed in #85075

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants