Skip to content

Commit e4b3f01

Browse files
committed
fix: transmute_bytes_to_str wrongly unmangled macros
1 parent 62589a2 commit e4b3f01

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

clippy_lints/src/transmute/transmute_ref_to_ref.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{TRANSMUTE_BYTES_TO_STR, TRANSMUTE_PTR_TO_PTR};
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
3-
use clippy_utils::source::snippet;
3+
use clippy_utils::source::snippet_with_context;
44
use clippy_utils::{std_or_core, sugg};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, Mutability};
@@ -29,7 +29,8 @@ pub(super) fn check<'tcx>(
2929

3030
let postfix = if from_mutbl == Mutability::Mut { "_mut" } else { "" };
3131

32-
let snippet = snippet(cx, arg.span, "..");
32+
let mut applicability = Applicability::MachineApplicable;
33+
let (snippet, _) = snippet_with_context(cx, arg.span, e.span.ctxt(), "..", &mut applicability);
3334

3435
span_lint_and_sugg(
3536
cx,

tests/ui/transmute.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,17 @@ fn bytes_to_str(mb: &mut [u8]) {
128128
//~^ transmute_bytes_to_str
129129
}
130130

131+
fn issue16104() {
132+
let b = vec![1_u8, 2_u8];
133+
macro_rules! take_ref {
134+
($x:expr) => {
135+
$x.as_slice()
136+
};
137+
}
138+
unsafe {
139+
let _: &str = std::mem::transmute(take_ref!(b));
140+
//~^ transmute_bytes_to_str
141+
}
142+
}
143+
131144
fn main() {}

tests/ui/transmute.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,11 @@ error: transmute from a `&[u8]` to a `&str`
106106
LL | const _: &str = unsafe { std::mem::transmute(B) };
107107
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_unchecked(B)`
108108

109-
error: aborting due to 16 previous errors
109+
error: transmute from a `&[u8]` to a `&str`
110+
--> tests/ui/transmute.rs:139:23
111+
|
112+
LL | let _: &str = std::mem::transmute(take_ref!(b));
113+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(take_ref!(b)).unwrap()`
114+
115+
error: aborting due to 17 previous errors
110116

0 commit comments

Comments
 (0)