Skip to content

Commit a274d9b

Browse files
committed
Use inner span in undocumented_unsafe_blocks
1 parent 3671f0c commit a274d9b

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,15 +606,33 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
606606

607607
fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
608608
let body = cx.enclosing_body?;
609-
for (_, parent_node) in cx.tcx.hir().parent_iter(body.hir_id) {
609+
let map = cx.tcx.hir();
610+
let mut maybe_mod_item = None;
611+
612+
for (_, parent_node) in map.parent_iter(body.hir_id) {
610613
match parent_node {
611614
Node::Crate(mod_) => return Some(mod_.spans.inner_span),
615+
Node::Item(hir::Item {
616+
kind: ItemKind::Mod(mod_),
617+
span,
618+
..
619+
}) => {
620+
return maybe_mod_item
621+
.and_then(|item| comment_start_before_item_in_mod(cx, mod_, *span, &item))
622+
.map(|comment_start| mod_.spans.inner_span.with_lo(comment_start))
623+
.or(Some(*span));
624+
},
612625
node if let Some((span, _)) = span_and_hid_of_item_alike_node(&node)
613626
&& !is_const_or_static(&node) =>
614627
{
615628
return Some(span);
616629
},
617-
_ => {},
630+
Node::Item(item) => {
631+
maybe_mod_item = Some(*item);
632+
},
633+
_ => {
634+
maybe_mod_item = None;
635+
},
618636
}
619637
}
620638
None

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.default.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,39 +311,39 @@ LL | let bar = unsafe {};
311311
= help: consider adding a safety comment on the preceding line
312312

313313
error: unsafe block missing a safety comment
314-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:592:52
314+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:638:52
315315
|
316316
LL | const NO_SAFETY_IN_TRAIT_BUT_IN_IMPL: u8 = unsafe { 0 };
317317
| ^^^^^^^^^^^^
318318
|
319319
= help: consider adding a safety comment on the preceding line
320320

321321
error: unsafe block missing a safety comment
322-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:601:41
322+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:647:41
323323
|
324324
LL | const NO_SAFETY_IN_TRAIT: i32 = unsafe { 1 };
325325
| ^^^^^^^^^^^^
326326
|
327327
= help: consider adding a safety comment on the preceding line
328328

329329
error: unsafe block missing a safety comment
330-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:611:42
330+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:657:42
331331
|
332332
LL | const HAS_SAFETY_IN_TRAIT: i32 = unsafe { 3 };
333333
| ^^^^^^^^^^^^
334334
|
335335
= help: consider adding a safety comment on the preceding line
336336

337337
error: unsafe block missing a safety comment
338-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:616:40
338+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:662:40
339339
|
340340
LL | const NO_SAFETY_IN_IMPL: i32 = unsafe { 1 };
341341
| ^^^^^^^^^^^^
342342
|
343343
= help: consider adding a safety comment on the preceding line
344344

345345
error: unsafe block missing a safety comment
346-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:627:9
346+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:673:9
347347
|
348348
LL | unsafe { here_is_another_variable_with_long_name };
349349
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.disabled.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,39 +391,39 @@ LL | unsafe {}
391391
= help: consider adding a safety comment on the preceding line
392392

393393
error: unsafe block missing a safety comment
394-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:592:52
394+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:638:52
395395
|
396396
LL | const NO_SAFETY_IN_TRAIT_BUT_IN_IMPL: u8 = unsafe { 0 };
397397
| ^^^^^^^^^^^^
398398
|
399399
= help: consider adding a safety comment on the preceding line
400400

401401
error: unsafe block missing a safety comment
402-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:601:41
402+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:647:41
403403
|
404404
LL | const NO_SAFETY_IN_TRAIT: i32 = unsafe { 1 };
405405
| ^^^^^^^^^^^^
406406
|
407407
= help: consider adding a safety comment on the preceding line
408408

409409
error: unsafe block missing a safety comment
410-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:611:42
410+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:657:42
411411
|
412412
LL | const HAS_SAFETY_IN_TRAIT: i32 = unsafe { 3 };
413413
| ^^^^^^^^^^^^
414414
|
415415
= help: consider adding a safety comment on the preceding line
416416

417417
error: unsafe block missing a safety comment
418-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:616:40
418+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:662:40
419419
|
420420
LL | const NO_SAFETY_IN_IMPL: i32 = unsafe { 1 };
421421
| ^^^^^^^^^^^^
422422
|
423423
= help: consider adding a safety comment on the preceding line
424424

425425
error: unsafe block missing a safety comment
426-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:627:9
426+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:673:9
427427
|
428428
LL | unsafe { here_is_another_variable_with_long_name };
429429
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ fn issue_13024() {
671671
// SAFETY: good
672672
just_a_simple_variable_with_a_very_long_name_that_has_seventy_eight_characters =
673673
unsafe { here_is_another_variable_with_long_name };
674+
//~^ undocumented_unsafe_blocks
674675
}
675676

676677
fn main() {}

0 commit comments

Comments
 (0)