Skip to content

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Oct 18, 2020

This adds a lint that warns about panic!("{}").

panic!(msg) invocations with a single argument use their argument as panic payload literally, without using it as a format string. The same holds for assert!(expr, msg).

This lints checks if msg is a string literal (after expansion), and warns in case it contained braces. It suggests to insert "{}", to use the message literally, or to add arguments to use it as a format string.

image

This lint is also a good starting point for adding warnings about panic!(not_a_string) later, once panic_any() becomes a stable alternative.

@rust-highfive
Copy link
Contributor

r? @estebank

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 18, 2020
The beta compiler doesn't accept rustc_diagnostic_items on macros yet.
@matthiaskrgr
Copy link
Member

Clippy already has a lint for panic!("{}") https://rust-lang.github.io/rust-clippy/master/index.html#panic_params (not sure about assert! though..)

@m-ou-se
Copy link
Member Author

m-ou-se commented Oct 18, 2020

Heh, interesting. The CI build is failing, because the lint found a problem in existing Rustc code:

image

Looks like it's working as it should. :)

@m-ou-se
Copy link
Member Author

m-ou-se commented Oct 18, 2020

Clippy already has a lint for panic!("{}") https://rust-lang.github.io/rust-clippy/master/index.html#panic_params (not sure about assert! though..)

Clippy does not warn for panic!("{") though, only if it actually looks like a placeholder. This lint is part of moving towards consistent behaviour for panic!() in Rust 2021, so just a Clippy lint isn't enough.

This lint should be extended later to also provide advice on panic!(123) and other uses that should be discouraged (or removed in Rust 2021).

@m-ou-se m-ou-se force-pushed the panic-fmt-lint branch 2 times, most recently from f6ef176 to aaea4fa Compare November 19, 2020 17:19
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Rustc itself now warns for all cases that triggered this lint.
@m-ou-se
Copy link
Member Author

m-ou-se commented Nov 19, 2020

All the clippy issues should be fixed now. Ready for review again. :)

@m-ou-se m-ou-se added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Nov 19, 2020
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Nov 19, 2020

📌 Commit a125ef2 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 19, 2020
@bors
Copy link
Collaborator

bors commented Nov 20, 2020

⌛ Testing commit a125ef2 with merge 74285eb...

@bors
Copy link
Collaborator

bors commented Nov 20, 2020

☀️ Test successful - checks-actions
Approved by: estebank
Pushing 74285eb to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 20, 2020
@bors bors merged commit 74285eb into rust-lang:master Nov 20, 2020
@rustbot rustbot added this to the 1.50.0 milestone Nov 20, 2020
@flip1995
Copy link
Member

Please ping the Clippy team for major changes to Clippy in the Rust repository! We have a lengthy process (by design) in place for deprecating lints.

@m-ou-se m-ou-se deleted the panic-fmt-lint branch November 20, 2020 08:55
@estebank
Copy link
Contributor

@flip1995 apologies, didn't realize that the changes to clippy were being introduced in this PR and not going through clippy's process. I'll keep it in mind for future situations.

@pthariensflame
Copy link
Contributor

Does this need relnotes? Thinking of compatibility notes in particular.

@petrochenkov
Copy link
Contributor

@m-ou-se
We have lint naming conventions and panic_fmt doesn't follow them.
See RFC 344 (allow(panic_fmt) => "allow panic formatting" => what?).

@m-ou-se
Copy link
Member Author

m-ou-se commented Jan 23, 2021

@petrochenkov Good to know, thanks. Now that std::panic::panic_any is stable, this lint needs to be extended to also warn for panic!(123). At that point it'll warn for every panic that does not use format_args formatting, so we could rename it to something like non_fmt_panic.

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 merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.