Skip to content

Commit faaf6d6

Browse files
authored
let_underscore_future: skip bindings with an explicit type annotation (#17001)
Currently `let_underscore_future` fires on `let _: T = expr` even when the user writes the type explicitly. The reporter's argument is that a typed `let _:` is an intentional discard, separate practice from the unannotated form. This is the same rationale `let_underscore_untyped` documents. This gates the lint on `local.ty.is_none()` so `let _: Pin<Box<dyn Future<Output = ()>>> = foo();` no longer warns, while bare `let _ = foo();` still does. Test case added under `tests/ui/let_underscore_future.rs` covering both shapes; `.stderr` updated for the line-number shift. Fixes #16991 ---- changelog: none
2 parents 77fc280 + f057719 commit faaf6d6

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

clippy_lints/src/let_underscore.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
162162
);
163163
},
164164
);
165-
} else if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
165+
} else if local.ty.is_none()
166+
&& let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
166167
&& implements_trait(cx, cx.typeck_results().expr_ty(init), future_trait_def_id, &[])
167168
{
168169
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]

tests/ui/let_underscore_future.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::future::Future;
2+
use std::pin::Pin;
23

34
async fn some_async_fn() {}
45

@@ -10,6 +11,10 @@ fn custom() -> impl Future<Output = ()> {
1011

1112
fn do_something_to_future(future: &mut impl Future<Output = ()>) {}
1213

14+
fn boxed() -> Pin<Box<dyn Future<Output = ()>>> {
15+
Box::pin(async {})
16+
}
17+
1318
fn main() {
1419
let _ = some_async_fn();
1520
//~^ let_underscore_future
@@ -21,4 +26,7 @@ fn main() {
2126
do_something_to_future(&mut future);
2227
let _ = future;
2328
//~^ let_underscore_future
29+
30+
// Typed bindings are an intentional discard, see also `let_underscore_untyped`.
31+
let _: Pin<Box<dyn Future<Output = ()>>> = boxed();
2432
}

tests/ui/let_underscore_future.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: non-binding `let` on a future
2-
--> tests/ui/let_underscore_future.rs:14:5
2+
--> tests/ui/let_underscore_future.rs:19:5
33
|
44
LL | let _ = some_async_fn();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -9,15 +9,15 @@ LL | let _ = some_async_fn();
99
= help: to override `-D warnings` add `#[allow(clippy::let_underscore_future)]`
1010

1111
error: non-binding `let` on a future
12-
--> tests/ui/let_underscore_future.rs:17:5
12+
--> tests/ui/let_underscore_future.rs:22:5
1313
|
1414
LL | let _ = custom();
1515
| ^^^^^^^^^^^^^^^^^
1616
|
1717
= help: consider awaiting the future or dropping explicitly with `std::mem::drop`
1818

1919
error: non-binding `let` on a future
20-
--> tests/ui/let_underscore_future.rs:22:5
20+
--> tests/ui/let_underscore_future.rs:27:5
2121
|
2222
LL | let _ = future;
2323
| ^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)