Skip to content

Commit aa8a82e

Browse files
comUserDummynot-elm
authored andcommitted
FIX: issue-12279
---- UPDATE: add async block into test. FIX: no_effect Fixed asynchronous function parameter names with underscores so that warnings are not displayed when underscores are added to parameter names ADD: test case
1 parent 6aa5f1a commit aa8a82e

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 3 additions & 2 deletions
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};
@@ -178,6 +178,7 @@ impl NoEffect {
178178
}
179179
} else if let StmtKind::Local(local) = stmt.kind {
180180
if !is_lint_allowed(cx, NO_EFFECT_UNDERSCORE_BINDING, local.hir_id)
181+
&& !matches!(local.source, LocalSource::AsyncFn)
181182
&& let Some(init) = local.init
182183
&& local.els.is_none()
183184
&& !local.pat.span.from_expansion()

tests/ui/no_effect_async_fn.rs

Lines changed: 50 additions & 0 deletions
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

Lines changed: 29 additions & 0 deletions
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)