From 73c026b73f247d38461ffd04b7115de28a9a3f2a Mon Sep 17 00:00:00 2001 From: Takayuki Nakata Date: Wed, 27 Oct 2021 09:12:13 +0900 Subject: [PATCH 1/2] Add test case --- tests/ui/crashes/auxiliary/ice-7868-aux.rs | 3 +++ tests/ui/crashes/ice-7868.rs | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 tests/ui/crashes/auxiliary/ice-7868-aux.rs create mode 100644 tests/ui/crashes/ice-7868.rs diff --git a/tests/ui/crashes/auxiliary/ice-7868-aux.rs b/tests/ui/crashes/auxiliary/ice-7868-aux.rs new file mode 100644 index 0000000000000..bee29894b63d5 --- /dev/null +++ b/tests/ui/crashes/auxiliary/ice-7868-aux.rs @@ -0,0 +1,3 @@ +fn zero() { + unsafe { 0 }; +} diff --git a/tests/ui/crashes/ice-7868.rs b/tests/ui/crashes/ice-7868.rs new file mode 100644 index 0000000000000..cfe4e55294875 --- /dev/null +++ b/tests/ui/crashes/ice-7868.rs @@ -0,0 +1,6 @@ +#![warn(clippy::undocumented_unsafe_blocks)] + +#[path = "auxiliary/ice-7868-aux.rs"] +mod zero; + +fn main() {} From 35bf041c21eeb5e4ce9d892c01741a179811c811 Mon Sep 17 00:00:00 2001 From: Takayuki Nakata Date: Thu, 28 Oct 2021 22:10:59 +0900 Subject: [PATCH 2/2] Fix ICE in `undocumented_unsafe_blocks` --- clippy_lints/src/undocumented_unsafe_blocks.rs | 5 +++-- tests/ui/crashes/ice-7868.rs | 1 + tests/ui/crashes/ice-7868.stderr | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/ui/crashes/ice-7868.stderr diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs index e08e4d03c7efe..11aef50991b0a 100644 --- a/clippy_lints/src/undocumented_unsafe_blocks.rs +++ b/clippy_lints/src/undocumented_unsafe_blocks.rs @@ -145,8 +145,9 @@ impl UndocumentedUnsafeBlocks { let file_name = source_map.span_to_filename(between_span); let source_file = source_map.get_source_file(&file_name)?; - let lex_start = (between_span.lo().0 + 1) as usize; - let src_str = source_file.src.as_ref()?[lex_start..between_span.hi().0 as usize].to_string(); + let lex_start = (between_span.lo().0 - source_file.start_pos.0 + 1) as usize; + let lex_end = (between_span.hi().0 - source_file.start_pos.0) as usize; + let src_str = source_file.src.as_ref()?[lex_start..lex_end].to_string(); let mut pos = 0; let mut comment = false; diff --git a/tests/ui/crashes/ice-7868.rs b/tests/ui/crashes/ice-7868.rs index cfe4e55294875..c6932164e3bff 100644 --- a/tests/ui/crashes/ice-7868.rs +++ b/tests/ui/crashes/ice-7868.rs @@ -1,4 +1,5 @@ #![warn(clippy::undocumented_unsafe_blocks)] +#![allow(clippy::no_effect)] #[path = "auxiliary/ice-7868-aux.rs"] mod zero; diff --git a/tests/ui/crashes/ice-7868.stderr b/tests/ui/crashes/ice-7868.stderr new file mode 100644 index 0000000000000..d7b49eb89a28b --- /dev/null +++ b/tests/ui/crashes/ice-7868.stderr @@ -0,0 +1,15 @@ +error: unsafe block missing a safety comment + --> $DIR/auxiliary/ice-7868-aux.rs:2:5 + | +LL | unsafe { 0 }; + | ^^^^^^^^^^^^ + | + = note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings` +help: consider adding a safety comment + | +LL ~ // Safety: ... +LL ~ unsafe { 0 }; + | + +error: aborting due to previous error +