Skip to content

Commit f057719

Browse files
committed
let_underscore_future: skip when type is annotated
An explicit type annotation on the LHS (`let _: T = expr`) signals an intentional discard, mirroring the rationale for `let_underscore_untyped`. Without this gate, typed bindings such as `let _: Pin<Box<dyn Future<Output = ()>>> = foo();` trigger the lint despite the author explicitly opting in. changelog: [`let_underscore_future`]: no longer triggers when the binding has an explicit type annotation Signed-off-by: ChrisJr404 <chris@hacknow.com>
1 parent d3a82bf commit f057719

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)