Skip to content

Commit

Permalink
in which the must-use additional messaging is tucked into a note
Browse files Browse the repository at this point in the history
Also, a comment is edited to reflect that spaces around the equals-sign in
attributes is the standard (q.v. rust-lang/style-team@bea80532e7).
  • Loading branch information
zackmdavis committed May 7, 2018
1 parent 700165d commit 5a5a25c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
if attr.check_name("must_use") {
let mut msg = format!("unused {}`{}` which must be used",
describe_path, cx.tcx.item_path_str(def_id));
// check for #[must_use="..."]
if let Some(s) = attr.value_str() {
msg.push_str(": ");
msg.push_str(&s.as_str());
let mut err = cx.struct_span_lint(UNUSED_MUST_USE, sp, &msg);
// check for #[must_use = "..."]
if let Some(note) = attr.value_str() {
err.note(&note.as_str());
}
cx.span_lint(UNUSED_MUST_USE, sp, &msg);
err.emit();
return true;
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/test/compile-fail/unused-result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![deny(unused_results, unused_must_use)]
#![allow(dead_code)]
#![deny(unused_results, unused_must_use)]
//~^ NOTE: lint level defined here
//~| NOTE: lint level defined here

#[must_use]
enum MustUse { Test }
Expand All @@ -27,7 +29,8 @@ fn qux() -> MustUseMsg { return foo::<MustUseMsg>(); }
fn test() {
foo::<isize>();
foo::<MustUse>(); //~ ERROR: unused `MustUse` which must be used
foo::<MustUseMsg>(); //~ ERROR: unused `MustUseMsg` which must be used: some message
foo::<MustUseMsg>(); //~ ERROR: unused `MustUseMsg` which must be used
//~^ NOTE: some message
}

#[allow(unused_results, unused_must_use)]
Expand All @@ -40,7 +43,8 @@ fn test2() {
fn main() {
foo::<isize>(); //~ ERROR: unused result
foo::<MustUse>(); //~ ERROR: unused `MustUse` which must be used
foo::<MustUseMsg>(); //~ ERROR: unused `MustUseMsg` which must be used: some message
foo::<MustUseMsg>(); //~ ERROR: unused `MustUseMsg` which must be used
//~^ NOTE: some message

let _ = foo::<isize>();
let _ = foo::<MustUse>();
Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/fn_must_use.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: unused return value of `need_to_use_this_value` which must be used: it's important
warning: unused return value of `need_to_use_this_value` which must be used
--> $DIR/fn_must_use.rs:60:5
|
LL | need_to_use_this_value(); //~ WARN unused return value
Expand All @@ -9,18 +9,21 @@ note: lint level defined here
|
LL | #![warn(unused_must_use)]
| ^^^^^^^^^^^^^^^
= note: it's important

warning: unused return value of `MyStruct::need_to_use_this_method_value` which must be used
--> $DIR/fn_must_use.rs:65:5
|
LL | m.need_to_use_this_method_value(); //~ WARN unused return value
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused return value of `EvenNature::is_even` which must be used: no side effects
warning: unused return value of `EvenNature::is_even` which must be used
--> $DIR/fn_must_use.rs:66:5
|
LL | m.is_even(); // trait method!
| ^^^^^^^^^^^^
|
= note: no side effects

warning: unused return value of `std::cmp::PartialEq::eq` which must be used
--> $DIR/fn_must_use.rs:72:5
Expand Down

0 comments on commit 5a5a25c

Please sign in to comment.