Skip to content

Commit 7a818c0

Browse files
committed
get the expected number of errors by acknowledging that other lints are covering the same ground
1 parent 1e5c14d commit 7a818c0

File tree

5 files changed

+100
-45
lines changed

5 files changed

+100
-45
lines changed

src/tools/clippy/clippy_lints/src/transmute.rs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ declare_clippy_lint! {
5050
"transmutes that have the same to and from types or could be a cast/coercion"
5151
}
5252

53+
// FIXME: Merge this lint with USELESS_TRANSMUTE once that is out of the nursery.
54+
declare_clippy_lint! {
55+
/// **What it does:**Checks for transmutes that could be a pointer cast.
56+
///
57+
/// **Why is this bad?** Readability. The code tricks people into thinking that
58+
/// something complex is going on.
59+
///
60+
/// **Known problems:** None.
61+
///
62+
/// **Example:**
63+
///
64+
/// ```rust,ignore
65+
/// core::intrinsics::transmute::<*const [i32], *const [u16]>(p)
66+
/// ```
67+
/// Use instead:
68+
/// ```rust
69+
/// p as *const [u16]
70+
/// ```
71+
pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
72+
complexity,
73+
"transmutes that could be a pointer cast"
74+
}
75+
5376
declare_clippy_lint! {
5477
/// **What it does:** Checks for transmutes between a type `T` and `*T`.
5578
///
@@ -272,27 +295,6 @@ declare_clippy_lint! {
272295
"transmute between collections of layout-incompatible types"
273296
}
274297

275-
declare_clippy_lint! {
276-
/// **What it does:**
277-
///
278-
/// **Why is this bad?**
279-
///
280-
/// **Known problems:** None.
281-
///
282-
/// **Example:**
283-
///
284-
/// ```rust
285-
/// // example code where clippy issues a warning
286-
/// ```
287-
/// Use instead:
288-
/// ```rust
289-
/// // example code which does not raise clippy warning
290-
/// ```
291-
pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
292-
complexity,
293-
"default lint description"
294-
}
295-
296298
declare_lint_pass!(Transmute => [
297299
CROSSPOINTER_TRANSMUTE,
298300
TRANSMUTE_PTR_TO_REF,
@@ -330,26 +332,6 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
330332
let from_ty = cx.typeck_results().expr_ty(&args[0]);
331333
let to_ty = cx.typeck_results().expr_ty(e);
332334

333-
if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) {
334-
span_lint_and_then(
335-
cx,
336-
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
337-
e.span,
338-
&format!(
339-
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
340-
from_ty,
341-
to_ty
342-
),
343-
|diag| {
344-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
345-
let sugg = format!("{} as {}", arg, to_ty);
346-
diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
347-
}
348-
}
349-
);
350-
return
351-
}
352-
353335
match (&from_ty.kind, &to_ty.kind) {
354336
_ if from_ty == to_ty => span_lint(
355337
cx,
@@ -646,6 +628,22 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
646628
);
647629
}
648630
},
631+
(_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then(
632+
cx,
633+
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
634+
e.span,
635+
&format!(
636+
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
637+
from_ty,
638+
to_ty
639+
),
640+
|diag| {
641+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
642+
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
643+
diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
644+
}
645+
}
646+
),
649647
_ => {
650648
return;
651649
},

src/tools/clippy/tests/ui/transmute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code)]
2-
#![allow(clippy::transmutes_expressible_as_ptr_casts)]
32

43
extern crate core;
4+
55
use std::mem::transmute as my_transmute;
66
use std::vec::Vec as MyVec;
77

src/tools/clippy/tests/ui/transmute_ptr_to_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::transmute_ptr_to_ptr)]
2-
#![allow(clippy::transmutes_expressible_as_ptr_casts)]
2+
33
// Make sure we can modify lifetimes, which is one of the recommended uses
44
// of transmute
55

src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
#![warn(clippy::transmutes_expressible_as_ptr_casts)]
2+
// These two warnings currrently cover the cases transmutes_expressible_as_ptr_casts
3+
// would otherwise be responsible for
4+
#![warn(clippy::useless_transmute)]
5+
#![warn(clippy::transmute_ptr_to_ptr)]
6+
7+
use std::mem::transmute;
28

39
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
410
// valid, which we quote from below.
5-
use std::mem::transmute;
611

712
fn main() {
13+
// We should see an error message for each transmute, and no error messages for
14+
// the casts, since the casts are the recommended fixes.
15+
816
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
917
let ptr_i32_transmute = unsafe {
1018
transmute::<isize, *const i32>(-1)
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
Should have 7 errors, one for each transmute
1+
error: transmute from an integer to a pointer
2+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:18:9
3+
|
4+
LL | transmute::<isize, *const i32>(-1)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-1 as *const i32`
6+
|
7+
= note: `-D clippy::useless-transmute` implied by `-D warnings`
8+
9+
error: transmute from a pointer to a pointer
10+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:24:9
11+
|
12+
LL | transmute::<*const i32, *const i8>(ptr_i32)
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
14+
|
15+
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
16+
17+
error: transmute from a pointer to a pointer
18+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:32:9
19+
|
20+
LL | transmute::<*const [i32], *const [u16]>(slice_ptr)
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]`
22+
23+
error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead
24+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:40:9
25+
|
26+
LL | transmute::<*const i32, usize>(ptr_i32)
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
28+
|
29+
= note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings`
30+
31+
error: transmute from a reference to a pointer
32+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:48:9
33+
|
34+
LL | transmute::<&[i32; 4], *const [i32; 4]>(array_ref)
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`
36+
37+
error: transmute from `fn(usize) -> u8 {main::foo}` to `*const usize` which could be expressed as a pointer cast instead
38+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:56:9
39+
|
40+
LL | transmute::<fn(usize) -> u8, *const usize>(foo)
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
42+
43+
error: transmute from `fn(usize) -> u8 {main::foo}` to `usize` which could be expressed as a pointer cast instead
44+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:62:9
45+
|
46+
LL | transmute::<fn(usize) -> u8, usize>(foo)
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
48+
49+
error: aborting due to 7 previous errors
50+

0 commit comments

Comments
 (0)