Skip to content

Commit 067e21f

Browse files
committed
only lint exported macros
1 parent eb98f26 commit 067e21f

File tree

3 files changed

+84
-43
lines changed

3 files changed

+84
-43
lines changed

clippy_lints/src/expr_metavars_in_unsafe.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,28 @@ declare_clippy_lint! {
8181
/// }
8282
/// }
8383
///
84-
/// assert_eq!(first!(&[1]), 1);
84+
/// assert_eq!(*first!(&[1i32]), 1);
8585
///
8686
/// // This will compile as a consequence (note the lack of `unsafe {}`)
87-
/// assert_eq!(first!(std::hint::unreachable_unchecked()), 1);
87+
/// assert_eq!(*first!(std::hint::unreachable_unchecked() as &[i32]), 1);
8888
/// ```
8989
/// Use instead:
9090
/// ```compile_fail
9191
/// macro_rules! first {
92-
/// ($slice:expr) => {
92+
/// ($slice:expr) => {{
9393
/// let slice = $slice; // ✅ outside of `unsafe {}`
9494
/// unsafe {
9595
/// assert!(!slice.is_empty());
9696
/// // SAFETY: slice is checked to have at least one element
9797
/// slice.first().unwrap_unchecked()
9898
/// }
99-
/// }
99+
/// }}
100100
/// }
101101
///
102+
/// assert_eq!(*first!(&[1]), 1);
103+
///
102104
/// // This won't compile:
103-
/// assert_eq!(first!(std::hint::unreachable_unchecked()), 1);
105+
/// assert_eq!(*first!(std::hint::unreachable_unchecked() as &[i32]), 1);
104106
/// ```
105107
#[clippy::version = "1.77.0"]
106108
pub EXPR_METAVARS_IN_UNSAFE,
@@ -356,7 +358,10 @@ fn with_token_stream(tt: Option<&TokenTree>, f: impl FnOnce(&TokenStream)) {
356358

357359
impl<'tcx> LateLintPass<'tcx> for ExprMetavarsInUnsafe {
358360
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
359-
if let ItemKind::Macro(def, MacroKind::Bang) = item.kind {
361+
if let ItemKind::Macro(def, MacroKind::Bang) = item.kind
362+
&& let def_id = item.owner_id.def_id
363+
&& (cx.effective_visibilities.is_exported(def_id) || cx.tcx.has_attr(def_id, sym::macro_export))
364+
{
360365
let mut tts = def.body.tokens.trees().peekable();
361366
let mut finder = UnsafeMetavariableFinder {
362367
enclosing: Vec::new(),

tests/ui/expr_metavars_in_unsafe.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
#![feature(decl_macro)]
12
#![warn(clippy::expr_metavars_in_unsafe)]
23
#![allow(clippy::no_effect)]
34

45
#[allow(clippy::expr_metavars_in_unsafe)]
6+
#[macro_export]
57
macro_rules! allow_works {
68
($v:expr) => {
79
unsafe {
@@ -10,6 +12,7 @@ macro_rules! allow_works {
1012
};
1113
}
1214

15+
#[macro_export]
1316
macro_rules! mac {
1417
($v:expr, $num:expr, $r#mod:expr) => {
1518
unsafe { dbg!($v) };
@@ -80,7 +83,21 @@ macro_rules! mac {
8083
}
8184
}
8285

83-
fn main() {
84-
allow_works!(todo!());
85-
mac!((), 1, todo!());
86+
pub macro macro2_0($v:expr) {
87+
unsafe {
88+
$v; //~ ERROR: expanding an expression metavariable in an unsafe block
89+
}
8690
}
91+
92+
// don't lint private macros
93+
// (these are typically fine, e.g. a common "FP" would be using a macro to generate a bunch of impls
94+
// with unsafe blocks)
95+
macro_rules! private_mac {
96+
($v:expr) => {
97+
unsafe {
98+
$v;
99+
}
100+
};
101+
}
102+
103+
fn main() {}
+53-34
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
error: expanding an expression metavariable in an unsafe block
2-
--> $DIR/expr_metavars_in_unsafe.rs:15:23
2+
--> $DIR/expr_metavars_in_unsafe.rs:18:23
33
|
44
LL | unsafe { dbg!($v) };
55
| ^^
66
|
77
note: metavariable declared here
8-
--> $DIR/expr_metavars_in_unsafe.rs:14:6
8+
--> $DIR/expr_metavars_in_unsafe.rs:17:6
99
|
1010
LL | ($v:expr, $num:expr, $r#mod:expr) => {
1111
| ^^^^^^^
1212
note: unsafe block entered here
13-
--> $DIR/expr_metavars_in_unsafe.rs:15:9
13+
--> $DIR/expr_metavars_in_unsafe.rs:18:9
1414
|
1515
LL | unsafe { dbg!($v) };
1616
| ^^^^^^^^
@@ -20,194 +20,213 @@ LL | unsafe { dbg!($v) };
2020
= help: to override `-D warnings` add `#[allow(clippy::expr_metavars_in_unsafe)]`
2121

2222
error: expanding an expression metavariable in an unsafe block
23-
--> $DIR/expr_metavars_in_unsafe.rs:18:13
23+
--> $DIR/expr_metavars_in_unsafe.rs:21:13
2424
|
2525
LL | $r#mod;
2626
| ^^^^^^
2727
|
2828
note: metavariable declared here
29-
--> $DIR/expr_metavars_in_unsafe.rs:14:26
29+
--> $DIR/expr_metavars_in_unsafe.rs:17:26
3030
|
3131
LL | ($v:expr, $num:expr, $r#mod:expr) => {
3232
| ^^^^^^^^^^^
3333
note: unsafe block entered here
34-
--> $DIR/expr_metavars_in_unsafe.rs:17:9
34+
--> $DIR/expr_metavars_in_unsafe.rs:20:9
3535
|
3636
LL | unsafe {
3737
| ^^^^^^^^
3838
= help: consider expanding it outside of this block, e.g. by storing it in a variable
3939
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
4040

4141
error: expanding an expression metavariable in an unsafe block
42-
--> $DIR/expr_metavars_in_unsafe.rs:24:17
42+
--> $DIR/expr_metavars_in_unsafe.rs:27:17
4343
|
4444
LL | $v;
4545
| ^^
4646
|
4747
note: metavariable declared here
48-
--> $DIR/expr_metavars_in_unsafe.rs:14:6
48+
--> $DIR/expr_metavars_in_unsafe.rs:17:6
4949
|
5050
LL | ($v:expr, $num:expr, $r#mod:expr) => {
5151
| ^^^^^^^
5252
note: unsafe block entered here
53-
--> $DIR/expr_metavars_in_unsafe.rs:23:13
53+
--> $DIR/expr_metavars_in_unsafe.rs:26:13
5454
|
5555
LL | unsafe {
5656
| ^^^^^^^^
5757
= help: consider expanding it outside of this block, e.g. by storing it in a variable
5858
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
5959

6060
error: expanding an expression metavariable in an unsafe block
61-
--> $DIR/expr_metavars_in_unsafe.rs:33:17
61+
--> $DIR/expr_metavars_in_unsafe.rs:36:17
6262
|
6363
LL | $v;
6464
| ^^
6565
|
6666
note: metavariable declared here
67-
--> $DIR/expr_metavars_in_unsafe.rs:14:6
67+
--> $DIR/expr_metavars_in_unsafe.rs:17:6
6868
|
6969
LL | ($v:expr, $num:expr, $r#mod:expr) => {
7070
| ^^^^^^^
7171
note: unsafe block entered here
72-
--> $DIR/expr_metavars_in_unsafe.rs:23:13
72+
--> $DIR/expr_metavars_in_unsafe.rs:26:13
7373
|
7474
LL | unsafe {
7575
| ^^^^^^^^
7676
= help: consider expanding it outside of this block, e.g. by storing it in a variable
7777
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
7878

7979
error: expanding an expression metavariable in an unsafe block
80-
--> $DIR/expr_metavars_in_unsafe.rs:37:47
80+
--> $DIR/expr_metavars_in_unsafe.rs:40:47
8181
|
8282
LL | const M: i32 = { unsafe { $num } },
8383
| ^^^^
8484
|
8585
note: metavariable declared here
86-
--> $DIR/expr_metavars_in_unsafe.rs:14:15
86+
--> $DIR/expr_metavars_in_unsafe.rs:17:15
8787
|
8888
LL | ($v:expr, $num:expr, $r#mod:expr) => {
8989
| ^^^^^^^^^
9090
note: unsafe block entered here
91-
--> $DIR/expr_metavars_in_unsafe.rs:37:38
91+
--> $DIR/expr_metavars_in_unsafe.rs:40:38
9292
|
9393
LL | const M: i32 = { unsafe { $num } },
9494
| ^^^^^^^^
9595
= help: consider expanding it outside of this block, e.g. by storing it in a variable
9696
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
9797

9898
error: expanding an expression metavariable in an unsafe block
99-
--> $DIR/expr_metavars_in_unsafe.rs:44:25
99+
--> $DIR/expr_metavars_in_unsafe.rs:47:25
100100
|
101101
LL | $v;
102102
| ^^
103103
|
104104
note: metavariable declared here
105-
--> $DIR/expr_metavars_in_unsafe.rs:14:6
105+
--> $DIR/expr_metavars_in_unsafe.rs:17:6
106106
|
107107
LL | ($v:expr, $num:expr, $r#mod:expr) => {
108108
| ^^^^^^^
109109
note: unsafe block entered here
110-
--> $DIR/expr_metavars_in_unsafe.rs:43:21
110+
--> $DIR/expr_metavars_in_unsafe.rs:46:21
111111
|
112112
LL | unsafe {
113113
| ^^^^^^^^
114114
= help: consider expanding it outside of this block, e.g. by storing it in a variable
115115
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
116116

117117
error: expanding an expression metavariable in an unsafe block
118-
--> $DIR/expr_metavars_in_unsafe.rs:49:30
118+
--> $DIR/expr_metavars_in_unsafe.rs:52:30
119119
|
120120
LL | unsafe { $v };
121121
| ^^
122122
|
123123
note: metavariable declared here
124-
--> $DIR/expr_metavars_in_unsafe.rs:14:6
124+
--> $DIR/expr_metavars_in_unsafe.rs:17:6
125125
|
126126
LL | ($v:expr, $num:expr, $r#mod:expr) => {
127127
| ^^^^^^^
128128
note: unsafe block entered here
129-
--> $DIR/expr_metavars_in_unsafe.rs:49:21
129+
--> $DIR/expr_metavars_in_unsafe.rs:52:21
130130
|
131131
LL | unsafe { $v };
132132
| ^^^^^^^^
133133
= help: consider expanding it outside of this block, e.g. by storing it in a variable
134134
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
135135

136136
error: expanding an expression metavariable in an unsafe block
137-
--> $DIR/expr_metavars_in_unsafe.rs:57:40
137+
--> $DIR/expr_metavars_in_unsafe.rs:60:40
138138
|
139139
LL | const _: Ty<{ unsafe { $v; $num } }> = Ty;
140140
| ^^
141141
|
142142
note: metavariable declared here
143-
--> $DIR/expr_metavars_in_unsafe.rs:14:6
143+
--> $DIR/expr_metavars_in_unsafe.rs:17:6
144144
|
145145
LL | ($v:expr, $num:expr, $r#mod:expr) => {
146146
| ^^^^^^^
147147
note: unsafe block entered here
148-
--> $DIR/expr_metavars_in_unsafe.rs:57:31
148+
--> $DIR/expr_metavars_in_unsafe.rs:60:31
149149
|
150150
LL | const _: Ty<{ unsafe { $v; $num } }> = Ty;
151151
| ^^^^^^^^
152152
= help: consider expanding it outside of this block, e.g. by storing it in a variable
153153
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
154154

155155
error: expanding an expression metavariable in an unsafe block
156-
--> $DIR/expr_metavars_in_unsafe.rs:57:44
156+
--> $DIR/expr_metavars_in_unsafe.rs:60:44
157157
|
158158
LL | const _: Ty<{ unsafe { $v; $num } }> = Ty;
159159
| ^^^^
160160
|
161161
note: metavariable declared here
162-
--> $DIR/expr_metavars_in_unsafe.rs:14:15
162+
--> $DIR/expr_metavars_in_unsafe.rs:17:15
163163
|
164164
LL | ($v:expr, $num:expr, $r#mod:expr) => {
165165
| ^^^^^^^^^
166166
note: unsafe block entered here
167-
--> $DIR/expr_metavars_in_unsafe.rs:57:31
167+
--> $DIR/expr_metavars_in_unsafe.rs:60:31
168168
|
169169
LL | const _: Ty<{ unsafe { $v; $num } }> = Ty;
170170
| ^^^^^^^^
171171
= help: consider expanding it outside of this block, e.g. by storing it in a variable
172172
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
173173

174174
error: expanding an expression metavariable in an unsafe block
175-
--> $DIR/expr_metavars_in_unsafe.rs:67:17
175+
--> $DIR/expr_metavars_in_unsafe.rs:70:17
176176
|
177177
LL | $num;
178178
| ^^^^
179179
|
180180
note: metavariable declared here
181-
--> $DIR/expr_metavars_in_unsafe.rs:14:15
181+
--> $DIR/expr_metavars_in_unsafe.rs:17:15
182182
|
183183
LL | ($v:expr, $num:expr, $r#mod:expr) => {
184184
| ^^^^^^^^^
185185
note: unsafe block entered here
186-
--> $DIR/expr_metavars_in_unsafe.rs:23:13
186+
--> $DIR/expr_metavars_in_unsafe.rs:26:13
187187
|
188188
LL | unsafe {
189189
| ^^^^^^^^
190190
= help: consider expanding it outside of this block, e.g. by storing it in a variable
191191
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
192192

193193
error: expanding an expression metavariable in an unsafe block
194-
--> $DIR/expr_metavars_in_unsafe.rs:77:17
194+
--> $DIR/expr_metavars_in_unsafe.rs:80:17
195195
|
196196
LL | $v;
197197
| ^^
198198
|
199199
note: metavariable declared here
200-
--> $DIR/expr_metavars_in_unsafe.rs:72:8
200+
--> $DIR/expr_metavars_in_unsafe.rs:75:8
201201
|
202202
LL | ($($v:expr),*, $lit:literal) => {
203203
| ^^^^^^^
204204
note: unsafe block entered here
205-
--> $DIR/expr_metavars_in_unsafe.rs:74:13
205+
--> $DIR/expr_metavars_in_unsafe.rs:77:13
206206
|
207207
LL | unsafe {
208208
| ^^^^^^^^
209209
= help: consider expanding it outside of this block, e.g. by storing it in a variable
210210
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
211211

212-
error: aborting due to 11 previous errors
212+
error: expanding an expression metavariable in an unsafe block
213+
--> $DIR/expr_metavars_in_unsafe.rs:88:9
214+
|
215+
LL | $v;
216+
| ^^
217+
|
218+
note: metavariable declared here
219+
--> $DIR/expr_metavars_in_unsafe.rs:86:20
220+
|
221+
LL | pub macro macro2_0($v:expr) {
222+
| ^^^^^^^
223+
note: unsafe block entered here
224+
--> $DIR/expr_metavars_in_unsafe.rs:87:5
225+
|
226+
LL | unsafe {
227+
| ^^^^^^^^
228+
= help: consider expanding it outside of this block, e.g. by storing it in a variable
229+
= note: this allows the user of the macro to write unsafe code without an unsafe block (at the callsite)
230+
231+
error: aborting due to 12 previous errors
213232

0 commit comments

Comments
 (0)