Skip to content

Commit f2b97fd

Browse files
committed
Fix 2 variable binding issues in let_underscore
1 parent f688dd6 commit f2b97fd

File tree

6 files changed

+92
-1
lines changed

6 files changed

+92
-1
lines changed

compiler/rustc_lint/src/let_underscore.rs

+5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
108108
if !matches!(local.pat.kind, hir::PatKind::Wild) {
109109
return;
110110
}
111+
112+
if matches!(local.source, rustc_hir::LocalSource::AsyncFn) {
113+
return;
114+
}
111115
if let Some(init) = local.init {
112116
let init_ty = cx.typeck_results().expr_ty(init);
113117
// If the type has a trivial Drop implementation, then it doesn't
@@ -126,6 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
126130
suggestion: local.pat.span,
127131
multi_suggestion_start: local.span.until(init.span),
128132
multi_suggestion_end: init.span.shrink_to_hi(),
133+
default_binding_mode: local.pat.default_binding_modes,
129134
};
130135
if is_sync_lock {
131136
let mut span = MultiSpan::from_spans(vec![local.pat.span, init.span]);

compiler/rustc_lint/src/lints.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ pub struct NonBindingLetSub {
950950
pub suggestion: Span,
951951
pub multi_suggestion_start: Span,
952952
pub multi_suggestion_end: Span,
953+
pub default_binding_mode: bool,
953954
}
954955

955956
impl AddToDiagnostic for NonBindingLetSub {
@@ -960,10 +961,11 @@ impl AddToDiagnostic for NonBindingLetSub {
960961
rustc_errors::SubdiagnosticMessage,
961962
) -> rustc_errors::SubdiagnosticMessage,
962963
{
964+
let prefix = if self.default_binding_mode { "" } else { "let " };
963965
diag.span_suggestion_verbose(
964966
self.suggestion,
965967
fluent::lint_non_binding_let_suggestion,
966-
"_unused",
968+
format!("{prefix}_unused"),
967969
Applicability::MachineApplicable,
968970
);
969971
diag.multipart_suggestion(
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// edition: 2021
2+
3+
#![deny(let_underscore_drop)]
4+
fn main() {
5+
let _ = foo(); //~ ERROR non-binding let on a type that implements `Drop`
6+
}
7+
8+
async fn from_config(_: Config) {}
9+
10+
async fn foo() {
11+
from_config(Config {
12+
nickname: None,
13+
..Default::default()
14+
})
15+
.await;
16+
}
17+
18+
#[derive(Default)]
19+
struct Config {
20+
nickname: Option<Box<u8>>,
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: non-binding let on a type that implements `Drop`
2+
--> $DIR/issue-119696-err-on-fn.rs:5:5
3+
|
4+
LL | let _ = foo();
5+
| ^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-119696-err-on-fn.rs:3:9
9+
|
10+
LL | #![deny(let_underscore_drop)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
help: consider binding to an unused variable to avoid immediately dropping the value
13+
|
14+
LL | let _unused = foo();
15+
| ~~~~~~~
16+
help: consider immediately dropping the value
17+
|
18+
LL | drop(foo());
19+
| ~~~~~ +
20+
21+
error: aborting due to 1 previous error
22+
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![deny(let_underscore_drop)]
2+
#![feature(type_alias_impl_trait)]
3+
4+
pub struct Foo {
5+
/// This type must have nontrivial drop glue
6+
field: String,
7+
}
8+
9+
pub type Tait = impl Sized;
10+
11+
pub fn ice_cold(beverage: Tait) {
12+
// Must destructure at least one field of `Foo`
13+
let Foo { field } = beverage;
14+
// boom
15+
_ = field; //~ ERROR non-binding let on a type that implements `Drop`
16+
}
17+
18+
19+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: non-binding let on a type that implements `Drop`
2+
--> $DIR/issue-119697-extra-let.rs:15:5
3+
|
4+
LL | _ = field;
5+
| ^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-119697-extra-let.rs:1:9
9+
|
10+
LL | #![deny(let_underscore_drop)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
help: consider binding to an unused variable to avoid immediately dropping the value
13+
|
14+
LL | let _unused = field;
15+
| ~~~~~~~~~~~
16+
help: consider immediately dropping the value
17+
|
18+
LL | drop(field);
19+
| ~~~~~ +
20+
21+
error: aborting due to 1 previous error
22+

0 commit comments

Comments
 (0)