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 196b173
This comment will be updated if you push new changes |
c67930d to
02a96bd
Compare
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.
| 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() |
There was a problem hiding this comment.
Pleas just put all these in the same if let chain.
| 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(..) => {}, | ||
| } | ||
| } |
There was a problem hiding this comment.
This shouldn't be a loop. There are always exactly two items when implementing TryFrom
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