From 9dd2c890662c83428ab17262281c51c6eb84e315 Mon Sep 17 00:00:00 2001 From: Alex Burka Date: Thu, 1 Nov 2018 03:32:45 +0000 Subject: [PATCH 1/3] enforce unused-must-use lint in macros --- src/librustc_lint/unused.rs | 3 ++- src/test/ui/macros/must-use-in-macro-55516.rs | 21 +++++++++++++++++++ .../ui/macros/must-use-in-macro-55516.stderr | 10 +++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/macros/must-use-in-macro-55516.rs create mode 100644 src/test/ui/macros/must-use-in-macro-55516.stderr diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 5950e19b0ee1d..6d365e6d1ecbf 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -29,7 +29,8 @@ use rustc::hir; declare_lint! { pub UNUSED_MUST_USE, Warn, - "unused result of a type flagged as #[must_use]" + "unused result of a type flagged as #[must_use]", + report_in_external_macro: true } declare_lint! { diff --git a/src/test/ui/macros/must-use-in-macro-55516.rs b/src/test/ui/macros/must-use-in-macro-55516.rs new file mode 100644 index 0000000000000..ad7cc37da52ca --- /dev/null +++ b/src/test/ui/macros/must-use-in-macro-55516.rs @@ -0,0 +1,21 @@ +// Copyright 2018 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. + +// compile-pass +// compile-flags: -Wunused + +// make sure write!() can't hide its unused Result + +fn main() { + use std::fmt::Write; + let mut example = String::new(); + write!(&mut example, "{}", 42); //~WARN must be used +} + diff --git a/src/test/ui/macros/must-use-in-macro-55516.stderr b/src/test/ui/macros/must-use-in-macro-55516.stderr new file mode 100644 index 0000000000000..b03a5806da5c3 --- /dev/null +++ b/src/test/ui/macros/must-use-in-macro-55516.stderr @@ -0,0 +1,10 @@ +warning: unused `std::result::Result` that must be used + --> $DIR/must-use-in-macro-55516.rs:19:5 + | +LL | write!(&mut example, "{}", 42); //~WARN must be used + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-W unused-must-use` implied by `-W unused` + = note: this `Result` may be an `Err` variant, which should be handled + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + From d5e3e8a89f7279a2240b691752d0f850feab8929 Mon Sep 17 00:00:00 2001 From: Alex Burka Date: Fri, 2 Nov 2018 03:26:31 +0000 Subject: [PATCH 2/3] remove unused result in resolve --- src/librustc_resolve/resolve_imports.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 359640ccda2b1..a3694cd73ad53 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -36,7 +36,6 @@ use syntax_pos::{MultiSpan, Span}; use std::cell::{Cell, RefCell}; use std::collections::BTreeMap; -use std::fmt::Write; use std::{mem, ptr}; /// Contains data for specific types of import directives. @@ -780,17 +779,14 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> { let msg = format!("`{}` import is ambiguous", name); let mut err = self.session.struct_span_err(span, &msg); - let mut suggestion_choices = String::new(); + let mut suggestion_choices = vec![]; if external_crate.is_some() { - write!(suggestion_choices, "`::{}`", name); + suggestion_choices.push(format!("`::{}`", name)); err.span_label(span, format!("can refer to external crate `::{}`", name)); } if let Some(result) = results.module_scope { - if !suggestion_choices.is_empty() { - suggestion_choices.push_str(" or "); - } - write!(suggestion_choices, "`self::{}`", name); + suggestion_choices.push(format!("`self::{}`", name)); if uniform_paths_feature { err.span_label(result.span, format!("can refer to `self::{}`", name)); @@ -803,7 +799,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> { err.span_label(result.span, format!("shadowed by block-scoped `{}`", name)); } - err.help(&format!("write {} explicitly instead", suggestion_choices)); + err.help(&format!("write {} explicitly instead", suggestion_choices.join(" or "))); if uniform_paths_feature { err.note("relative `use` paths enabled by `#![feature(uniform_paths)]`"); } else { From 706a1cc0f21bb3212e1e1f9987f5be543ae12f6e Mon Sep 17 00:00:00 2001 From: Alex Burka Date: Sat, 3 Nov 2018 05:03:30 +0000 Subject: [PATCH 3/3] fix test fallout --- src/test/run-pass/impl-trait/example-calendar.rs | 6 +++--- src/test/run-pass/macros/colorful-write-macros.rs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/run-pass/impl-trait/example-calendar.rs b/src/test/run-pass/impl-trait/example-calendar.rs index 6cf06d1562104..e6dd421f48f51 100644 --- a/src/test/run-pass/impl-trait/example-calendar.rs +++ b/src/test/run-pass/impl-trait/example-calendar.rs @@ -310,10 +310,10 @@ trait IteratorExt: Iterator + Sized { where Self::Item: std::fmt::Display { let mut s = String::new(); if let Some(e) = self.next() { - write!(s, "{}", e); + write!(s, "{}", e).unwrap(); for e in self { s.push_str(sep); - write!(s, "{}", e); + write!(s, "{}", e).unwrap(); } } s @@ -537,7 +537,7 @@ fn format_weeks(it: impl Iterator) -> impl Iterator2}", d.day()); + write!(buf, " {:>2}", d.day()).unwrap(); } // Insert more filler at the end to fill up the remainder of the week, diff --git a/src/test/run-pass/macros/colorful-write-macros.rs b/src/test/run-pass/macros/colorful-write-macros.rs index 7c557eb2bd079..ee597f11c6a9d 100644 --- a/src/test/run-pass/macros/colorful-write-macros.rs +++ b/src/test/run-pass/macros/colorful-write-macros.rs @@ -27,18 +27,18 @@ impl fmt::Write for Bar { } fn borrowing_writer_from_struct_and_formatting_struct_field(foo: Foo) { - write!(foo.writer, "{}", foo.other); + write!(foo.writer, "{}", foo.other).unwrap(); } fn main() { let mut w = Vec::new(); - write!(&mut w as &mut Write, ""); - write!(&mut w, ""); // should coerce + write!(&mut w as &mut Write, "").unwrap(); + write!(&mut w, "").unwrap(); // should coerce println!("ok"); let mut s = Bar; { use std::fmt::Write; - write!(&mut s, "test"); + write!(&mut s, "test").unwrap(); } }