From 0b2c9f03efc248680a8e7eeedb6e5f8da5947774 Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Fri, 11 Aug 2017 13:43:33 -0700 Subject: [PATCH 1/2] Fix unused_result lint triggering when a function returns `()` or `!` Add a test for this case --- src/librustc_lint/unused.rs | 2 ++ src/test/ui/issue-43806.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/test/ui/issue-43806.rs diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index ba17df4cdca4b..e5b818cd5fb5a 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -146,6 +146,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults { let t = cx.tables.expr_ty(&expr); let ty_warned = match t.sty { + ty::TyTuple(ref tys, _) if tys.is_empty() => return, + ty::TyNever => return, ty::TyAdt(def, _) => check_must_use(cx, def.did, s.span, ""), _ => false, }; diff --git a/src/test/ui/issue-43806.rs b/src/test/ui/issue-43806.rs new file mode 100644 index 0000000000000..df7f756127c4d --- /dev/null +++ b/src/test/ui/issue-43806.rs @@ -0,0 +1,26 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// run-pass + +#![deny(unused_results)] + +fn foo() {} + +fn bar() -> ! { + loop {} +} + +fn qux() { + foo(); + bar(); +} + +fn main() {} From eeb748aa12a3bb7f6bbd1205385fa0a0adbef90c Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Fri, 11 Aug 2017 21:52:16 -0700 Subject: [PATCH 2/2] Don't trigger unused_result on functions returning empty enums --- src/librustc_lint/unused.rs | 8 +++++++- src/test/ui/issue-43806.rs | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index e5b818cd5fb5a..7773891b5d066 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -148,7 +148,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults { let ty_warned = match t.sty { ty::TyTuple(ref tys, _) if tys.is_empty() => return, ty::TyNever => return, - ty::TyAdt(def, _) => check_must_use(cx, def.did, s.span, ""), + ty::TyAdt(def, _) => { + if def.variants.is_empty() { + return; + } else { + check_must_use(cx, def.did, s.span, "") + } + }, _ => false, }; diff --git a/src/test/ui/issue-43806.rs b/src/test/ui/issue-43806.rs index df7f756127c4d..7757a503c7e87 100644 --- a/src/test/ui/issue-43806.rs +++ b/src/test/ui/issue-43806.rs @@ -12,15 +12,22 @@ #![deny(unused_results)] +enum Void {} + fn foo() {} fn bar() -> ! { loop {} } +fn baz() -> Void { + loop {} +} + fn qux() { foo(); bar(); + baz(); } fn main() {}