From 05540bf08b067a6894937b3601cc2c66804615a6 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Tue, 20 Jun 2017 04:36:56 +0900 Subject: [PATCH] Show type name for unused_must_use lint --- src/librustc_lint/unused.rs | 13 ++++++------- src/test/compile-fail/unused-result.rs | 8 ++++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 0c82679c307d3..473c0f3ffda86 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use rustc::hir::def_id::DefId; use rustc::ty; use rustc::ty::adjustment; use util::nodemap::FxHashMap; @@ -144,20 +145,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults { ty::TyTuple(ref tys, _) if tys.is_empty() => return, ty::TyNever => return, ty::TyBool => return, - ty::TyAdt(def, _) => { - let attrs = cx.tcx.get_attrs(def.did); - check_must_use(cx, &attrs, s.span) - } + ty::TyAdt(def, _) => check_must_use(cx, def.did, s.span), _ => false, }; if !warned { cx.span_lint(UNUSED_RESULTS, s.span, "unused result"); } - fn check_must_use(cx: &LateContext, attrs: &[ast::Attribute], sp: Span) -> bool { - for attr in attrs { + fn check_must_use(cx: &LateContext, def_id: DefId, sp: Span) -> bool { + for attr in cx.tcx.get_attrs(def_id).iter() { if attr.check_name("must_use") { - let mut msg = "unused result which must be used".to_string(); + let mut msg = format!("unused `{}` which must be used", + cx.tcx.item_path_str(def_id)); // check for #[must_use="..."] if let Some(s) = attr.value_str() { msg.push_str(": "); diff --git a/src/test/compile-fail/unused-result.rs b/src/test/compile-fail/unused-result.rs index 6ed3b081c97fd..0c6c7fc5a0d75 100644 --- a/src/test/compile-fail/unused-result.rs +++ b/src/test/compile-fail/unused-result.rs @@ -26,8 +26,8 @@ fn qux() -> MustUseMsg { return foo::(); } #[allow(unused_results)] fn test() { foo::(); - foo::(); //~ ERROR: unused result which must be used - foo::(); //~ ERROR: unused result which must be used: some message + foo::(); //~ ERROR: unused `MustUse` which must be used + foo::(); //~ ERROR: unused `MustUseMsg` which must be used: some message } #[allow(unused_results, unused_must_use)] @@ -39,8 +39,8 @@ fn test2() { fn main() { foo::(); //~ ERROR: unused result - foo::(); //~ ERROR: unused result which must be used - foo::(); //~ ERROR: unused result which must be used: some message + foo::(); //~ ERROR: unused `MustUse` which must be used + foo::(); //~ ERROR: unused `MustUseMsg` which must be used: some message let _ = foo::(); let _ = foo::();