Skip to content

Commit ae1e2c6

Browse files
committed
must_use: drive-by-cleanup
1 parent b2cc4d5 commit ae1e2c6

1 file changed

Lines changed: 43 additions & 47 deletions

File tree

compiler/rustc_lint/src/unused/must_use.rs

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,13 @@ pub fn is_ty_must_use<'tcx>(
146146
if ty.is_unit() {
147147
return IsTyMustUse::Trivial;
148148
}
149+
149150
let parent_mod_did = cx.tcx.parent_module(expr.hir_id).to_def_id();
150151
let is_uninhabited =
151152
|t: Ty<'tcx>| !t.is_inhabited_from(cx.tcx, parent_mod_did, cx.typing_env());
152-
if is_uninhabited(ty) {
153-
return IsTyMustUse::Trivial;
154-
}
155153

156154
match *ty.kind() {
155+
_ if is_uninhabited(ty) => IsTyMustUse::Trivial,
157156
ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => {
158157
is_ty_must_use(cx, boxed, expr, span).map(|inner| MustUsePath::Boxed(Box::new(inner)))
159158
}
@@ -212,6 +211,7 @@ pub fn is_ty_must_use<'tcx>(
212211
}
213212
})
214213
.map_or(IsTyMustUse::No, IsTyMustUse::Yes),
214+
// NB: unit is checked up above; this is only reachable for tuples with at least one element
215215
ty::Tuple(tys) => {
216216
let elem_exprs = if let hir::ExprKind::Tup(elem_exprs) = expr.kind {
217217
debug_assert_eq!(elem_exprs.len(), tys.len());
@@ -364,28 +364,29 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
364364
_ => None,
365365
};
366366

367-
let mut op_warned = false;
368-
369-
if let Some(must_use_op) = must_use_op {
370-
let span = expr.span.find_ancestor_not_from_macro().unwrap_or(expr.span);
371-
cx.emit_span_lint(
372-
UNUSED_MUST_USE,
373-
expr.span,
374-
UnusedOp {
375-
op: must_use_op,
376-
label: expr.span,
377-
suggestion: if expr_is_from_block {
378-
UnusedOpSuggestion::BlockTailExpr {
379-
before_span: span.shrink_to_lo(),
380-
after_span: span.shrink_to_hi(),
381-
}
382-
} else {
383-
UnusedOpSuggestion::NormalExpr { span: span.shrink_to_lo() }
367+
let op_warned = match must_use_op {
368+
Some(must_use_op) => {
369+
let span = expr.span.find_ancestor_not_from_macro().unwrap_or(expr.span);
370+
cx.emit_span_lint(
371+
UNUSED_MUST_USE,
372+
expr.span,
373+
UnusedOp {
374+
op: must_use_op,
375+
label: expr.span,
376+
suggestion: if expr_is_from_block {
377+
UnusedOpSuggestion::BlockTailExpr {
378+
before_span: span.shrink_to_lo(),
379+
after_span: span.shrink_to_hi(),
380+
}
381+
} else {
382+
UnusedOpSuggestion::NormalExpr { span: span.shrink_to_lo() }
383+
},
384384
},
385-
},
386-
);
387-
op_warned = true;
388-
}
385+
);
386+
true
387+
}
388+
None => false,
389+
};
389390

390391
// Only emit unused results lint if we haven't emitted any of the more specific lints and the expression type is non trivial.
391392
if !(type_lint_emitted_or_trivial || fn_warned || op_warned) {
@@ -399,38 +400,33 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
399400
fn check_fn_must_use(cx: &LateContext<'_>, expr: &hir::Expr<'_>, expr_is_from_block: bool) -> bool {
400401
let maybe_def_id = match expr.kind {
401402
hir::ExprKind::Call(callee, _) => {
402-
match callee.kind {
403-
hir::ExprKind::Path(ref qpath) => {
404-
match cx.qpath_res(qpath, callee.hir_id) {
405-
Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) => Some(def_id),
406-
// `Res::Local` if it was a closure, for which we
407-
// do not currently support must-use linting
408-
_ => None,
409-
}
410-
}
411-
_ => None,
403+
if let hir::ExprKind::Path(ref qpath) = callee.kind
404+
// `Res::Local` if it was a closure, for which we
405+
// do not currently support must-use linting
406+
&& let Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) =
407+
cx.qpath_res(qpath, callee.hir_id)
408+
{
409+
Some(def_id)
410+
} else {
411+
None
412412
}
413413
}
414414
hir::ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id),
415415
_ => None,
416416
};
417-
if let Some(def_id) = maybe_def_id {
418-
check_must_use_def(cx, def_id, expr.span, "return value of ", "", expr_is_from_block)
419-
} else {
420-
false
417+
418+
match maybe_def_id {
419+
Some(def_id) => {
420+
check_must_use_def(cx, def_id, expr.span, "return value of ", "", expr_is_from_block)
421+
}
422+
None => false,
421423
}
422424
}
423425

424426
fn is_def_must_use(cx: &LateContext<'_>, def_id: DefId, span: Span) -> Option<MustUsePath> {
425-
if let Some(reason) = find_attr!(
426-
cx.tcx, def_id,
427-
MustUse { reason, .. } => reason
428-
) {
429-
// check for #[must_use = "..."]
430-
Some(MustUsePath::Def(span, def_id, *reason))
431-
} else {
432-
None
433-
}
427+
// check for #[must_use = "..."]
428+
find_attr!(cx.tcx, def_id, MustUse { reason, .. } => reason)
429+
.map(|reason| MustUsePath::Def(span, def_id, *reason))
434430
}
435431

436432
/// Returns whether further errors should be suppressed because a lint has been emitted.

0 commit comments

Comments
 (0)