about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-10-28 13:37:40 +0000
committerbors <bors@rust-lang.org>2021-10-28 13:37:40 +0000
commit7788af9c47de03a8c7ff854cbdfbb6696042d4ab (patch)
tree87cf304483642ed15f1ca1c3561eb8162ce4073f
parent89a11564cc6f247bfb744f1587bc44e0ce1bb9cf (diff)
parent35bf041c21eeb5e4ce9d892c01741a179811c811 (diff)
downloadrust-7788af9c47de03a8c7ff854cbdfbb6696042d4ab.tar.gz
rust-7788af9c47de03a8c7ff854cbdfbb6696042d4ab.zip
Auto merge of #7891 - giraffate:fix_ice_for_undocumented_unsafe_blocks, r=flip1995
Fix ice in `undocumented_unsafe_blocks`

Fix https://github.com/rust-lang/rust-clippy/issues/7868

changelog: Fix ice in [`undocumented_unsafe_blocks`]
-rw-r--r--clippy_lints/src/undocumented_unsafe_blocks.rs5
-rw-r--r--tests/ui/crashes/auxiliary/ice-7868-aux.rs3
-rw-r--r--tests/ui/crashes/ice-7868.rs7
-rw-r--r--tests/ui/crashes/ice-7868.stderr15
4 files changed, 28 insertions, 2 deletions
diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs
index e08e4d03c7e..11aef50991b 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/auxiliary/ice-7868-aux.rs b/tests/ui/crashes/auxiliary/ice-7868-aux.rs
new file mode 100644
index 00000000000..bee29894b63
--- /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 00000000000..c6932164e3b
--- /dev/null
+++ b/tests/ui/crashes/ice-7868.rs
@@ -0,0 +1,7 @@
+#![warn(clippy::undocumented_unsafe_blocks)]
+#![allow(clippy::no_effect)]
+
+#[path = "auxiliary/ice-7868-aux.rs"]
+mod zero;
+
+fn main() {}
diff --git a/tests/ui/crashes/ice-7868.stderr b/tests/ui/crashes/ice-7868.stderr
new file mode 100644
index 00000000000..d7b49eb89a2
--- /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
+