Skip to content

Commit 4195522

Browse files
committed
Auto merge of rust-lang#10701 - blyxyas:fix-fp-let_underscore_untyped, r=llogiq
Bugfix: Ignore `impl Trait`(s) @ `let_underscore_untyped` Fixes rust-lang#10411 changelog:[`let_underscore_untyped`]: Ignore `impl Trait`(s) that caused false positives.
2 parents 9a33778 + bdd0545 commit 4195522

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

clippy_lints/src/let_underscore.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::{implements_trait, is_must_use_ty, match_type};
33
use clippy_utils::{is_must_use_func_call, paths};
4-
use rustc_hir::{Local, PatKind};
4+
use rustc_hir::{ExprKind, Local, PatKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_middle::ty::subst::GenericArgKind;
@@ -189,7 +189,18 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
189189

190190
if local.pat.default_binding_modes && local.ty.is_none() {
191191
// When `default_binding_modes` is true, the `let` keyword is present.
192-
span_lint_and_help(
192+
193+
// Ignore function calls that return impl traits...
194+
if let Some(init) = local.init &&
195+
matches!(init.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(_, _, _, _)) {
196+
let expr_ty = cx.typeck_results().expr_ty(init);
197+
if expr_ty.is_impl_trait() {
198+
return;
199+
}
200+
}
201+
202+
203+
span_lint_and_help(
193204
cx,
194205
LET_UNDERSCORE_UNTYPED,
195206
local.span,

tests/ui/let_underscore_untyped.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ fn f() -> Box<dyn Display> {
2828
Box::new(1)
2929
}
3030

31+
fn g() -> impl Fn() {
32+
|| {}
33+
}
34+
3135
fn main() {
3236
let _ = a();
3337
let _ = b(1);
3438
let _ = c();
3539
let _ = d(&1);
3640
let _ = e();
3741
let _ = f();
42+
let _ = g();
3843

3944
_ = a();
4045
_ = b(1);
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: non-binding `let` without a type annotation
2-
--> $DIR/let_underscore_untyped.rs:32:5
2+
--> $DIR/let_underscore_untyped.rs:36:5
33
|
44
LL | let _ = a();
55
| ^^^^^^^^^^^^
@@ -8,44 +8,36 @@ LL | let _ = a();
88
= note: `-D clippy::let-underscore-untyped` implied by `-D warnings`
99

1010
error: non-binding `let` without a type annotation
11-
--> $DIR/let_underscore_untyped.rs:33:5
11+
--> $DIR/let_underscore_untyped.rs:37:5
1212
|
1313
LL | let _ = b(1);
1414
| ^^^^^^^^^^^^^
1515
|
1616
= help: consider adding a type annotation or removing the `let` keyword
1717

1818
error: non-binding `let` without a type annotation
19-
--> $DIR/let_underscore_untyped.rs:34:5
20-
|
21-
LL | let _ = c();
22-
| ^^^^^^^^^^^^
23-
|
24-
= help: consider adding a type annotation or removing the `let` keyword
25-
26-
error: non-binding `let` without a type annotation
27-
--> $DIR/let_underscore_untyped.rs:35:5
19+
--> $DIR/let_underscore_untyped.rs:39:5
2820
|
2921
LL | let _ = d(&1);
3022
| ^^^^^^^^^^^^^^
3123
|
3224
= help: consider adding a type annotation or removing the `let` keyword
3325

3426
error: non-binding `let` without a type annotation
35-
--> $DIR/let_underscore_untyped.rs:36:5
27+
--> $DIR/let_underscore_untyped.rs:40:5
3628
|
3729
LL | let _ = e();
3830
| ^^^^^^^^^^^^
3931
|
4032
= help: consider adding a type annotation or removing the `let` keyword
4133

4234
error: non-binding `let` without a type annotation
43-
--> $DIR/let_underscore_untyped.rs:37:5
35+
--> $DIR/let_underscore_untyped.rs:41:5
4436
|
4537
LL | let _ = f();
4638
| ^^^^^^^^^^^^
4739
|
4840
= help: consider adding a type annotation or removing the `let` keyword
4941

50-
error: aborting due to 6 previous errors
42+
error: aborting due to 5 previous errors
5143

0 commit comments

Comments
 (0)