Skip to content

Commit d7dbefd

Browse files
committed
misc: reduce nesting
1 parent 050bc35 commit d7dbefd

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

0 commit comments

Comments
 (0)