1
Fork 0

Use general uninhabitedness checking

This commit is contained in:
varkor 2018-11-03 19:23:05 +00:00
parent 9e8a982a23
commit b55717f9b0

View file

@ -60,19 +60,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
} }
let t = cx.tables.expr_ty(&expr); let t = cx.tables.expr_ty(&expr);
// FIXME(varkor): replace with `t.is_unit() || t.conservative_is_uninhabited()`. let type_permits_lack_of_use = if t.is_unit()
let type_permits_no_use = match t.sty { || cx.tcx.is_ty_uninhabited_from(cx.tcx.hir.get_module_parent(expr.id), t) {
ty::Tuple(ref tys) if tys.is_empty() => true, true
ty::Never => true, } else {
ty::Adt(def, _) => { match t.sty {
if def.variants.is_empty() { ty::Adt(def, _) => check_must_use(cx, def.did, s.span, ""),
true _ => false,
} else {
check_must_use(cx, def.did, s.span, "")
}
} }
_ => false, }
};
let mut fn_warned = false; let mut fn_warned = false;
let mut op_warned = false; let mut op_warned = false;
@ -99,7 +95,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
if let Some(def) = maybe_def { if let Some(def) = maybe_def {
let def_id = def.def_id(); let def_id = def.def_id();
fn_warned = check_must_use(cx, def_id, s.span, "return value of "); fn_warned = check_must_use(cx, def_id, s.span, "return value of ");
} else if type_permits_no_use { } else if type_permits_lack_of_use {
// We don't warn about unused unit or uninhabited types. // We don't warn about unused unit or uninhabited types.
// (See https://github.com/rust-lang/rust/issues/43806 for details.) // (See https://github.com/rust-lang/rust/issues/43806 for details.)
return; return;
@ -148,7 +144,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
op_warned = true; op_warned = true;
} }
if !(type_permits_no_use || fn_warned || op_warned) { if !(type_permits_lack_of_use || fn_warned || op_warned) {
cx.span_lint(UNUSED_RESULTS, s.span, "unused result"); cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
} }