get the expected number of errors by acknowledging that other lints are covering the same ground

This commit is contained in:
Ryan1729 2020-08-03 02:47:25 -06:00
parent de05212987
commit ccc4747f46
5 changed files with 100 additions and 45 deletions

View File

@ -50,6 +50,29 @@
"transmutes that have the same to and from types or could be a cast/coercion"
}
// FIXME: Merge this lint with USELESS_TRANSMUTE once that is out of the nursery.
declare_clippy_lint! {
/// **What it does:**Checks for transmutes that could be a pointer cast.
///
/// **Why is this bad?** Readability. The code tricks people into thinking that
/// something complex is going on.
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust,ignore
/// core::intrinsics::transmute::<*const [i32], *const [u16]>(p)
/// ```
/// Use instead:
/// ```rust
/// p as *const [u16]
/// ```
pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
complexity,
"transmutes that could be a pointer cast"
}
declare_clippy_lint! {
/// **What it does:** Checks for transmutes between a type `T` and `*T`.
///
@ -272,27 +295,6 @@
"transmute between collections of layout-incompatible types"
}
declare_clippy_lint! {
/// **What it does:**
///
/// **Why is this bad?**
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
complexity,
"default lint description"
}
declare_lint_pass!(Transmute => [
CROSSPOINTER_TRANSMUTE,
TRANSMUTE_PTR_TO_REF,
@ -330,26 +332,6 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
let from_ty = cx.typeck_results().expr_ty(&args[0]);
let to_ty = cx.typeck_results().expr_ty(e);
if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) {
span_lint_and_then(
cx,
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
e.span,
&format!(
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
from_ty,
to_ty
),
|diag| {
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
let sugg = format!("{} as {}", arg, to_ty);
diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
}
}
);
return
}
match (&from_ty.kind, &to_ty.kind) {
_ if from_ty == to_ty => span_lint(
cx,
@ -646,6 +628,22 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
);
}
},
(_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then(
cx,
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
e.span,
&format!(
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
from_ty,
to_ty
),
|diag| {
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
}
}
),
_ => {
return;
},

View File

@ -1,7 +1,7 @@
#![allow(dead_code)]
#![allow(clippy::transmutes_expressible_as_ptr_casts)]
extern crate core;
use std::mem::transmute as my_transmute;
use std::vec::Vec as MyVec;

View File

@ -1,5 +1,5 @@
#![warn(clippy::transmute_ptr_to_ptr)]
#![allow(clippy::transmutes_expressible_as_ptr_casts)]
// Make sure we can modify lifetimes, which is one of the recommended uses
// of transmute

View File

@ -1,10 +1,18 @@
#![warn(clippy::transmutes_expressible_as_ptr_casts)]
// These two warnings currrently cover the cases transmutes_expressible_as_ptr_casts
// would otherwise be responsible for
#![warn(clippy::useless_transmute)]
#![warn(clippy::transmute_ptr_to_ptr)]
use std::mem::transmute;
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
// valid, which we quote from below.
use std::mem::transmute;
fn main() {
// We should see an error message for each transmute, and no error messages for
// the casts, since the casts are the recommended fixes.
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
let ptr_i32_transmute = unsafe {
transmute::<isize, *const i32>(-1)

View File

@ -1 +1,50 @@
Should have 7 errors, one for each transmute
error: transmute from an integer to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:18:9
|
LL | transmute::<isize, *const i32>(-1)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-1 as *const i32`
|
= note: `-D clippy::useless-transmute` implied by `-D warnings`
error: transmute from a pointer to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:24:9
|
LL | transmute::<*const i32, *const i8>(ptr_i32)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
|
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
error: transmute from a pointer to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:32:9
|
LL | transmute::<*const [i32], *const [u16]>(slice_ptr)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]`
error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:40:9
|
LL | transmute::<*const i32, usize>(ptr_i32)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
|
= note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings`
error: transmute from a reference to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:48:9
|
LL | transmute::<&[i32; 4], *const [i32; 4]>(array_ref)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`
error: transmute from `fn(usize) -> u8 {main::foo}` to `*const usize` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:56:9
|
LL | transmute::<fn(usize) -> u8, *const usize>(foo)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
error: transmute from `fn(usize) -> u8 {main::foo}` to `usize` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:62:9
|
LL | transmute::<fn(usize) -> u8, usize>(foo)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
error: aborting due to 7 previous errors