]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #6370 - giraffate:fix_fp_in_unnecessary_lazy_evaluations, r=llogiq...
authorbors <bors@rust-lang.org>
Mon, 7 Dec 2020 15:19:30 +0000 (15:19 +0000)
committerbors <bors@rust-lang.org>
Mon, 7 Dec 2020 15:19:30 +0000 (15:19 +0000)
Fix FP in `unnecessary_lazy_evaluations`

Fix https://github.com/rust-lang/rust-clippy/issues/6343

changelog: Fix FP in `unnecessary_lazy_evaluations`

clippy_lints/src/doc.rs
clippy_lints/src/missing_const_for_fn.rs
clippy_lints/src/needless_pass_by_value.rs
clippy_lints/src/pass_by_ref_or_value.rs
clippy_lints/src/redundant_clone.rs
clippy_lints/src/types.rs
clippy_lints/src/unnecessary_wraps.rs
clippy_lints/src/utils/ast_utils.rs
clippy_lints/src/utils/mod.rs
clippy_lints/src/utils/usage.rs
tests/ui/unnecessary_lazy_eval_unfixable.rs

index edecba57e44f0a38073d8664ff76211aed3174a7..55e4755c278a05df449c899cba206db9615626d8 100644 (file)
@@ -480,7 +480,7 @@ fn has_needless_main(code: &str) -> bool {
                     | ItemKind::ForeignMod(..) => return false,
                     // We found a main function ...
                     ItemKind::Fn(_, sig, _, Some(block)) if item.ident.name == sym::main => {
-                        let is_async = matches!(sig.header.asyncness, Async::Yes{..});
+                        let is_async = matches!(sig.header.asyncness, Async::Yes { .. });
                         let returns_nothing = match &sig.decl.output {
                             FnRetTy::Default(..) => true,
                             FnRetTy::Ty(ty) if ty.kind.is_unit() => true,
index 25245b3dbf08e1274174b992f18abf2631e7d073..38e2ce563eeb2951feda3de9b0ae55633230111c 100644 (file)
@@ -99,7 +99,7 @@ fn check_fn(
                 let has_const_generic_params = generics
                     .params
                     .iter()
-                    .any(|param| matches!(param.kind, GenericParamKind::Const{ .. }));
+                    .any(|param| matches!(param.kind, GenericParamKind::Const { .. }));
 
                 if already_const(header) || has_const_generic_params {
                     return;
index 532c0266946b3f71b183c5361733e812fd588440..5043b7b1fc3c1858743ee4862127e67aed09a4a0 100644 (file)
@@ -90,9 +90,10 @@ fn check_fn(
 
         // Exclude non-inherent impls
         if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
-            if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. } |
-                ItemKind::Trait(..))
-            {
+            if matches!(
+                item.kind,
+                ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
+            ) {
                 return;
             }
         }
index f03facc235e28f748b2297345b555e9c9f1b192d..6a17d654ac943e55f3964cfd9a8b7865c296b53e 100644 (file)
@@ -244,9 +244,10 @@ fn check_fn(
 
         // Exclude non-inherent impls
         if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
-            if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. } |
-            ItemKind::Trait(..))
-            {
+            if matches!(
+                item.kind,
+                ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
+            ) {
                 return;
             }
         }
index f0e507105a6a0ef241ece96d66782c039b35cbd7..06adbb523d706288c4e1a98b8a990fb188af7313 100644 (file)
@@ -390,7 +390,10 @@ fn visit_place(&mut self, place: &mir::Place<'tcx>, ctx: PlaceContext, _: mir::L
         let local = place.local;
 
         if local == self.used.0
-            && !matches!(ctx, PlaceContext::MutatingUse(MutatingUseContext::Drop) | PlaceContext::NonUse(_))
+            && !matches!(
+                ctx,
+                PlaceContext::MutatingUse(MutatingUseContext::Drop) | PlaceContext::NonUse(_)
+            )
         {
             self.used.1 = true;
         }
index 74ba53e6a9a02fbd9c7f90b63ed72e8d58ea00c9..fd74783335d57599bd57b3a1fd4d1fc9b7f75e31 100644 (file)
@@ -1104,7 +1104,9 @@ fn is_empty_block(expr: &Expr<'_>) -> bool {
         expr.kind,
         ExprKind::Block(
             Block {
-                stmts: &[], expr: None, ..
+                stmts: &[],
+                expr: None,
+                ..
             },
             _,
         )
index 5d801511a0b185b32af9e40816f1fc6a68cfd42c..e763da593d49bd8be4bc1185621f1291cfbbc4a7 100644 (file)
@@ -74,7 +74,10 @@ fn check_fn(
         }
 
         if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
-            if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), ..} | ItemKind::Trait(..)) {
+            if matches!(
+                item.kind,
+                ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
+            ) {
                 return;
             }
         }
index 31b4e25411bd39ba24a2aa927d4c28259e24a405..f0267e4c792893b36869f19ed32f5103c8c5569b 100644 (file)
@@ -408,7 +408,10 @@ pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
 }
 
 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
-    matches!((l, r), (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_)))
+    matches!(
+        (l, r),
+        (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
+    )
 }
 
 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
index 3a6b64c90e8f65ef4634ecc7197f2275af7017c9..007e72d129f75673a8c05988133a36b2cb7aacf0 100644 (file)
@@ -1500,7 +1500,7 @@ pub fn is_no_std_crate(krate: &Crate<'_>) -> bool {
 /// ```
 pub fn is_trait_impl_item(cx: &LateContext<'_>, hir_id: HirId) -> bool {
     if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
-        matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. })
+        matches!(item.kind, ItemKind::Impl { of_trait: Some(_), .. })
     } else {
         false
     }
index a7d0ea6ccfbb24e234b23956ef92fc9ff7604320..fc0db7f64ec9550dbc0dc830c6fb5c833a2a6a24 100644 (file)
@@ -116,20 +116,27 @@ pub struct ParamBindingIdCollector {
 }
 impl<'tcx> ParamBindingIdCollector {
     fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
-        let mut finder = ParamBindingIdCollector {
-            binding_hir_ids: Vec::new(),
-        };
-        finder.visit_body(body);
-        finder.binding_hir_ids
+        let mut hir_ids: Vec<hir::HirId> = Vec::new();
+        for param in body.params.iter() {
+            let mut finder = ParamBindingIdCollector {
+                binding_hir_ids: Vec::new(),
+            };
+            finder.visit_param(param);
+            for hir_id in &finder.binding_hir_ids {
+                hir_ids.push(*hir_id);
+            }
+        }
+        hir_ids
     }
 }
 impl<'tcx> intravisit::Visitor<'tcx> for ParamBindingIdCollector {
     type Map = Map<'tcx>;
 
-    fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
-        if let hir::PatKind::Binding(_, hir_id, ..) = param.pat.kind {
+    fn visit_pat(&mut self, pat: &'tcx hir::Pat<'tcx>) {
+        if let hir::PatKind::Binding(_, hir_id, ..) = pat.kind {
             self.binding_hir_ids.push(hir_id);
         }
+        intravisit::walk_pat(self, pat);
     }
 
     fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
index 2e923bc97a2ef9036bc6309f24a1fe85437e70e2..b05dd143bfd745f6901e2fd34109bdae5fdd611c 100644 (file)
@@ -15,4 +15,8 @@ mod e {
     }
     let _ = Ok(1).unwrap_or_else(|e::E| 2);
     let _ = Ok(1).unwrap_or_else(|SomeStruct { .. }| 2);
+
+    // Fix #6343
+    let arr = [(Some(1),)];
+    Some(&0).and_then(|&i| arr[i].0);
 }