Skip to content

Commit a056ff3

Browse files
committed
Auto merge of rust-lang#12323 - not-elm:fix/issue-12279, r=y21
Fix: no_effect_underscore_binding fires on ignored parameters of async fns Fixes rust-lang#12279 changelog: Fix [`no_effect_underscore_binding`]) The warning is no longer displayed when an underscore is given in the parameter name of an asynchronous function.
2 parents 6aa5f1a + 672bd5e commit a056ff3

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

clippy_lints/src/no_effect.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use clippy_utils::{any_parent_is_automatically_derived, get_parent_node, is_lint
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::{
8-
is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, HirId, HirIdMap, ItemKind, Node, PatKind, Stmt,
9-
StmtKind, UnsafeSource,
8+
is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, HirId, HirIdMap, ItemKind, LocalSource, Node, PatKind,
9+
Stmt, StmtKind, UnsafeSource,
1010
};
1111
use rustc_infer::infer::TyCtxtInferExt as _;
1212
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -43,10 +43,6 @@ declare_clippy_lint! {
4343
/// executed. However, as they have no effect and shouldn't be used further on, all they
4444
/// do is make the code less readable.
4545
///
46-
/// ### Known problems
47-
/// Further usage of this variable is not checked, which can lead to false positives if it is
48-
/// used later in the code.
49-
///
5046
/// ### Example
5147
/// ```rust,ignore
5248
/// let _i_serve_no_purpose = 1;
@@ -178,6 +174,7 @@ impl NoEffect {
178174
}
179175
} else if let StmtKind::Local(local) = stmt.kind {
180176
if !is_lint_allowed(cx, NO_EFFECT_UNDERSCORE_BINDING, local.hir_id)
177+
&& !matches!(local.source, LocalSource::AsyncFn)
181178
&& let Some(init) = local.init
182179
&& local.els.is_none()
183180
&& !local.pat.span.from_expansion()

tests/ui/no_effect_async_fn.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![warn(clippy::no_effect_underscore_binding)]
2+
#![no_main]
3+
4+
trait AsyncTrait {
5+
async fn bar(i: u64);
6+
}
7+
8+
struct Bar;
9+
10+
impl AsyncTrait for Bar {
11+
// Shouldn't lint `binding to `_` prefixed variable with no side-effect`
12+
async fn bar(_i: u64) {
13+
let _a = 0;
14+
//~^ ERROR: binding to `_` prefixed variable with no side-effect
15+
16+
// Shouldn't lint `binding to `_` prefixed variable with no side-effect`
17+
let _b = num();
18+
19+
let _ = async {
20+
let _c = 0;
21+
//~^ ERROR: binding to `_` prefixed variable with no side-effect
22+
23+
// Shouldn't lint `binding to `_` prefixed variable with no side-effect`
24+
let _d = num();
25+
}
26+
.await;
27+
}
28+
}
29+
30+
// Shouldn't lint `binding to `_` prefixed variable with no side-effect`
31+
async fn foo(_i: u64) {
32+
let _a = 0;
33+
//~^ ERROR: binding to `_` prefixed variable with no side-effect
34+
35+
// Shouldn't lint `binding to `_` prefixed variable with no side-effect`
36+
let _b = num();
37+
38+
let _ = async {
39+
let _c = 0;
40+
//~^ ERROR: binding to `_` prefixed variable with no side-effect
41+
42+
// Shouldn't lint `binding to `_` prefixed variable with no side-effect`
43+
let _d = num();
44+
}
45+
.await;
46+
}
47+
48+
fn num() -> usize {
49+
0
50+
}

tests/ui/no_effect_async_fn.stderr

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: binding to `_` prefixed variable with no side-effect
2+
--> tests/ui/no_effect_async_fn.rs:20:17
3+
|
4+
LL | let _c = 0;
5+
| ^^
6+
|
7+
= note: `-D clippy::no-effect-underscore-binding` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::no_effect_underscore_binding)]`
9+
10+
error: binding to `_` prefixed variable with no side-effect
11+
--> tests/ui/no_effect_async_fn.rs:13:13
12+
|
13+
LL | let _a = 0;
14+
| ^^
15+
16+
error: binding to `_` prefixed variable with no side-effect
17+
--> tests/ui/no_effect_async_fn.rs:39:13
18+
|
19+
LL | let _c = 0;
20+
| ^^
21+
22+
error: binding to `_` prefixed variable with no side-effect
23+
--> tests/ui/no_effect_async_fn.rs:32:9
24+
|
25+
LL | let _a = 0;
26+
| ^^
27+
28+
error: aborting due to 4 previous errors
29+

0 commit comments

Comments
 (0)