Skip to content
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

Improve non_fmt_panics suggestion based on trait impls. #88083

Merged
merged 5 commits into from
Aug 17, 2021

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Aug 16, 2021

This improves the non_fmt_panics lint suggestions by checking first which trait (Display or Debug) are actually implemented on the type.

Fixes #87313

Fixes #87999

Before:

help: add a "{}" format string to Display the message
  |
2 |     panic!("{}", Some(1));
  |            +++++
help: or use std::panic::panic_any instead
  |
2 |     std::panic::panic_any(Some(1));
  |     ~~~~~~~~~~~~~~~~~~~~~

After:

help: add a "{:?}" format string to use the Debug implementation of `Option<i32>`
  |
2 |     panic!("{:?}", Some(1));
  |            +++++++
help: or use std::panic::panic_any instead
  |
2 |     std::panic::panic_any(Some(1));
  |     ~~~~~~~~~~~~~~~~~~~~~

r? @estebank

@m-ou-se m-ou-se added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-edition-2021 Area: The 2021 edition labels Aug 16, 2021
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 16, 2021
Comment on lines +148 to +154
let fmt_applicability = if suggest_panic_any {
// If we can use panic_any, use that as the MachineApplicable suggestion.
Applicability::MaybeIncorrect
} else {
// If we don't suggest panic_any, using a format string is our best bet.
Applicability::MachineApplicable
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this logic reversed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It marks the format-string suggestion (adding "{}" or "{:?}") as machine applicable only when the panic_any suggestion isn't, as there shouldn't be more than one alternative marked as machine applicable.

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except for the concern around applicability.

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Aug 17, 2021

📌 Commit ab8cbc3 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 Aug 17, 2021
@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 17, 2021

Raising priority so it can be part of the edition migration crater run.

@bors p=1

@m-ou-se m-ou-se added this to Migration crater run blockers in 2021 Edition Blockers Aug 17, 2021
@m-ou-se m-ou-se moved this from Migration crater run blockers to Not-really blockers (?) in 2021 Edition Blockers Aug 17, 2021
@bors
Copy link
Contributor

bors commented Aug 17, 2021

⌛ Testing commit ab8cbc3 with merge d83da1d...

@bors
Copy link
Contributor

bors commented Aug 17, 2021

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 17, 2021
@bors bors merged commit d83da1d into rust-lang:master Aug 17, 2021
2021 Edition Blockers automation moved this from Not-really blockers (?) to Completed items Aug 17, 2021
@rustbot rustbot added this to the 1.56.0 milestone Aug 17, 2021
@m-ou-se m-ou-se deleted the non-fmt-panics-suggest-debug branch August 17, 2021 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2021 Area: The 2021 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
2021 Edition Blockers
  
Completed items
5 participants