Skip to content

Add new try_from_instead_of_from_str lint#17030

Open
GuillaumeGomez wants to merge 1 commit 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 1 commit into
rust-lang:masterfrom
GuillaumeGomez:try_from_instead_of_from_str

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Member

@GuillaumeGomez GuillaumeGomez commented May 18, 2026

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

rustbot commented May 18, 2026

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

github-actions Bot commented May 18, 2026

Lintcheck changes for 196b173

Lint Added Removed Changed
clippy::try_from_instead_of_from_str 14 0 0

This comment will be updated if you push new changes

@GuillaumeGomez GuillaumeGomez force-pushed the try_from_instead_of_from_str branch 3 times, most recently from c67930d to 02a96bd Compare May 18, 2026 02:28
@GuillaumeGomez GuillaumeGomez force-pushed the try_from_instead_of_from_str branch from 02a96bd to 196b173 Compare May 18, 2026 02:34
Copy link
Copy Markdown
Contributor

@Jarcho Jarcho left a comment

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 on lines +56 to +71
let ItemKind::Impl(imp) = item.kind else { return };
let Some(of_trait) = imp.of_trait else { return };
let Some(trait_def_id) = of_trait.trait_ref.trait_def_id() else {
return;
};
if !cx.tcx.is_diagnostic_item(sym::TryFrom, trait_def_id) {
return;
}

let impl_def_id = item.owner_id.to_def_id();
let trait_ref = cx.tcx.impl_trait_ref(impl_def_id);

let instantiated_trait_ref = trait_ref.instantiate_identity().skip_norm_wip();

if let Some(from_arg) = instantiated_trait_ref.args.get(1)
&& let Some(from_ty) = from_arg.as_type()
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.

Pleas just put all these in the same if let chain.

Comment on lines +79 to +87
for assoc_item in imp.items {
match cx.tcx.hir_expect_impl_item(assoc_item.owner_id.def_id).kind {
ImplItemKind::Type(ty) => err_ty = snippet_opt(cx, ty.span),
ImplItemKind::Fn(_, body_id) => {
fn_body = snippet_opt(cx, cx.tcx.hir_span_with_body(body_id.hir_id));
},
ImplItemKind::Const(..) => {},
}
}
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 shouldn't be a loop. There are always exactly two items when implementing TryFrom

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

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest impl FromStr instead of TryFrom<&str>

3 participants