Skip to content

Commit c9a97e1

Browse files
committed
Do not lint macro generated codes
`disallowed_names` was used to lint code generated by macros. This behavior can confuse programmers who did not write the macro themselves. This change suppresses lints for code originating from macros, including external ones. changelog: [`disallowed_names`] do not lint macro generated codes
1 parent eafef84 commit c9a97e1

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

clippy_lints/src/disallowed_names.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::is_in_test;
3+
use clippy_utils::{is_from_proc_macro, is_in_test};
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_hir::{Pat, PatKind};
66
use rustc_lint::{LateContext, LateLintPass};
@@ -43,8 +43,10 @@ impl_lint_pass!(DisallowedNames => [DISALLOWED_NAMES]);
4343
impl<'tcx> LateLintPass<'tcx> for DisallowedNames {
4444
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
4545
if let PatKind::Binding(.., ident, _) = pat.kind
46+
&& !ident.span.from_expansion()
4647
&& self.disallow.contains(&ident.name)
4748
&& !is_in_test(cx.tcx, pat.hir_id)
49+
&& !is_from_proc_macro(cx, &ident)
4850
{
4951
span_lint(
5052
cx,

tests/ui/disallowed_names.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:proc_macros.rs
12
#![allow(
23
dead_code,
34
clippy::needless_if,
@@ -9,6 +10,9 @@
910
)]
1011
#![warn(clippy::disallowed_names)]
1112

13+
extern crate proc_macros;
14+
use proc_macros::{external, with_span};
15+
1216
fn test(foo: ()) {}
1317
//~^ disallowed_names
1418

@@ -66,6 +70,17 @@ fn issue_1647_ref_mut() {
6670
//~^ disallowed_names
6771
}
6872

73+
pub fn issue_14958_proc_macro() {
74+
// does not lint macro-generated code
75+
external! {
76+
let foo = 0;
77+
}
78+
with_span! {
79+
span
80+
let foo = 0;
81+
}
82+
}
83+
6984
#[cfg(test)]
7085
mod tests {
7186
fn issue_7305() {

tests/ui/disallowed_names.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: use of a disallowed/placeholder name `foo`
2-
--> tests/ui/disallowed_names.rs:12:9
2+
--> tests/ui/disallowed_names.rs:16:9
33
|
44
LL | fn test(foo: ()) {}
55
| ^^^
@@ -8,79 +8,79 @@ LL | fn test(foo: ()) {}
88
= help: to override `-D warnings` add `#[allow(clippy::disallowed_names)]`
99

1010
error: use of a disallowed/placeholder name `foo`
11-
--> tests/ui/disallowed_names.rs:16:9
11+
--> tests/ui/disallowed_names.rs:20:9
1212
|
1313
LL | let foo = 42;
1414
| ^^^
1515

1616
error: use of a disallowed/placeholder name `baz`
17-
--> tests/ui/disallowed_names.rs:19:9
17+
--> tests/ui/disallowed_names.rs:23:9
1818
|
1919
LL | let baz = 42;
2020
| ^^^
2121

2222
error: use of a disallowed/placeholder name `quux`
23-
--> tests/ui/disallowed_names.rs:22:9
23+
--> tests/ui/disallowed_names.rs:26:9
2424
|
2525
LL | let quux = 42;
2626
| ^^^^
2727

2828
error: use of a disallowed/placeholder name `foo`
29-
--> tests/ui/disallowed_names.rs:35:10
29+
--> tests/ui/disallowed_names.rs:39:10
3030
|
3131
LL | (foo, Some(baz), quux @ Some(_)) => (),
3232
| ^^^
3333

3434
error: use of a disallowed/placeholder name `baz`
35-
--> tests/ui/disallowed_names.rs:35:20
35+
--> tests/ui/disallowed_names.rs:39:20
3636
|
3737
LL | (foo, Some(baz), quux @ Some(_)) => (),
3838
| ^^^
3939

4040
error: use of a disallowed/placeholder name `quux`
41-
--> tests/ui/disallowed_names.rs:35:26
41+
--> tests/ui/disallowed_names.rs:39:26
4242
|
4343
LL | (foo, Some(baz), quux @ Some(_)) => (),
4444
| ^^^^
4545

4646
error: use of a disallowed/placeholder name `foo`
47-
--> tests/ui/disallowed_names.rs:43:19
47+
--> tests/ui/disallowed_names.rs:47:19
4848
|
4949
LL | fn issue_1647(mut foo: u8) {
5050
| ^^^
5151

5252
error: use of a disallowed/placeholder name `baz`
53-
--> tests/ui/disallowed_names.rs:46:13
53+
--> tests/ui/disallowed_names.rs:50:13
5454
|
5555
LL | let mut baz = 0;
5656
| ^^^
5757

5858
error: use of a disallowed/placeholder name `quux`
59-
--> tests/ui/disallowed_names.rs:49:21
59+
--> tests/ui/disallowed_names.rs:53:21
6060
|
6161
LL | if let Some(mut quux) = Some(42) {}
6262
| ^^^^
6363

6464
error: use of a disallowed/placeholder name `baz`
65-
--> tests/ui/disallowed_names.rs:54:13
65+
--> tests/ui/disallowed_names.rs:58:13
6666
|
6767
LL | let ref baz = 0;
6868
| ^^^
6969

7070
error: use of a disallowed/placeholder name `quux`
71-
--> tests/ui/disallowed_names.rs:57:21
71+
--> tests/ui/disallowed_names.rs:61:21
7272
|
7373
LL | if let Some(ref quux) = Some(42) {}
7474
| ^^^^
7575

7676
error: use of a disallowed/placeholder name `baz`
77-
--> tests/ui/disallowed_names.rs:62:17
77+
--> tests/ui/disallowed_names.rs:66:17
7878
|
7979
LL | let ref mut baz = 0;
8080
| ^^^
8181

8282
error: use of a disallowed/placeholder name `quux`
83-
--> tests/ui/disallowed_names.rs:65:25
83+
--> tests/ui/disallowed_names.rs:69:25
8484
|
8585
LL | if let Some(ref mut quux) = Some(42) {}
8686
| ^^^^

0 commit comments

Comments
 (0)