Skip to content

Commit aaed9d9

Browse files
committed
Auto merge of #6370 - giraffate:fix_fp_in_unnecessary_lazy_evaluations, r=llogiq,flip1995
Fix FP in `unnecessary_lazy_evaluations` Fix #6343 changelog: Fix FP in `unnecessary_lazy_evaluations`
2 parents c1664c5 + ba12494 commit aaed9d9

11 files changed

+44
-20
lines changed

clippy_lints/src/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, span: Span) {
480480
| ItemKind::ForeignMod(..) => return false,
481481
// We found a main function ...
482482
ItemKind::Fn(_, sig, _, Some(block)) if item.ident.name == sym::main => {
483-
let is_async = matches!(sig.header.asyncness, Async::Yes{..});
483+
let is_async = matches!(sig.header.asyncness, Async::Yes { .. });
484484
let returns_nothing = match &sig.decl.output {
485485
FnRetTy::Default(..) => true,
486486
FnRetTy::Ty(ty) if ty.kind.is_unit() => true,

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
9999
let has_const_generic_params = generics
100100
.params
101101
.iter()
102-
.any(|param| matches!(param.kind, GenericParamKind::Const{ .. }));
102+
.any(|param| matches!(param.kind, GenericParamKind::Const { .. }));
103103

104104
if already_const(header) || has_const_generic_params {
105105
return;

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
9090

9191
// Exclude non-inherent impls
9292
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
93-
if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. } |
94-
ItemKind::Trait(..))
95-
{
93+
if matches!(
94+
item.kind,
95+
ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
96+
) {
9697
return;
9798
}
9899
}

clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
244244

245245
// Exclude non-inherent impls
246246
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
247-
if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. } |
248-
ItemKind::Trait(..))
249-
{
247+
if matches!(
248+
item.kind,
249+
ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
250+
) {
250251
return;
251252
}
252253
}

clippy_lints/src/redundant_clone.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ impl<'tcx> mir::visit::Visitor<'tcx> for LocalUseVisitor {
390390
let local = place.local;
391391

392392
if local == self.used.0
393-
&& !matches!(ctx, PlaceContext::MutatingUse(MutatingUseContext::Drop) | PlaceContext::NonUse(_))
393+
&& !matches!(
394+
ctx,
395+
PlaceContext::MutatingUse(MutatingUseContext::Drop) | PlaceContext::NonUse(_)
396+
)
394397
{
395398
self.used.1 = true;
396399
}

clippy_lints/src/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,9 @@ fn is_empty_block(expr: &Expr<'_>) -> bool {
11041104
expr.kind,
11051105
ExprKind::Block(
11061106
Block {
1107-
stmts: &[], expr: None, ..
1107+
stmts: &[],
1108+
expr: None,
1109+
..
11081110
},
11091111
_,
11101112
)

clippy_lints/src/unnecessary_wraps.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
7474
}
7575

7676
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
77-
if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), ..} | ItemKind::Trait(..)) {
77+
if matches!(
78+
item.kind,
79+
ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
80+
) {
7881
return;
7982
}
8083
}

clippy_lints/src/utils/ast_utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,10 @@ pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
408408
}
409409

410410
pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
411-
matches!((l, r), (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_)))
411+
matches!(
412+
(l, r),
413+
(Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
414+
)
412415
}
413416

414417
pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ pub fn is_no_std_crate(krate: &Crate<'_>) -> bool {
15001500
/// ```
15011501
pub fn is_trait_impl_item(cx: &LateContext<'_>, hir_id: HirId) -> bool {
15021502
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
1503-
matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. })
1503+
matches!(item.kind, ItemKind::Impl { of_trait: Some(_), .. })
15041504
} else {
15051505
false
15061506
}

clippy_lints/src/utils/usage.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,27 @@ pub struct ParamBindingIdCollector {
116116
}
117117
impl<'tcx> ParamBindingIdCollector {
118118
fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
119-
let mut finder = ParamBindingIdCollector {
120-
binding_hir_ids: Vec::new(),
121-
};
122-
finder.visit_body(body);
123-
finder.binding_hir_ids
119+
let mut hir_ids: Vec<hir::HirId> = Vec::new();
120+
for param in body.params.iter() {
121+
let mut finder = ParamBindingIdCollector {
122+
binding_hir_ids: Vec::new(),
123+
};
124+
finder.visit_param(param);
125+
for hir_id in &finder.binding_hir_ids {
126+
hir_ids.push(*hir_id);
127+
}
128+
}
129+
hir_ids
124130
}
125131
}
126132
impl<'tcx> intravisit::Visitor<'tcx> for ParamBindingIdCollector {
127133
type Map = Map<'tcx>;
128134

129-
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
130-
if let hir::PatKind::Binding(_, hir_id, ..) = param.pat.kind {
135+
fn visit_pat(&mut self, pat: &'tcx hir::Pat<'tcx>) {
136+
if let hir::PatKind::Binding(_, hir_id, ..) = pat.kind {
131137
self.binding_hir_ids.push(hir_id);
132138
}
139+
intravisit::walk_pat(self, pat);
133140
}
134141

135142
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {

tests/ui/unnecessary_lazy_eval_unfixable.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ fn main() {
1515
}
1616
let _ = Ok(1).unwrap_or_else(|e::E| 2);
1717
let _ = Ok(1).unwrap_or_else(|SomeStruct { .. }| 2);
18+
19+
// Fix #6343
20+
let arr = [(Some(1),)];
21+
Some(&0).and_then(|&i| arr[i].0);
1822
}

0 commit comments

Comments
 (0)