Skip to content

Add a hint that the expressions produce a value #87607

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 3 commits into from
Jul 31, 2021
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: 9 additions & 1 deletion compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,15 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {

if let Some(must_use_op) = must_use_op {
cx.struct_span_lint(UNUSED_MUST_USE, expr.span, |lint| {
lint.build(&format!("unused {} that must be used", must_use_op)).emit()
let mut lint = lint.build(&format!("unused {} that must be used", must_use_op));
lint.span_label(expr.span, &format!("the {} produces a value", must_use_op));
lint.span_suggestion_verbose(
expr.span.shrink_to_lo(),
"use `let _ = ...` to ignore the resulting value",
"let _ = ".to_string(),
Applicability::MachineApplicable,
);
lint.emit();
});
op_warned = true;
}
Expand Down
14 changes: 12 additions & 2 deletions src/test/ui/lint/fn_must_use.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ warning: unused comparison that must be used
--> $DIR/fn_must_use.rs:74:5
|
LL | 2 == 3;
| ^^^^^^
| ^^^^^^ the comparison produces a value
|
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = 2 == 3;
| ^^^^^^^

warning: unused comparison that must be used
--> $DIR/fn_must_use.rs:75:5
|
LL | m == n;
| ^^^^^^
| ^^^^^^ the comparison produces a value
|
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = m == n;
| ^^^^^^^

warning: 8 warnings emitted

134 changes: 0 additions & 134 deletions src/test/ui/lint/must-use-ops.stderr

This file was deleted.

41 changes: 35 additions & 6 deletions src/test/ui/lint/unused-borrows.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,72 @@ error: unused borrow that must be used
--> $DIR/unused-borrows.rs:6:5
|
LL | &42;
| ^^^
| ^^^ the borrow produces a value
|
note: the lint level is defined here
--> $DIR/unused-borrows.rs:1:9
|
LL | #![deny(unused_must_use)]
| ^^^^^^^^^^^^^^^
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = &42;
| ^^^^^^^

error: unused borrow that must be used
--> $DIR/unused-borrows.rs:9:5
|
LL | &mut foo(42);
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^ the borrow produces a value
|
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = &mut foo(42);
| ^^^^^^^

error: unused borrow that must be used
--> $DIR/unused-borrows.rs:12:5
|
LL | &&42;
| ^^^^
| ^^^^ the borrow produces a value
|
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = &&42;
| ^^^^^^^

error: unused borrow that must be used
--> $DIR/unused-borrows.rs:15:5
|
LL | &&mut 42;
| ^^^^^^^^
| ^^^^^^^^ the borrow produces a value
|
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = &&mut 42;
| ^^^^^^^

error: unused borrow that must be used
--> $DIR/unused-borrows.rs:18:5
|
LL | &mut &42;
| ^^^^^^^^
| ^^^^^^^^ the borrow produces a value
|
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = &mut &42;
| ^^^^^^^

error: unused borrow that must be used
--> $DIR/unused-borrows.rs:23:5
|
LL | && foo(42);
| ^^^^^^^^^^
| ^^^^^^^^^^ the borrow produces a value
|
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = && foo(42);
| ^^^^^^^

error: aborting due to 6 previous errors

13 changes: 13 additions & 0 deletions src/test/ui/lint/unused/issue-85913.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![deny(unused_must_use)]

pub fn fun() -> i32 {
function() && return 1;
//~^ ERROR: unused logical operation that must be used
return 0;
}

fn function() -> bool {
true
}

fn main() {}
18 changes: 18 additions & 0 deletions src/test/ui/lint/unused/issue-85913.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: unused logical operation that must be used
--> $DIR/issue-85913.rs:4:5
|
LL | function() && return 1;
| ^^^^^^^^^^^^^^^^^^^^^^ the logical operation produces a value
|
note: the lint level is defined here
--> $DIR/issue-85913.rs:1:9
|
LL | #![deny(unused_must_use)]
| ^^^^^^^^^^^^^^^
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = function() && return 1;
| ^^^^^^^

error: aborting due to previous error

Loading