Skip to content

Commit 248745a

Browse files
committed
Auto merge of #55569 - durka:must-use-external-macro, r=alexcrichton
enforce unused-must-use lint in macros Fixes #55516 by turning on the UNUSED_MUST_USE lint within macros.
2 parents e6c5cf9 + 706a1cc commit 248745a

File tree

6 files changed

+44
-16
lines changed

6 files changed

+44
-16
lines changed

Diff for: src/librustc_lint/unused.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use rustc::hir;
2929
declare_lint! {
3030
pub UNUSED_MUST_USE,
3131
Warn,
32-
"unused result of a type flagged as #[must_use]"
32+
"unused result of a type flagged as #[must_use]",
33+
report_in_external_macro: true
3334
}
3435

3536
declare_lint! {

Diff for: src/librustc_resolve/resolve_imports.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use syntax_pos::{MultiSpan, Span};
3636

3737
use std::cell::{Cell, RefCell};
3838
use std::collections::BTreeMap;
39-
use std::fmt::Write;
4039
use std::{mem, ptr};
4140

4241
/// Contains data for specific types of import directives.
@@ -780,17 +779,14 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
780779

781780
let msg = format!("`{}` import is ambiguous", name);
782781
let mut err = self.session.struct_span_err(span, &msg);
783-
let mut suggestion_choices = String::new();
782+
let mut suggestion_choices = vec![];
784783
if external_crate.is_some() {
785-
write!(suggestion_choices, "`::{}`", name);
784+
suggestion_choices.push(format!("`::{}`", name));
786785
err.span_label(span,
787786
format!("can refer to external crate `::{}`", name));
788787
}
789788
if let Some(result) = results.module_scope {
790-
if !suggestion_choices.is_empty() {
791-
suggestion_choices.push_str(" or ");
792-
}
793-
write!(suggestion_choices, "`self::{}`", name);
789+
suggestion_choices.push(format!("`self::{}`", name));
794790
if uniform_paths_feature {
795791
err.span_label(result.span,
796792
format!("can refer to `self::{}`", name));
@@ -803,7 +799,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
803799
err.span_label(result.span,
804800
format!("shadowed by block-scoped `{}`", name));
805801
}
806-
err.help(&format!("write {} explicitly instead", suggestion_choices));
802+
err.help(&format!("write {} explicitly instead", suggestion_choices.join(" or ")));
807803
if uniform_paths_feature {
808804
err.note("relative `use` paths enabled by `#![feature(uniform_paths)]`");
809805
} else {

Diff for: src/test/run-pass/impl-trait/example-calendar.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ trait IteratorExt: Iterator + Sized {
310310
where Self::Item: std::fmt::Display {
311311
let mut s = String::new();
312312
if let Some(e) = self.next() {
313-
write!(s, "{}", e);
313+
write!(s, "{}", e).unwrap();
314314
for e in self {
315315
s.push_str(sep);
316-
write!(s, "{}", e);
316+
write!(s, "{}", e).unwrap();
317317
}
318318
}
319319
s
@@ -537,7 +537,7 @@ fn format_weeks(it: impl Iterator<Item = impl DateIterator>) -> impl Iterator<It
537537
first = false;
538538
}
539539

540-
write!(buf, " {:>2}", d.day());
540+
write!(buf, " {:>2}", d.day()).unwrap();
541541
}
542542

543543
// Insert more filler at the end to fill up the remainder of the week,

Diff for: src/test/run-pass/macros/colorful-write-macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ impl fmt::Write for Bar {
2727
}
2828

2929
fn borrowing_writer_from_struct_and_formatting_struct_field(foo: Foo) {
30-
write!(foo.writer, "{}", foo.other);
30+
write!(foo.writer, "{}", foo.other).unwrap();
3131
}
3232

3333
fn main() {
3434
let mut w = Vec::new();
35-
write!(&mut w as &mut Write, "");
36-
write!(&mut w, ""); // should coerce
35+
write!(&mut w as &mut Write, "").unwrap();
36+
write!(&mut w, "").unwrap(); // should coerce
3737
println!("ok");
3838

3939
let mut s = Bar;
4040
{
4141
use std::fmt::Write;
42-
write!(&mut s, "test");
42+
write!(&mut s, "test").unwrap();
4343
}
4444
}

Diff for: src/test/ui/macros/must-use-in-macro-55516.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-pass
12+
// compile-flags: -Wunused
13+
14+
// make sure write!() can't hide its unused Result
15+
16+
fn main() {
17+
use std::fmt::Write;
18+
let mut example = String::new();
19+
write!(&mut example, "{}", 42); //~WARN must be used
20+
}
21+

Diff for: src/test/ui/macros/must-use-in-macro-55516.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: unused `std::result::Result` that must be used
2+
--> $DIR/must-use-in-macro-55516.rs:19:5
3+
|
4+
LL | write!(&mut example, "{}", 42); //~WARN must be used
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-W unused-must-use` implied by `-W unused`
8+
= note: this `Result` may be an `Err` variant, which should be handled
9+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
10+

0 commit comments

Comments
 (0)