Skip to content

Commit 9e1cd08

Browse files
committed
misc: reduce nesting
1 parent e25c5e7 commit 9e1cd08

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -375,65 +375,50 @@ fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(Ve
375375
};
376376
Some((vec![start, end], applicability))
377377
},
378-
ExprKind::Struct(_, fields, ref base) => {
378+
ExprKind::Struct(_, fields, ref base) if !has_drop(cx, cx.typeck_results().expr_ty(expr)) => {
379379
let applicability = if fields.iter().all(|f| expr_type_is_certain(cx, f.expr)) {
380380
Applicability::MachineApplicable
381381
} else {
382382
// there's a risk that, if we take the field exprs out of the context of the struct constructor,
383383
// their types might become ambiguous
384384
Applicability::MaybeIncorrect
385385
};
386-
if has_drop(cx, cx.typeck_results().expr_ty(expr)) {
387-
None
388-
} else {
389-
let base = match base {
390-
StructTailExpr::Base(base) => Some(base),
391-
StructTailExpr::None | StructTailExpr::DefaultFields(_) => None,
392-
};
393-
Some((
394-
fields.iter().map(|f| &f.expr).chain(base).map(Deref::deref).collect(),
395-
applicability,
396-
))
397-
}
386+
let base = match base {
387+
StructTailExpr::Base(base) => Some(base),
388+
StructTailExpr::None | StructTailExpr::DefaultFields(_) => None,
389+
};
390+
Some((
391+
fields.iter().map(|f| &f.expr).chain(base).map(Deref::deref).collect(),
392+
applicability,
393+
))
398394
},
399-
ExprKind::Call(callee, args) => {
395+
ExprKind::Call(callee, args)
396+
if let ExprKind::Path(ref qpath) = callee.kind
397+
// not a type-dependent function call like `impl FnOnce for X`
398+
&& cx.typeck_results().type_dependent_def(expr.hir_id).is_none()
399+
&& let Res::Def(def_kind, ..) = cx.qpath_res(qpath, callee.hir_id)
400+
&& matches!(def_kind, DefKind::Struct | DefKind::Variant | DefKind::Ctor(..))
401+
&& !has_drop(cx, cx.typeck_results().expr_ty(expr)) =>
402+
{
400403
let applicability = if args.iter().all(|a| expr_type_is_certain(cx, a)) {
401404
Applicability::MachineApplicable
402405
} else {
403406
// there's a risk that, if we take the args out of the context of the
404407
// call/constructor, their types might become ambiguous
405408
Applicability::MaybeIncorrect
406409
};
407-
if let ExprKind::Path(ref qpath) = callee.kind {
408-
if cx.typeck_results().type_dependent_def(expr.hir_id).is_some() {
409-
// type-dependent function call like `impl FnOnce for X`
410-
return None;
411-
}
412-
let res = cx.qpath_res(qpath, callee.hir_id);
413-
match res {
414-
Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)
415-
if !has_drop(cx, cx.typeck_results().expr_ty(expr)) =>
416-
{
417-
Some((args.iter().collect(), applicability))
418-
},
419-
_ => None,
420-
}
421-
} else {
422-
None
423-
}
410+
Some((args.iter().collect(), applicability))
424411
},
425-
ExprKind::Block(block, _) => {
426-
if block.stmts.is_empty() && !block.targeted_by_break {
427-
block.expr.as_ref().and_then(|e| {
428-
match block.rules {
429-
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
430-
BlockCheckMode::DefaultBlock => Some((vec![&**e], Applicability::MachineApplicable)),
431-
// in case of compiler-inserted signaling blocks
432-
BlockCheckMode::UnsafeBlock(_) => reduce_expression(cx, e),
433-
}
434-
})
435-
} else {
436-
None
412+
ExprKind::Block(block, _)
413+
if !block.targeted_by_break
414+
&& block.stmts.is_empty()
415+
&& let Some(e) = &block.expr =>
416+
{
417+
match block.rules {
418+
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
419+
BlockCheckMode::DefaultBlock => Some((vec![&**e], Applicability::MachineApplicable)),
420+
// in case of compiler-inserted signaling blocks
421+
BlockCheckMode::UnsafeBlock(_) => reduce_expression(cx, e),
437422
}
438423
},
439424
_ => None,

0 commit comments

Comments
 (0)