Skip to content

Add lint for panic!("{}") - #78088

Merged
bors merged 28 commits into
rust-lang:masterfrom
fusion-engineering-forks:panic-fmt-lint
Nov 20, 2020
Merged

Add lint for panic!("{}")#78088
bors merged 28 commits into
rust-lang:masterfrom
fusion-engineering-forks:panic-fmt-lint

Conversation

@m-ou-se

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

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor

r? @estebank

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

@rust-highfiverust-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
Copy Markdown
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..)

Comment threadcompiler/rustc_lint/src/panic_fmt.rs Outdated
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
MemberAuthor

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).

Comment threadcompiler/rustc_expand/src/mbe/macro_rules.rs Outdated
Comment threadsrc/test/ui/panic-brace.stderr Outdated
@m-ou-se
m-ou-seforce-pushed the panic-fmt-lint branch 2 times, most recently from f6ef176 to aaea4faCompareNovember 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
Copy Markdown
MemberAuthor

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

@m-ou-sem-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
Copy Markdown
Contributor

@bors r+

@bors

bors commented Nov 19, 2020

Copy link
Copy Markdown
Collaborator

📌 Commit a125ef2 has been approved by estebank

@borsbors 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

bors commented Nov 20, 2020

Copy link
Copy Markdown
Collaborator

⌛ Testing commit a125ef2 with merge 74285eb...

@bors

bors commented Nov 20, 2020

Copy link
Copy Markdown
Collaborator

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

@borsbors added the merged-by-bors This PR was explicitly merged by bors. label Nov 20, 2020
@bors
bors merged commit 74285eb into rust-lang:masterNov 20, 2020
@rustbotrustbot added this to the 1.50.0 milestone Nov 20, 2020
@flip1995

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

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

@petrochenkov

Copy link
Copy Markdown
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

m-ou-se commented Jan 23, 2021

Copy link
Copy Markdown
MemberAuthor

@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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsmerged-by-borsThis PR was explicitly merged by bors.S-waiting-on-borsStatus: 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.

14 participants

@m-ou-se@rust-highfive@matthiaskrgr@bors@estebank@jonas-schievink@rust-log-analyzer@flip1995@pthariensflame@petrochenkov@nagisa@Aaron1011@bjorn3@rustbot