]> git.lizzy.rs Git - rust.git/commitdiff
Don't assume lang items will exist.
authorJason Newcomb <jsnewcomb@pm.me>
Mon, 8 Mar 2021 16:08:52 +0000 (11:08 -0500)
committerJason Newcomb <jsnewcomb@pm.me>
Mon, 8 Mar 2021 16:08:52 +0000 (11:08 -0500)
clippy_lints/src/derive.rs
clippy_lints/src/lifetimes.rs
clippy_lints/src/map_clone.rs
clippy_utils/src/lib.rs

index 382a045a3f7cc761ae1fdc66212726cb880de587..594c370f6608f87df00f79f58bd317ce7751f3f1 100644 (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;
         }
index 3807d346f681683e60a4a2efcc70f3ea2c06efe4..33ff01a30e8813c7e85a0963f5c094d6c47b9d31 100644 (file)
@@ -358,10 +358,13 @@ fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
 
     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());
index 0c358e2e538b19b560aa6dfe0e9b4d0784c10c59..4b685c09a0548771101789ac952da24cd873ce25 100644 (file)
@@ -81,7 +81,7 @@ fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
                                 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(..))));
index 3845667802d80bde97e047185ea3a56d34a0eb63..1f62cf5ae7fcfc46a8004509202b7fb48d41d667 100644 (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
     }