Skip to content

Commit e234dfa

Browse files
committed
Ignoring let_underscore_untyped warnings in code from proc macros
1 parent a167973 commit e234dfa

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

clippy_lints/src/let_underscore.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::is_from_proc_macro;
23
use clippy_utils::ty::{implements_trait, is_must_use_ty, match_type};
34
use clippy_utils::{is_must_use_func_call, paths};
45
use rustc_hir::{ExprKind, Local, PatKind};
@@ -138,7 +139,7 @@ const SYNC_GUARD_PATHS: [&[&str]; 3] = [
138139
];
139140

140141
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
141-
fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
142+
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &Local<'tcx>) {
142143
if !in_external_macro(cx.tcx.sess, local.span)
143144
&& let PatKind::Wild = local.pat.kind
144145
&& let Some(init) = local.init
@@ -191,15 +192,20 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
191192
if local.pat.default_binding_modes && local.ty.is_none() {
192193
// When `default_binding_modes` is true, the `let` keyword is present.
193194

194-
// Ignore function calls that return impl traits...
195-
if let Some(init) = local.init &&
196-
matches!(init.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(_, _, _, _)) {
197-
let expr_ty = cx.typeck_results().expr_ty(init);
198-
if expr_ty.is_impl_trait() {
199-
return;
200-
}
201-
}
202-
195+
if let Some(init) = local.init {
196+
// Ignore function calls that return impl traits...
197+
if matches!(init.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(_, _, _, _)) {
198+
let expr_ty = cx.typeck_results().expr_ty(init);
199+
if expr_ty.is_impl_trait() {
200+
return;
201+
}
202+
}
203+
204+
// Ignore if it is from a procedural macro...
205+
if is_from_proc_macro(cx, init) {
206+
return;
207+
}
208+
}
203209

204210
span_lint_and_help(
205211
cx,

tests/ui/let_underscore_untyped.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
//@aux-build: proc_macros.rs
2+
13
#![allow(unused)]
24
#![warn(clippy::let_underscore_untyped)]
35

6+
extern crate proc_macros;
7+
use proc_macros::with_span;
8+
9+
use clippy_utils::is_from_proc_macro;
410
use std::future::Future;
511
use std::{boxed::Box, fmt::Display};
612

@@ -32,6 +38,14 @@ fn g() -> impl Fn() {
3238
|| {}
3339
}
3440

41+
with_span!(
42+
span
43+
44+
fn dont_lint_proc_macro() {
45+
let _ = a();
46+
}
47+
);
48+
3549
fn main() {
3650
let _ = a();
3751
let _ = b(1);

tests/ui/let_underscore_untyped.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
11
error: non-binding `let` without a type annotation
2-
--> $DIR/let_underscore_untyped.rs:36:5
2+
--> $DIR/let_underscore_untyped.rs:50:5
33
|
44
LL | let _ = a();
55
| ^^^^^^^^^^^^
66
|
77
help: consider adding a type annotation
8-
--> $DIR/let_underscore_untyped.rs:36:10
8+
--> $DIR/let_underscore_untyped.rs:50:10
99
|
1010
LL | let _ = a();
1111
| ^
1212
= note: `-D clippy::let-underscore-untyped` implied by `-D warnings`
1313

1414
error: non-binding `let` without a type annotation
15-
--> $DIR/let_underscore_untyped.rs:37:5
15+
--> $DIR/let_underscore_untyped.rs:51:5
1616
|
1717
LL | let _ = b(1);
1818
| ^^^^^^^^^^^^^
1919
|
2020
help: consider adding a type annotation
21-
--> $DIR/let_underscore_untyped.rs:37:10
21+
--> $DIR/let_underscore_untyped.rs:51:10
2222
|
2323
LL | let _ = b(1);
2424
| ^
2525

2626
error: non-binding `let` without a type annotation
27-
--> $DIR/let_underscore_untyped.rs:39:5
27+
--> $DIR/let_underscore_untyped.rs:53:5
2828
|
2929
LL | let _ = d(&1);
3030
| ^^^^^^^^^^^^^^
3131
|
3232
help: consider adding a type annotation
33-
--> $DIR/let_underscore_untyped.rs:39:10
33+
--> $DIR/let_underscore_untyped.rs:53:10
3434
|
3535
LL | let _ = d(&1);
3636
| ^
3737

3838
error: non-binding `let` without a type annotation
39-
--> $DIR/let_underscore_untyped.rs:40:5
39+
--> $DIR/let_underscore_untyped.rs:54:5
4040
|
4141
LL | let _ = e();
4242
| ^^^^^^^^^^^^
4343
|
4444
help: consider adding a type annotation
45-
--> $DIR/let_underscore_untyped.rs:40:10
45+
--> $DIR/let_underscore_untyped.rs:54:10
4646
|
4747
LL | let _ = e();
4848
| ^
4949

5050
error: non-binding `let` without a type annotation
51-
--> $DIR/let_underscore_untyped.rs:41:5
51+
--> $DIR/let_underscore_untyped.rs:55:5
5252
|
5353
LL | let _ = f();
5454
| ^^^^^^^^^^^^
5555
|
5656
help: consider adding a type annotation
57-
--> $DIR/let_underscore_untyped.rs:41:10
57+
--> $DIR/let_underscore_untyped.rs:55:10
5858
|
5959
LL | let _ = f();
6060
| ^

0 commit comments

Comments
 (0)