Skip to content

Commit 5bd8cff

Browse files
committed
Fixed range of suggestions
1 parent c33c091 commit 5bd8cff

File tree

8 files changed

+86
-90
lines changed

8 files changed

+86
-90
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,12 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
191191

192192
let mk_spans = |pos: BytePos| {
193193
let source_map = cx.tcx.sess.source_map();
194-
let span = Span::new(pos, pos, SyntaxContext::root(), None);
195-
let help_span = source_map.span_extend_to_next_char(span, '\n', true);
194+
let help_span = Span::new(
195+
pos,
196+
pos + BytePos(u32::try_from("SAFETY:".len()).unwrap()),
197+
SyntaxContext::root(),
198+
None,
199+
);
196200
let span = if source_map.is_multiline(item.span) {
197201
source_map.span_until_char(item.span, '\n')
198202
} else {
@@ -270,22 +274,12 @@ fn check_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>, (span, h
270274
if is_doc {
271275
// If it's already within a doc comment, we try to suggest the change
272276

273-
let source_map = cx.sess().source_map();
274-
if let Ok(unsafe_line) = source_map.lookup_line(item.span.lo())
275-
&& let Some(src) = unsafe_line.sf.src.as_deref()
276-
{
277-
let help_pos_lo = source_map.lookup_byte_offset(help_span.lo());
278-
let help_pos_hi = source_map.lookup_byte_offset(help_span.hi());
279-
280-
diag.span_suggestion(
281-
help_span,
282-
"consider changing it to a `# Safety` section",
283-
src.get(help_pos_lo.pos.to_usize()..help_pos_hi.pos.to_usize())
284-
.unwrap()
285-
.replace("SAFETY:", "# Safety"),
286-
Applicability::MachineApplicable,
287-
);
288-
}
277+
diag.span_suggestion(
278+
help_span,
279+
"consider changing it to a `# Safety` section",
280+
"# Safety",
281+
Applicability::MachineApplicable,
282+
);
289283
} else {
290284
diag.span_help(
291285
help_span,
@@ -814,9 +808,11 @@ fn text_has_safety_comment(
814808
in_codeblock = !in_codeblock;
815809
}
816810

817-
if !in_codeblock && line.to_ascii_uppercase().contains("SAFETY:") {
811+
if !in_codeblock && let Some(safety_pos) = line.to_ascii_uppercase().find("SAFETY:") {
818812
return SafetyComment::Present {
819-
pos: start_pos + BytePos(u32::try_from(line_start).unwrap()),
813+
pos: start_pos
814+
+ BytePos(u32::try_from(line_start).unwrap())
815+
+ BytePos(u32::try_from(safety_pos).unwrap()),
820816
is_doc: line.starts_with("///"),
821817
};
822818
}

tests/ui-cargo/undocumented_unsafe_blocks/fail/Cargo.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ error: module has unnecessary safety comment
55
| ^^^^^^^^
66
|
77
help: consider removing the safety comment
8-
--> src/main.rs:1:1
8+
--> src/main.rs:1:4
99
|
1010
1 | // SAFETY: ...
11-
| ^^^^^^^^^^^^^^
11+
| ^^^^^^^
1212
= note: requested on the command line with `-D clippy::unnecessary-safety-comment`
1313

1414
error: module has unnecessary safety comment
@@ -18,9 +18,9 @@ error: module has unnecessary safety comment
1818
| ^^^^^^^^
1919
|
2020
help: consider removing the safety comment
21-
--> src/main.rs:4:1
21+
--> src/main.rs:4:4
2222
|
2323
4 | // SAFETY: ...
24-
| ^^^^^^^^^^^^^^
24+
| ^^^^^^^
2525

2626
error: could not compile `undocumented_unsafe_blocks` (bin "undocumented_unsafe_blocks") due to 2 previous errors

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ LL | const BIG_NUMBER: i32 = 1000000;
247247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248248
|
249249
help: consider removing the safety comment
250-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:507:5
250+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:507:8
251251
|
252252
LL | // SAFETY:
253-
| ^^^^^^^^^^
253+
| ^^^^^^^
254254
= note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings`
255255
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_safety_comment)]`
256256

@@ -289,10 +289,10 @@ LL | | };
289289
| |______^
290290
|
291291
help: consider removing the safety comment
292-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:542:5
292+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:542:8
293293
|
294294
LL | // SAFETY: this is more than one level away, so it should warn
295-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
295+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
296296

297297
error: unsafe block missing a safety comment
298298
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:545:12
@@ -349,10 +349,10 @@ LL | const UNIX_EPOCH_JULIAN_DAY: i32 =
349349
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
350350
|
351351
help: consider removing the safety comment
352-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:699:5
352+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:699:8
353353
|
354354
LL | // SAFETY: fail ONLY if `accept-comment-above-attribute = false`
355-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
355+
| ^^^^^^^
356356

357357
error: statement has unnecessary safety comment
358358
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:721:5
@@ -361,10 +361,10 @@ LL | _ = bar();
361361
| ^^^^^^^^^^
362362
|
363363
help: consider removing the safety comment
364-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:720:5
364+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:720:8
365365
|
366366
LL | // SAFETY: unnecessary_safety_comment triggers here
367-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
367+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
368368

369369
error: module has unnecessary safety comment
370370
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:741:5
@@ -373,10 +373,10 @@ LL | mod x {}
373373
| ^^^^^^^^
374374
|
375375
help: consider removing the safety comment
376-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:740:5
376+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:740:8
377377
|
378378
LL | // SAFETY: ...
379-
| ^^^^^^^^^^^^^^
379+
| ^^^^^^^
380380

381381
error: module has unnecessary safety comment
382382
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:746:5
@@ -385,10 +385,10 @@ LL | mod y {}
385385
| ^^^^^^^^
386386
|
387387
help: consider removing the safety comment
388-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:744:5
388+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:744:8
389389
|
390390
LL | // SAFETY: ...
391-
| ^^^^^^^^^^^^^^
391+
| ^^^^^^^
392392

393393
error: module has unnecessary safety comment
394394
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:751:5
@@ -397,10 +397,10 @@ LL | mod z {}
397397
| ^^^^^^^^
398398
|
399399
help: consider removing the safety comment
400-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:750:5
400+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:750:8
401401
|
402402
LL | // SAFETY: ...
403-
| ^^^^^^^^^^^^^^
403+
| ^^^^^^^
404404

405405
error: module has unnecessary safety comment
406406
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:759:5
@@ -409,10 +409,10 @@ LL | mod y {}
409409
| ^^^^^^^^
410410
|
411411
help: consider removing the safety comment
412-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:757:5
412+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:757:8
413413
|
414414
LL | // SAFETY: ...
415-
| ^^^^^^^^^^^^^^
415+
| ^^^^^^^
416416

417417
error: statement has unnecessary safety comment
418418
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:774:9
@@ -421,10 +421,10 @@ LL | let x = 34;
421421
| ^^^^^^^^^^^
422422
|
423423
help: consider removing the safety comment
424-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:772:9
424+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:772:12
425425
|
426426
LL | // SAFETY: ...
427-
| ^^^^^^^^^^^^^^
427+
| ^^^^^^^^^^^
428428

429429
error: function has unnecessary safety comment
430430
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:781:5
@@ -433,10 +433,10 @@ LL | unsafe fn unsafe_comment() {}
433433
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
434434
|
435435
help: consider changing the `SAFETY` comment for a `# Safety` doc comment
436-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:780:5
436+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:780:8
437437
|
438438
LL | // SAFETY: Bla
439-
| ^^^^^^^^^^^^^^
439+
| ^^^^^^^
440440

441441
error: function has unnecessary safety comment
442442
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:787:5
@@ -448,7 +448,7 @@ help: consider changing the `SAFETY` comment for a `# Safety` doc comment
448448
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:785:8
449449
|
450450
LL | SAFETY: Bla
451-
| ^^^^^^^^^^^
451+
| ^^^^^^^
452452

453453
error: function has unnecessary safety comment
454454
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:791:5
@@ -457,10 +457,10 @@ LL | fn safe_comment() {}
457457
| ^^^^^^^^^^^^^^^^^^^^
458458
|
459459
help: consider removing the safety comment
460-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:790:5
460+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:790:8
461461
|
462462
LL | // SAFETY: Bla
463-
| ^^^^^^^^^^^^^^
463+
| ^^^^^^^
464464

465465
error: function has unnecessary safety comment
466466
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:795:5
@@ -469,10 +469,10 @@ LL | fn safe_doc_comment() {}
469469
| ^^^^^^^^^^^^^^^^^^^^^^^^
470470
|
471471
help: consider removing the safety comment
472-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:794:5
472+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:794:9
473473
|
474474
LL | /// SAFETY: Bla
475-
| ^^^^^^^^^^^^^^^
475+
| ^^^^^^^
476476

477477
error: aborting due to 50 previous errors
478478

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ LL | const BIG_NUMBER: i32 = 1000000;
247247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248248
|
249249
help: consider removing the safety comment
250-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:507:5
250+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:507:8
251251
|
252252
LL | // SAFETY:
253-
| ^^^^^^^^^^
253+
| ^^^^^^^
254254
= note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings`
255255
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_safety_comment)]`
256256

@@ -297,10 +297,10 @@ LL | | };
297297
| |______^
298298
|
299299
help: consider removing the safety comment
300-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:542:5
300+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:542:8
301301
|
302302
LL | // SAFETY: this is more than one level away, so it should warn
303-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
303+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
304304

305305
error: unsafe block missing a safety comment
306306
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:545:12
@@ -445,10 +445,10 @@ LL | _ = bar();
445445
| ^^^^^^^^^^
446446
|
447447
help: consider removing the safety comment
448-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:720:5
448+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:720:8
449449
|
450450
LL | // SAFETY: unnecessary_safety_comment triggers here
451-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
451+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
452452

453453
error: unsafe block missing a safety comment
454454
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:735:12
@@ -465,10 +465,10 @@ LL | mod x {}
465465
| ^^^^^^^^
466466
|
467467
help: consider removing the safety comment
468-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:740:5
468+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:740:8
469469
|
470470
LL | // SAFETY: ...
471-
| ^^^^^^^^^^^^^^
471+
| ^^^^^^^
472472

473473
error: module has unnecessary safety comment
474474
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:751:5
@@ -477,10 +477,10 @@ LL | mod z {}
477477
| ^^^^^^^^
478478
|
479479
help: consider removing the safety comment
480-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:750:5
480+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:750:8
481481
|
482482
LL | // SAFETY: ...
483-
| ^^^^^^^^^^^^^^
483+
| ^^^^^^^
484484

485485
error: unsafe block missing a safety comment
486486
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:766:9
@@ -497,10 +497,10 @@ LL | unsafe fn unsafe_comment() {}
497497
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
498498
|
499499
help: consider changing the `SAFETY` comment for a `# Safety` doc comment
500-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:780:5
500+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:780:8
501501
|
502502
LL | // SAFETY: Bla
503-
| ^^^^^^^^^^^^^^
503+
| ^^^^^^^
504504

505505
error: function has unnecessary safety comment
506506
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:787:5
@@ -512,7 +512,7 @@ help: consider changing the `SAFETY` comment for a `# Safety` doc comment
512512
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:785:8
513513
|
514514
LL | SAFETY: Bla
515-
| ^^^^^^^^^^^
515+
| ^^^^^^^
516516

517517
error: function has unnecessary safety comment
518518
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:791:5
@@ -521,10 +521,10 @@ LL | fn safe_comment() {}
521521
| ^^^^^^^^^^^^^^^^^^^^
522522
|
523523
help: consider removing the safety comment
524-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:790:5
524+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:790:8
525525
|
526526
LL | // SAFETY: Bla
527-
| ^^^^^^^^^^^^^^
527+
| ^^^^^^^
528528

529529
error: function has unnecessary safety comment
530530
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:795:5
@@ -533,10 +533,10 @@ LL | fn safe_doc_comment() {}
533533
| ^^^^^^^^^^^^^^^^^^^^^^^^
534534
|
535535
help: consider removing the safety comment
536-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:794:5
536+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:794:9
537537
|
538538
LL | /// SAFETY: Bla
539-
| ^^^^^^^^^^^^^^^
539+
| ^^^^^^^
540540

541541
error: aborting due to 60 previous errors
542542

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: function has unnecessary safety comment
22
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks_fixable.rs:10:5
33
|
44
LL | /// SAFETY: Bla
5-
| --------------- help: consider changing it to a `# Safety` section: `/// # Safety Bla`
5+
| ------- help: consider changing it to a `# Safety` section: `# Safety`
66
LL | unsafe fn unsafe_doc_comment() {}
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88
|
@@ -13,7 +13,7 @@ error: function has unnecessary safety comment
1313
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks_fixable.rs:16:5
1414
|
1515
LL | * SAFETY: Bla
16-
| ----------- help: consider changing it to a `# Safety` section: `# Safety Bla`
16+
| ------- help: consider changing it to a `# Safety` section: `# Safety`
1717
LL | */
1818
LL | unsafe fn unsafe_block_doc_comment() {}
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: function has unnecessary safety comment
22
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks_fixable.rs:10:5
33
|
44
LL | /// SAFETY: Bla
5-
| --------------- help: consider changing it to a `# Safety` section: `/// # Safety Bla`
5+
| ------- help: consider changing it to a `# Safety` section: `# Safety`
66
LL | unsafe fn unsafe_doc_comment() {}
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88
|
@@ -13,7 +13,7 @@ error: function has unnecessary safety comment
1313
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks_fixable.rs:16:5
1414
|
1515
LL | * SAFETY: Bla
16-
| ----------- help: consider changing it to a `# Safety` section: `# Safety Bla`
16+
| ------- help: consider changing it to a `# Safety` section: `# Safety`
1717
LL | */
1818
LL | unsafe fn unsafe_block_doc_comment() {}
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)