Skip to content

Commit 96fbd21

Browse files
authored
Rollup merge of rust-lang#119704 - chenyukang:yukang-fix-let_underscore, r=Nilstrieb
Fix two variable binding issues in lint let_underscore Fixes rust-lang#119696 Fixes rust-lang#119697
2 parents e46e279 + 75df38e commit 96fbd21

File tree

7 files changed

+110
-2
lines changed

7 files changed

+110
-2
lines changed

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ pub enum LocalSource {
19971997
AsyncFn,
19981998
/// A desugared `<expr>.await`.
19991999
AwaitDesugar,
2000-
/// A desugared `expr = expr`, where the LHS is a tuple, struct or array.
2000+
/// A desugared `expr = expr`, where the LHS is a tuple, struct, array or underscore expression.
20012001
/// The span is that of the `=` sign.
20022002
AssignDesugar(Span),
20032003
}

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+
is_assign_desugar: matches!(local.source, rustc_hir::LocalSource::AssignDesugar(_)),
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
@@ -932,17 +932,19 @@ pub struct NonBindingLetSub {
932932
pub suggestion: Span,
933933
pub multi_suggestion_start: Span,
934934
pub multi_suggestion_end: Span,
935+
pub is_assign_desugar: bool,
935936
}
936937

937938
impl AddToDiagnostic for NonBindingLetSub {
938939
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
939940
where
940941
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
941942
{
943+
let prefix = if self.is_assign_desugar { "let " } else { "" };
942944
diag.span_suggestion_verbose(
943945
self.suggestion,
944946
fluent::lint_non_binding_let_suggestion,
945-
"_unused",
947+
format!("{prefix}_unused"),
946948
Applicability::MachineApplicable,
947949
);
948950
diag.multipart_suggestion(
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
let _ = field; //~ ERROR non-binding let on a type that implements `Drop`
18+
}
19+
20+
21+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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: non-binding let on a type that implements `Drop`
22+
--> $DIR/issue-119697-extra-let.rs:17:5
23+
|
24+
LL | let _ = field;
25+
| ^^^^^^^^^^^^^^
26+
|
27+
help: consider binding to an unused variable to avoid immediately dropping the value
28+
|
29+
LL | let _unused = field;
30+
| ~~~~~~~
31+
help: consider immediately dropping the value
32+
|
33+
LL | drop(field);
34+
| ~~~~~ +
35+
36+
error: aborting due to 2 previous errors
37+

0 commit comments

Comments
 (0)