Add new try_from_instead_of_from_str lint - #17030
Conversation
|
r? @Jarcho rustbot has assigned @Jarcho. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for 2b63015
This comment will be updated if you push new changes |
02a96bd to
196b173
Compare
There was a problem hiding this comment.
You also have to check if the error type is using the lifetime since FromStr can't capture there either. Also please make sure to put the HIR matching code before item identification code. It's generally faster to check since it doesn't got through the query system.
This comment has been minimized.
This comment has been minimized.
196b173 to
ceb2411
Compare
This comment has been minimized.
This comment has been minimized.
aa122c3 to
4acca7d
Compare
4acca7d to
a9d577c
Compare
a9d577c to
44e112d
Compare
|
Handled the lifetimes, moved the params length condition upper and added tests. Ready for a new review. @rustbot ready |
44e112d to
0398277
Compare
|
Applied suggestions. |
8b54996 to
384a8a6
Compare
384a8a6 to
f761827
Compare
|
Applied suggestion to only look at |
|
@rustbot label lint-nominated Forgot to do this earlier. |
|
This lint has been nominated for inclusion. |
|
One last case from the lintcheck results: we shouldn't lint if |
|
Ah indeed. ^^' Adding it as well. |
f761827 to
3fca089
Compare
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// It is more idiomatic to use `FromStr`. |
There was a problem hiding this comment.
This needs to be significantly fleshed out. FromStr is only more idiomatic for parsing the string, not for all conversions. e.g. something like ArrayStr<const N: usize> would make more sense using TryFrom<&str> even though FromStr works.
There was a problem hiding this comment.
I added more information.
There was a problem hiding this comment.
This still needs an explanation of why it's idiomatic, not just a claim that it is. I'm not sure why you added a note about constants since that makes no difference between the two traits.
There was a problem hiding this comment.
Rewrote the explanation.
There was a problem hiding this comment.
The latest version still leaves the reason implied from the mention of str::parse rather than stated outright. Specifically it uses "the intent of the conversion is more explicit" without stating what intent is made more explicit. I would word it more like: "FromStr is the idiomatic trait for parsing a string and should be preferred over TryFrom when doing so. Implementing FromStr allows the the use str:parse to make the intent of the conversion clearer at the conversion site."
Adding a "Known problems" section explaining the lint doesn't pick up on the intent of the conversion would also help. You can use a type like ArrayString as an example where FromStr doesn't express the intent of the conversion (a length limit).
3fca089 to
a8fb74d
Compare
b4d0f69 to
aa59c63
Compare
This comment has been minimized.
This comment has been minimized.
aa59c63 to
b4d0f69
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
Forgot to toggle the label... @rustbot ready |
| && let Some(generics) = snippet_opt(cx, imp.generics.span) | ||
| && let Some(self_ty) = snippet_opt(cx, imp.self_ty.span) | ||
| { | ||
| span_lint_and_sugg( |
There was a problem hiding this comment.
Would be nice to have a second help message with "or implement both FromStr and TryFrom" to help point out that it doesn't have to be one or the other.
There was a problem hiding this comment.
Huuuum... I'm not sure we want to multiply the trait impls for a somewhat similar purpose.
There was a problem hiding this comment.
I was thinking of cases where you can't remove necessarily remove the TryFrom impl. Thinking about this the lint should be using avoid_breaking_exported_api. So for reachable type this could use suggest to either replace the old impl or only add the new one. When avoid_breaking_exported_api the suggestion to replace should be suppressed since it's not a viable option anymore.
There was a problem hiding this comment.
I'll make the suggestion MachineApplicable only in case avoid_breaking_exported_api is disabled.
b4d0f69 to
d738982
Compare
|
Updated the description. Not sure about the second suggestion message though. |
859cd28 to
23695e8
Compare
23695e8 to
2b63015
Compare
|
|
|
☔ The latest upstream changes (possibly #17552) made this pull request unmergeable. Please resolve the merge conflicts. |
View all comments
Fixes #14522.
One big thing: I'm not sure if the lint name is good, so very much up to debate.
changelog: Add new
try_from_instead_of_from_strlint