Skip to content

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
Collaborator

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

@bors
Copy link
Collaborator

bors commented Aug 17, 2021

⌛ Testing commit ab8cbc3 with merge d83da1d...

@bors
Copy link
Collaborator

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
@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
None yet
Development

Successfully merging this pull request may close these issues.

non_panic_fmt lint suggestion should check what traits are implemented non_fmt_panics cannot migrate to 2021 for assert with non-string payload
5 participants