Skip to content

Add new try_from_instead_of_from_str lint - #17030

Open
GuillaumeGomez wants to merge 2 commits into
rust-lang:masterfrom
GuillaumeGomez:try_from_instead_of_from_str
Open

Add new try_from_instead_of_from_str lint#17030
GuillaumeGomez wants to merge 2 commits into
rust-lang:masterfrom
GuillaumeGomez:try_from_instead_of_from_str

Conversation

@GuillaumeGomez

@GuillaumeGomez GuillaumeGomez commented May 18, 2026

Copy link
Copy Markdown
Member

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_str lint

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 18, 2026
@rustbot

rustbot commented May 18, 2026

Copy link
Copy Markdown
Collaborator

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, llogiq, samueltardieu

@github-actions

github-actions Bot commented May 18, 2026

Copy link
Copy Markdown

Lintcheck changes for 2b63015

Lint Added Removed Changed
clippy::try_from_instead_of_from_str 1 0 0

This comment will be updated if you push new changes

@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch 4 times, most recently from 02a96bd to 196b173 Compare May 18, 2026 02:34

@Jarcho Jarcho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

View changes since this review

Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 21, 2026
@rustbot

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 196b173 to ceb2411 Compare June 3, 2026 15:34
@rustbot

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch 2 times, most recently from aa122c3 to 4acca7d Compare June 3, 2026 16:04
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Thanks for the suggestions @Jarcho. Hopefully I caught all lifetime cases. Updated the code too as you suggested.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 3, 2026
Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 7, 2026
@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 4acca7d to a9d577c Compare June 7, 2026 20:54
@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from a9d577c to 44e112d Compare June 15, 2026 16:17
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Handled the lifetimes, moved the params length condition upper and added tests. Ready for a new review.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 15, 2026
@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 44e112d to 0398277 Compare June 16, 2026 12:13
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Applied suggestions.

@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 8b54996 to 384a8a6 Compare June 17, 2026 21:47
Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 384a8a6 to f761827 Compare June 24, 2026 12:55
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Applied suggestion to only look at where predicates.

@Jarcho

Jarcho commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

@rustbot label lint-nominated

Forgot to do this earlier.

@rustbot rustbot added the lint-nominated Create an FCP-thread on Zulip for this PR label Jun 24, 2026
@rustbot

rustbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

@Jarcho

Jarcho commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

One last case from the lintcheck results: we shouldn't lint if FromStr is already implemented. Should just need a single call to implements_trait. There isn't a diagnostic item for FromStr so you can add it to clippy_utils::paths until a diagnostic item is added.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Ah indeed. ^^'

Adding it as well.

@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from f761827 to 3fca089 Compare June 25, 2026 13:36
Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
///
/// ### Why is this bad?
///
/// It is more idiomatic to use `FromStr`.

@Jarcho Jarcho Jul 6, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added more information.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Rewrote the explanation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread clippy_lints/src/try_from_instead_of_from_str.rs Outdated
Comment thread clippy_lints/src/try_from_instead_of_from_str.rs
@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 3fca089 to a8fb74d Compare July 6, 2026 10:01
@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch 2 times, most recently from b4d0f69 to aa59c63 Compare July 15, 2026 13:54
@rustbot

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from aa59c63 to b4d0f69 Compare July 15, 2026 13:55
@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

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.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Forgot to toggle the label...

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jul 27, 2026
&& let Some(generics) = snippet_opt(cx, imp.generics.span)
&& let Some(self_ty) = snippet_opt(cx, imp.self_ty.span)
{
span_lint_and_sugg(

@Jarcho Jarcho Jul 28, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Huuuum... I'm not sure we want to multiply the trait impls for a somewhat similar purpose.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll make the suggestion MachineApplicable only in case avoid_breaking_exported_api is disabled.

@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from b4d0f69 to d738982 Compare July 28, 2026 13:11
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Updated the description. Not sure about the second suggestion message though.

@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 859cd28 to 23695e8 Compare July 31, 2026 18:05
@GuillaumeGomez
GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 23695e8 to 2b63015 Compare July 31, 2026 18:08
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

avoid_breaking_exported_api is now taken into account. Thanks a lot for all that and sorry for all the back and forth.

@rustbot

rustbot commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #17552) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest impl FromStr instead of TryFrom<&str>

3 participants