New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make sure checked type implements Try
trait when linting [question_mark
]
#12563
Conversation
r? @Alexendoo rustbot has assigned @Alexendoo. Use |
731985b
to
765a52c
Compare
question_mark
]Try
trait when linting [question_mark
]
fn init_expr_can_use_question_mark(cx: &LateContext<'_>, init_expr: &Expr<'_>) -> bool { | ||
let init_ty = cx.typeck_results().expr_ty_adjusted(init_expr); | ||
cx.tcx | ||
.lang_items() | ||
.try_trait() | ||
.map_or(false, |did| implements_trait(cx, init_ty, did, &[])) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this pattern come up often? I think I have seen something like this before. Perhaps we could have this in the utils mod.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cx.tcx.lang_items.something().map_or(false, |id| some_util(...))
comes up fairly often, some utils have different variants that take lang items or diagnostic names, maybe we could do something like
trait Definition {
fn def_id(&self) -> Option<DefId>;
}
And impl it for DefId
, LangItem
, Symbol
(for diagnostic names)
Then you could do implements_trait(..., LangItem::Try)
or implements_trait(.., sym::Default)
. Could use it to get unify a good few utils, but things taking a trait is going to be slightly more difficult to understand
Thanks! @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
(indirectly) fixes: #12412 and fixes: #11983
changelog: make sure checked type implements
Try
trait when linting [question_mark
]