Skip to content

in which the must-use additional messaging is tucked into a note #50437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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