From 7a80c23f838b832e651a717767c41a2536f3ae42 Mon Sep 17 00:00:00 2001 From: SabrinaJewson Date: Wed, 30 Mar 2022 19:31:50 +0100 Subject: [PATCH 1/3] Suggest from_utf8_unchecked in const contexts --- .../src/transmute/transmute_ref_to_ref.rs | 12 +++++++----- tests/ui/transmute.rs | 7 +++++-- tests/ui/transmute.stderr | 16 +++++++++++----- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/transmute/transmute_ref_to_ref.rs b/clippy_lints/src/transmute/transmute_ref_to_ref.rs index 7570bc2a7a8..9c893cacf4f 100644 --- a/clippy_lints/src/transmute/transmute_ref_to_ref.rs +++ b/clippy_lints/src/transmute/transmute_ref_to_ref.rs @@ -32,17 +32,19 @@ pub(super) fn check<'tcx>( "" }; + let snippet = snippet(cx, arg.span, ".."); + span_lint_and_sugg( cx, TRANSMUTE_BYTES_TO_STR, e.span, &format!("transmute from a `{}` to a `{}`", from_ty, to_ty), "consider using", - format!( - "std::str::from_utf8{}({}).unwrap()", - postfix, - snippet(cx, arg.span, ".."), - ), + if const_context { + format!("unsafe {{ std::str::from_utf8_unchecked{postfix}({snippet}) }}") + } else { + format!("std::str::from_utf8{postfix}({snippet}).unwrap()") + }, Applicability::Unspecified, ); triggered = true; diff --git a/tests/ui/transmute.rs b/tests/ui/transmute.rs index 54f9727d8ea..5b688ce4d64 100644 --- a/tests/ui/transmute.rs +++ b/tests/ui/transmute.rs @@ -134,9 +134,12 @@ const fn test_const() { } } -fn bytes_to_str(b: &[u8], mb: &mut [u8]) { - let _: &str = unsafe { std::mem::transmute(b) }; +fn bytes_to_str(mb: &mut [u8]) { + const B: &[u8] = b""; + + let _: &str = unsafe { std::mem::transmute(B) }; let _: &mut str = unsafe { std::mem::transmute(mb) }; + const _: &str = unsafe { std::mem::transmute(B) }; } fn main() {} diff --git a/tests/ui/transmute.stderr b/tests/ui/transmute.stderr index 5c60fc2d511..1213d192ae9 100644 --- a/tests/ui/transmute.stderr +++ b/tests/ui/transmute.stderr @@ -227,18 +227,24 @@ LL | let _: [u8; 16] = std::mem::transmute(0i128); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i128.to_ne_bytes()` error: transmute from a `&[u8]` to a `&str` - --> $DIR/transmute.rs:138:28 + --> $DIR/transmute.rs:140:28 | -LL | let _: &str = unsafe { std::mem::transmute(b) }; - | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(b).unwrap()` +LL | let _: &str = unsafe { std::mem::transmute(B) }; + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(B).unwrap()` | = note: `-D clippy::transmute-bytes-to-str` implied by `-D warnings` error: transmute from a `&mut [u8]` to a `&mut str` - --> $DIR/transmute.rs:139:32 + --> $DIR/transmute.rs:141:32 | LL | let _: &mut str = unsafe { std::mem::transmute(mb) }; | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()` -error: aborting due to 38 previous errors +error: transmute from a `&[u8]` to a `&str` + --> $DIR/transmute.rs:142:30 + | +LL | const _: &str = unsafe { std::mem::transmute(B) }; + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `unsafe { std::str::from_utf8_unchecked(B) }` + +error: aborting due to 39 previous errors From 11045f94e267805e90662577ee7486cba5dbcf18 Mon Sep 17 00:00:00 2001 From: SabrinaJewson Date: Fri, 1 Apr 2022 06:32:22 +0100 Subject: [PATCH 2/3] Don't unnecessarily suggest unsafe block --- clippy_lints/src/transmute/transmute_ref_to_ref.rs | 2 +- tests/ui/transmute.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/transmute/transmute_ref_to_ref.rs b/clippy_lints/src/transmute/transmute_ref_to_ref.rs index 9c893cacf4f..fe3e88613f4 100644 --- a/clippy_lints/src/transmute/transmute_ref_to_ref.rs +++ b/clippy_lints/src/transmute/transmute_ref_to_ref.rs @@ -41,7 +41,7 @@ pub(super) fn check<'tcx>( &format!("transmute from a `{}` to a `{}`", from_ty, to_ty), "consider using", if const_context { - format!("unsafe {{ std::str::from_utf8_unchecked{postfix}({snippet}) }}") + format!("std::str::from_utf8_unchecked{postfix}({snippet})") } else { format!("std::str::from_utf8{postfix}({snippet}).unwrap()") }, diff --git a/tests/ui/transmute.stderr b/tests/ui/transmute.stderr index 1213d192ae9..72a386e8fa6 100644 --- a/tests/ui/transmute.stderr +++ b/tests/ui/transmute.stderr @@ -244,7 +244,7 @@ error: transmute from a `&[u8]` to a `&str` --> $DIR/transmute.rs:142:30 | LL | const _: &str = unsafe { std::mem::transmute(B) }; - | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `unsafe { std::str::from_utf8_unchecked(B) }` + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_unchecked(B)` error: aborting due to 39 previous errors From 41ef4f729f3df237918229ff960c84792dd3231d Mon Sep 17 00:00:00 2001 From: Sabrina Jewson Date: Wed, 6 Apr 2022 10:14:20 +0100 Subject: [PATCH 3/3] Report `from_utf8` suggestion as maybe incorrect Co-authored-by: Philipp Krones --- clippy_lints/src/transmute/transmute_ref_to_ref.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/transmute/transmute_ref_to_ref.rs b/clippy_lints/src/transmute/transmute_ref_to_ref.rs index fe3e88613f4..786e7bfc56f 100644 --- a/clippy_lints/src/transmute/transmute_ref_to_ref.rs +++ b/clippy_lints/src/transmute/transmute_ref_to_ref.rs @@ -45,7 +45,7 @@ pub(super) fn check<'tcx>( } else { format!("std::str::from_utf8{postfix}({snippet}).unwrap()") }, - Applicability::Unspecified, + Applicability::MaybeIncorrect, ); triggered = true; } else {