Don't assume lang items will exist.

This commit is contained in:
Jason Newcomb 2021-03-08 11:08:52 -05:00
parent d0d5232c72
commit f2d917e3b1
No known key found for this signature in database
GPG Key ID: DA59E8643A37ED06
4 changed files with 20 additions and 8 deletions

View File

@ -293,7 +293,12 @@ fn check_ord_partial_ord<'tcx>(
/// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
fn check_copy_clone<'tcx>(cx: &LateContext<'tcx>, item: &Item<'_>, trait_ref: &TraitRef<'_>, ty: Ty<'tcx>) {
if cx.tcx.lang_items().clone_trait() == trait_ref.trait_def_id() {
if cx
.tcx
.lang_items()
.clone_trait()
.map_or(false, |id| Some(id) == trait_ref.trait_def_id())
{
if !is_copy(cx, ty) {
return;
}

View File

@ -358,10 +358,13 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
fn visit_poly_trait_ref(&mut self, poly_tref: &'tcx PolyTraitRef<'tcx>, tbm: TraitBoundModifier) {
let trait_ref = &poly_tref.trait_ref;
if CLOSURE_TRAIT_BOUNDS
.iter()
.any(|&item| trait_ref.trait_def_id() == self.cx.tcx.lang_items().require(item).ok())
{
if CLOSURE_TRAIT_BOUNDS.iter().any(|&item| {
self.cx
.tcx
.lang_items()
.require(item)
.map_or(false, |id| Some(id) == trait_ref.trait_def_id())
}) {
let mut sub_visitor = RefVisitor::new(self.cx);
sub_visitor.visit_trait_ref(trait_ref);
self.nested_elision_site_lts.append(&mut sub_visitor.all_lts());

View File

@ -81,7 +81,7 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
if ident_eq(name, obj) && method.ident.name == sym::clone;
if let Some(fn_id) = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id);
if let Some(trait_id) = cx.tcx.trait_of_item(fn_id);
if Some(trait_id) == cx.tcx.lang_items().clone_trait();
if cx.tcx.lang_items().clone_trait().map_or(false, |id| id == trait_id);
// no autoderefs
if !cx.typeck_results().expr_adjustments(obj).iter()
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))));

View File

@ -239,7 +239,10 @@ pub fn is_ty_param_lang_item(cx: &LateContext<'_>, qpath: &QPath<'tcx>, item: La
if let TyKind::Path(qpath) = &ty.kind {
cx.qpath_res(qpath, ty.hir_id)
.opt_def_id()
.and_then(|id| (cx.tcx.lang_items().require(item) == Ok(id)).then(|| ty))
.map_or(false, |id| {
cx.tcx.lang_items().require(item).map_or(false, |lang_id| id == lang_id)
})
.then(|| ty)
} else {
None
}
@ -256,7 +259,8 @@ pub fn is_ty_param_diagnostic_item(
if let TyKind::Path(qpath) = &ty.kind {
cx.qpath_res(qpath, ty.hir_id)
.opt_def_id()
.and_then(|id| cx.tcx.is_diagnostic_item(item, id).then(|| ty))
.map_or(false, |id| cx.tcx.is_diagnostic_item(item, id))
.then(|| ty)
} else {
None
}