Skip to content

Commit 48e527e

Browse files
committed
Auto merge of rust-lang#9117 - Jarcho:unsafe_block_closure, r=giraffate
Fix `undocumented_unsafe_blocks` in closures fixes rust-lang#9114 changelog: Fix `undocumented_unsafe_blocks` not checking for comments before the start of a closure
2 parents c257f09 + 9756679 commit 48e527e

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,28 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
265265
}
266266
}
267267

268+
fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
269+
let body = cx.enclosing_body?;
270+
let map = cx.tcx.hir();
271+
let mut span = map.body(body).value.span;
272+
for (_, node) in map.parent_iter(body.hir_id) {
273+
match node {
274+
Node::Expr(e) => span = e.span,
275+
Node::Block(_) | Node::Arm(_) | Node::Stmt(_) | Node::Local(_) => (),
276+
_ => break,
277+
}
278+
}
279+
Some(span)
280+
}
281+
268282
fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
269283
let source_map = cx.sess().source_map();
270284
let ctxt = span.ctxt();
271285
if ctxt == SyntaxContext::root()
272-
&& let Some(body) = cx.enclosing_body
286+
&& let Some(search_span) = get_body_search_span(cx)
273287
{
274288
if let Ok(unsafe_line) = source_map.lookup_line(span.lo())
275-
&& let Some(body_span) = walk_span_to_context(cx.tcx.hir().body(body).value.span, SyntaxContext::root())
289+
&& let Some(body_span) = walk_span_to_context(search_span, SyntaxContext::root())
276290
&& let Ok(body_line) = source_map.lookup_line(body_span.lo())
277291
&& Lrc::ptr_eq(&unsafe_line.sf, &body_line.sf)
278292
&& let Some(src) = unsafe_line.sf.src.as_deref()

tests/ui/undocumented_unsafe_blocks.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ fn from_proc_macro() {
250250
proc_macro_unsafe::unsafe_block!(token);
251251
}
252252

253+
fn in_closure(x: *const u32) {
254+
// Safety: reason
255+
let _ = || unsafe { *x };
256+
}
257+
253258
// Invalid comments
254259

255260
#[rustfmt::skip]
@@ -351,9 +356,9 @@ mod unsafe_impl_smoke_test {
351356

352357
#[rustfmt::skip]
353358
mod sub_mod2 {
354-
//
359+
//
355360
// SAFETY: ok
356-
//
361+
//
357362

358363
unsafe impl B for (u32) {}
359364
unsafe trait B {}

tests/ui/undocumented_unsafe_blocks.stderr

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unsafe block missing a safety comment
2-
--> $DIR/undocumented_unsafe_blocks.rs:257:19
2+
--> $DIR/undocumented_unsafe_blocks.rs:262:19
33
|
44
LL | /* Safety: */ unsafe {}
55
| ^^^^^^^^^
@@ -8,103 +8,103 @@ LL | /* Safety: */ unsafe {}
88
= help: consider adding a safety comment on the preceding line
99

1010
error: unsafe block missing a safety comment
11-
--> $DIR/undocumented_unsafe_blocks.rs:261:5
11+
--> $DIR/undocumented_unsafe_blocks.rs:266:5
1212
|
1313
LL | unsafe {}
1414
| ^^^^^^^^^
1515
|
1616
= help: consider adding a safety comment on the preceding line
1717

1818
error: unsafe block missing a safety comment
19-
--> $DIR/undocumented_unsafe_blocks.rs:265:14
19+
--> $DIR/undocumented_unsafe_blocks.rs:270:14
2020
|
2121
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
2222
| ^^^^^^^^^^^^^
2323
|
2424
= help: consider adding a safety comment on the preceding line
2525

2626
error: unsafe block missing a safety comment
27-
--> $DIR/undocumented_unsafe_blocks.rs:265:29
27+
--> $DIR/undocumented_unsafe_blocks.rs:270:29
2828
|
2929
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
3030
| ^^^^^^^^^^^^^
3131
|
3232
= help: consider adding a safety comment on the preceding line
3333

3434
error: unsafe block missing a safety comment
35-
--> $DIR/undocumented_unsafe_blocks.rs:265:48
35+
--> $DIR/undocumented_unsafe_blocks.rs:270:48
3636
|
3737
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
3838
| ^^^^^^^^^^^^^
3939
|
4040
= help: consider adding a safety comment on the preceding line
4141

4242
error: unsafe block missing a safety comment
43-
--> $DIR/undocumented_unsafe_blocks.rs:269:18
43+
--> $DIR/undocumented_unsafe_blocks.rs:274:18
4444
|
4545
LL | let _ = (42, unsafe {}, "test", unsafe {});
4646
| ^^^^^^^^^
4747
|
4848
= help: consider adding a safety comment on the preceding line
4949

5050
error: unsafe block missing a safety comment
51-
--> $DIR/undocumented_unsafe_blocks.rs:269:37
51+
--> $DIR/undocumented_unsafe_blocks.rs:274:37
5252
|
5353
LL | let _ = (42, unsafe {}, "test", unsafe {});
5454
| ^^^^^^^^^
5555
|
5656
= help: consider adding a safety comment on the preceding line
5757

5858
error: unsafe block missing a safety comment
59-
--> $DIR/undocumented_unsafe_blocks.rs:273:14
59+
--> $DIR/undocumented_unsafe_blocks.rs:278:14
6060
|
6161
LL | let _ = *unsafe { &42 };
6262
| ^^^^^^^^^^^^^^
6363
|
6464
= help: consider adding a safety comment on the preceding line
6565

6666
error: unsafe block missing a safety comment
67-
--> $DIR/undocumented_unsafe_blocks.rs:278:19
67+
--> $DIR/undocumented_unsafe_blocks.rs:283:19
6868
|
6969
LL | let _ = match unsafe {} {
7070
| ^^^^^^^^^
7171
|
7272
= help: consider adding a safety comment on the preceding line
7373

7474
error: unsafe block missing a safety comment
75-
--> $DIR/undocumented_unsafe_blocks.rs:284:14
75+
--> $DIR/undocumented_unsafe_blocks.rs:289:14
7676
|
7777
LL | let _ = &unsafe {};
7878
| ^^^^^^^^^
7979
|
8080
= help: consider adding a safety comment on the preceding line
8181

8282
error: unsafe block missing a safety comment
83-
--> $DIR/undocumented_unsafe_blocks.rs:288:14
83+
--> $DIR/undocumented_unsafe_blocks.rs:293:14
8484
|
8585
LL | let _ = [unsafe {}; 5];
8686
| ^^^^^^^^^
8787
|
8888
= help: consider adding a safety comment on the preceding line
8989

9090
error: unsafe block missing a safety comment
91-
--> $DIR/undocumented_unsafe_blocks.rs:292:13
91+
--> $DIR/undocumented_unsafe_blocks.rs:297:13
9292
|
9393
LL | let _ = unsafe {};
9494
| ^^^^^^^^^
9595
|
9696
= help: consider adding a safety comment on the preceding line
9797

9898
error: unsafe block missing a safety comment
99-
--> $DIR/undocumented_unsafe_blocks.rs:302:8
99+
--> $DIR/undocumented_unsafe_blocks.rs:307:8
100100
|
101101
LL | t!(unsafe {});
102102
| ^^^^^^^^^
103103
|
104104
= help: consider adding a safety comment on the preceding line
105105

106106
error: unsafe block missing a safety comment
107-
--> $DIR/undocumented_unsafe_blocks.rs:308:13
107+
--> $DIR/undocumented_unsafe_blocks.rs:313:13
108108
|
109109
LL | unsafe {}
110110
| ^^^^^^^^^
@@ -116,55 +116,55 @@ LL | t!();
116116
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)
117117

118118
error: unsafe block missing a safety comment
119-
--> $DIR/undocumented_unsafe_blocks.rs:316:5
119+
--> $DIR/undocumented_unsafe_blocks.rs:321:5
120120
|
121121
LL | unsafe {} // SAFETY:
122122
| ^^^^^^^^^
123123
|
124124
= help: consider adding a safety comment on the preceding line
125125

126126
error: unsafe block missing a safety comment
127-
--> $DIR/undocumented_unsafe_blocks.rs:320:5
127+
--> $DIR/undocumented_unsafe_blocks.rs:325:5
128128
|
129129
LL | unsafe {
130130
| ^^^^^^^^
131131
|
132132
= help: consider adding a safety comment on the preceding line
133133

134134
error: unsafe block missing a safety comment
135-
--> $DIR/undocumented_unsafe_blocks.rs:330:5
135+
--> $DIR/undocumented_unsafe_blocks.rs:335:5
136136
|
137137
LL | unsafe {};
138138
| ^^^^^^^^^
139139
|
140140
= help: consider adding a safety comment on the preceding line
141141

142142
error: unsafe block missing a safety comment
143-
--> $DIR/undocumented_unsafe_blocks.rs:334:20
143+
--> $DIR/undocumented_unsafe_blocks.rs:339:20
144144
|
145145
LL | println!("{}", unsafe { String::from_utf8_unchecked(vec![]) });
146146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147147
|
148148
= help: consider adding a safety comment on the preceding line
149149

150150
error: unsafe impl missing a safety comment
151-
--> $DIR/undocumented_unsafe_blocks.rs:341:5
151+
--> $DIR/undocumented_unsafe_blocks.rs:346:5
152152
|
153153
LL | unsafe impl A for () {}
154154
| ^^^^^^^^^^^^^^^^^^^^^^^
155155
|
156156
= help: consider adding a safety comment on the preceding line
157157

158158
error: unsafe impl missing a safety comment
159-
--> $DIR/undocumented_unsafe_blocks.rs:348:9
159+
--> $DIR/undocumented_unsafe_blocks.rs:353:9
160160
|
161161
LL | unsafe impl B for (u32) {}
162162
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
163163
|
164164
= help: consider adding a safety comment on the preceding line
165165

166166
error: unsafe impl missing a safety comment
167-
--> $DIR/undocumented_unsafe_blocks.rs:369:13
167+
--> $DIR/undocumented_unsafe_blocks.rs:374:13
168168
|
169169
LL | unsafe impl T for $t {}
170170
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -176,7 +176,7 @@ LL | no_safety_comment!(());
176176
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)
177177

178178
error: unsafe impl missing a safety comment
179-
--> $DIR/undocumented_unsafe_blocks.rs:394:13
179+
--> $DIR/undocumented_unsafe_blocks.rs:399:13
180180
|
181181
LL | unsafe impl T for $t {}
182182
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -188,15 +188,15 @@ LL | no_safety_comment!(());
188188
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)
189189

190190
error: unsafe impl missing a safety comment
191-
--> $DIR/undocumented_unsafe_blocks.rs:402:5
191+
--> $DIR/undocumented_unsafe_blocks.rs:407:5
192192
|
193193
LL | unsafe impl T for (i32) {}
194194
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
195195
|
196196
= help: consider adding a safety comment on the preceding line
197197

198198
error: unsafe impl missing a safety comment
199-
--> $DIR/undocumented_unsafe_blocks.rs:394:13
199+
--> $DIR/undocumented_unsafe_blocks.rs:399:13
200200
|
201201
LL | unsafe impl T for $t {}
202202
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -208,55 +208,55 @@ LL | no_safety_comment!(u32);
208208
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)
209209

210210
error: unsafe impl missing a safety comment
211-
--> $DIR/undocumented_unsafe_blocks.rs:408:5
211+
--> $DIR/undocumented_unsafe_blocks.rs:413:5
212212
|
213213
LL | unsafe impl T for (bool) {}
214214
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
215215
|
216216
= help: consider adding a safety comment on the preceding line
217217

218218
error: unsafe impl missing a safety comment
219-
--> $DIR/undocumented_unsafe_blocks.rs:454:5
219+
--> $DIR/undocumented_unsafe_blocks.rs:459:5
220220
|
221221
LL | unsafe impl NoComment for () {}
222222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
223223
|
224224
= help: consider adding a safety comment on the preceding line
225225

226226
error: unsafe impl missing a safety comment
227-
--> $DIR/undocumented_unsafe_blocks.rs:458:19
227+
--> $DIR/undocumented_unsafe_blocks.rs:463:19
228228
|
229229
LL | /* SAFETY: */ unsafe impl InlineComment for () {}
230230
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
231231
|
232232
= help: consider adding a safety comment on the preceding line
233233

234234
error: unsafe impl missing a safety comment
235-
--> $DIR/undocumented_unsafe_blocks.rs:462:5
235+
--> $DIR/undocumented_unsafe_blocks.rs:467:5
236236
|
237237
LL | unsafe impl TrailingComment for () {} // SAFETY:
238238
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
239239
|
240240
= help: consider adding a safety comment on the preceding line
241241

242242
error: unsafe impl missing a safety comment
243-
--> $DIR/undocumented_unsafe_blocks.rs:467:5
243+
--> $DIR/undocumented_unsafe_blocks.rs:472:5
244244
|
245245
LL | unsafe impl Interference for () {}
246246
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
247247
|
248248
= help: consider adding a safety comment on the preceding line
249249

250250
error: unsafe impl missing a safety comment
251-
--> $DIR/undocumented_unsafe_blocks.rs:474:5
251+
--> $DIR/undocumented_unsafe_blocks.rs:479:5
252252
|
253253
LL | unsafe impl ImplInFn for () {}
254254
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
255255
|
256256
= help: consider adding a safety comment on the preceding line
257257

258258
error: unsafe impl missing a safety comment
259-
--> $DIR/undocumented_unsafe_blocks.rs:483:1
259+
--> $DIR/undocumented_unsafe_blocks.rs:488:1
260260
|
261261
LL | unsafe impl CrateRoot for () {}
262262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)