Add borrow_as_ptr lint

Closes: #6995

Signed-off-by: Federico Guerinoni <guerinoni.federico@gmail.com>
Co-authored-by: Paolo Barbolini <paolo@paolo565.org>
This commit is contained in:
Federico Guerinoni 2022-01-02 14:26:44 +01:00 committed by Federico Guerinoni
parent fccf07bae5
commit 3298de7f66
35 changed files with 294 additions and 75 deletions

View File

@ -2887,6 +2887,7 @@ Released 2018-09-13
[`blocks_in_if_conditions`]: https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_if_conditions
[`bool_assert_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison
[`bool_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
[`borrow_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr
[`borrow_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const
[`borrowed_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box
[`box_collection`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_collection

View File

@ -5,7 +5,7 @@
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
[There are over 450 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are over 500 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
Lints are divided into categories, each with a default [lint level](https://doc.rust-lang.org/rustc/lints/levels.html).
You can choose how much Clippy is supposed to ~~annoy~~ help you by changing the lint level by category.

View File

@ -0,0 +1,97 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_no_std_crate;
use clippy_utils::source::snippet_opt;
use clippy_utils::{meets_msrv, msrvs};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass};
declare_clippy_lint! {
/// ### What it does
/// Checks for the usage of `&expr as *const T` or
/// `&mut expr as *mut T`, and suggest using `ptr::addr_of` or
/// `ptr::addr_of_mut` instead.
///
/// ### Why is this bad?
/// This would improve readability and avoid creating a reference
/// that points to an uninitialized value or unaligned place.
/// Read the `ptr::addr_of` docs for more information.
///
/// ### Example
/// ```rust
/// let val = 1;
/// let p = &val as *const i32;
///
/// let mut val_mut = 1;
/// let p_mut = &mut val_mut as *mut i32;
/// ```
/// Use instead:
/// ```rust
/// let val = 1;
/// let p = std::ptr::addr_of!(val);
///
/// let mut val_mut = 1;
/// let p_mut = std::ptr::addr_of_mut!(val_mut);
/// ```
#[clippy::version = "1.60.0"]
pub BORROW_AS_PTR,
pedantic,
"borrowing just to cast to a raw pointer"
}
impl_lint_pass!(BorrowAsPtr => [BORROW_AS_PTR]);
pub struct BorrowAsPtr {
msrv: Option<RustcVersion>,
}
impl BorrowAsPtr {
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {
Self { msrv }
}
}
impl<'tcx> LateLintPass<'tcx> for BorrowAsPtr {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if !meets_msrv(self.msrv.as_ref(), &msrvs::BORROW_AS_PTR) {
return;
}
if expr.span.from_expansion() {
return;
}
if_chain! {
if let ExprKind::Cast(left_expr, ty) = &expr.kind;
if let TyKind::Ptr(_) = ty.kind;
if let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = &left_expr.kind;
then {
let core_or_std = if is_no_std_crate(cx) { "core" } else { "std" };
let macro_name = match mutability {
Mutability::Not => "addr_of",
Mutability::Mut => "addr_of_mut",
};
span_lint_and_sugg(
cx,
BORROW_AS_PTR,
expr.span,
"borrow as raw pointer",
"try",
format!(
"{}::ptr::{}!({})",
core_or_std,
macro_name,
snippet_opt(cx, e.span).unwrap()
),
Applicability::MachineApplicable,
);
}
}
}
}

View File

@ -59,6 +59,7 @@ store.register_lints(&[
bool_assert_comparison::BOOL_ASSERT_COMPARISON,
booleans::LOGIC_BUG,
booleans::NONMINIMAL_BOOL,
borrow_as_ptr::BORROW_AS_PTR,
bytecount::NAIVE_BYTECOUNT,
cargo_common_metadata::CARGO_COMMON_METADATA,
case_sensitive_file_extension_comparisons::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,

View File

@ -7,6 +7,7 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
LintId::of(await_holding_invalid::AWAIT_HOLDING_LOCK),
LintId::of(await_holding_invalid::AWAIT_HOLDING_REFCELL_REF),
LintId::of(bit_mask::VERBOSE_BIT_MASK),
LintId::of(borrow_as_ptr::BORROW_AS_PTR),
LintId::of(bytecount::NAIVE_BYTECOUNT),
LintId::of(case_sensitive_file_extension_comparisons::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS),
LintId::of(casts::CAST_LOSSLESS),

View File

@ -174,6 +174,7 @@ mod blacklisted_name;
mod blocks_in_if_conditions;
mod bool_assert_comparison;
mod booleans;
mod borrow_as_ptr;
mod bytecount;
mod cargo_common_metadata;
mod case_sensitive_file_extension_comparisons;
@ -857,6 +858,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| Box::new(return_self_not_must_use::ReturnSelfNotMustUse));
store.register_late_pass(|| Box::new(init_numbered_fields::NumberedFields));
store.register_early_pass(|| Box::new(single_char_lifetime_names::SingleCharLifetimeNames));
store.register_late_pass(move || Box::new(borrow_as_ptr::BorrowAsPtr::new(msrv)));
// add lints here, do not remove this comment, it's used in `new_lint`
}

View File

@ -14,6 +14,7 @@ macro_rules! msrv_aliases {
msrv_aliases! {
1,53,0 { OR_PATTERNS }
1,52,0 { STR_SPLIT_ONCE }
1,51,0 { BORROW_AS_PTR }
1,50,0 { BOOL_THEN }
1,47,0 { TAU }
1,46,0 { CONST_IF_MATCH }

View File

@ -1,6 +1,7 @@
// aux-build:macro_rules.rs
#![warn(clippy::as_conversions)]
#![allow(clippy::borrow_as_ptr)]
#[macro_use]
extern crate macro_rules;

View File

@ -1,5 +1,5 @@
error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:14:13
--> $DIR/as_conversions.rs:15:13
|
LL | let i = 0u32 as u64;
| ^^^^^^^^^^^
@ -8,7 +8,7 @@ LL | let i = 0u32 as u64;
= help: consider using a safe wrapper for this conversion
error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:16:13
--> $DIR/as_conversions.rs:17:13
|
LL | let j = &i as *const u64 as *mut u64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -16,7 +16,7 @@ LL | let j = &i as *const u64 as *mut u64;
= help: consider using a safe wrapper for this conversion
error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:16:13
--> $DIR/as_conversions.rs:17:13
|
LL | let j = &i as *const u64 as *mut u64;
| ^^^^^^^^^^^^^^^^

View File

@ -0,0 +1,10 @@
// run-rustfix
#![warn(clippy::borrow_as_ptr)]
fn main() {
let val = 1;
let _p = std::ptr::addr_of!(val);
let mut val_mut = 1;
let _p_mut = std::ptr::addr_of_mut!(val_mut);
}

10
tests/ui/borrow_as_ptr.rs Normal file
View File

@ -0,0 +1,10 @@
// run-rustfix
#![warn(clippy::borrow_as_ptr)]
fn main() {
let val = 1;
let _p = &val as *const i32;
let mut val_mut = 1;
let _p_mut = &mut val_mut as *mut i32;
}

View File

@ -0,0 +1,16 @@
error: borrow as raw pointer
--> $DIR/borrow_as_ptr.rs:6:14
|
LL | let _p = &val as *const i32;
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of!(val)`
|
= note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
error: borrow as raw pointer
--> $DIR/borrow_as_ptr.rs:9:18
|
LL | let _p_mut = &mut val_mut as *mut i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(val_mut)`
error: aborting due to 2 previous errors

View File

@ -0,0 +1,22 @@
// run-rustfix
#![warn(clippy::borrow_as_ptr)]
#![feature(lang_items, start, libc)]
#![no_std]
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let val = 1;
let _p = core::ptr::addr_of!(val);
let mut val_mut = 1;
let _p_mut = core::ptr::addr_of_mut!(val_mut);
0
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

View File

@ -0,0 +1,22 @@
// run-rustfix
#![warn(clippy::borrow_as_ptr)]
#![feature(lang_items, start, libc)]
#![no_std]
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let val = 1;
let _p = &val as *const i32;
let mut val_mut = 1;
let _p_mut = &mut val_mut as *mut i32;
0
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

View File

@ -0,0 +1,16 @@
error: borrow as raw pointer
--> $DIR/borrow_as_ptr_no_std.rs:9:14
|
LL | let _p = &val as *const i32;
| ^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of!(val)`
|
= note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
error: borrow as raw pointer
--> $DIR/borrow_as_ptr_no_std.rs:12:18
|
LL | let _p_mut = &mut val_mut as *mut i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of_mut!(val_mut)`
error: aborting due to 2 previous errors

View File

@ -4,7 +4,13 @@
extern crate libc;
#[warn(clippy::cast_ptr_alignment)]
#[allow(clippy::no_effect, clippy::unnecessary_operation, clippy::cast_lossless)]
#[allow(
clippy::no_effect,
clippy::unnecessary_operation,
clippy::cast_lossless,
clippy::borrow_as_ptr
)]
fn main() {
/* These should be warned against */

View File

@ -1,5 +1,5 @@
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
--> $DIR/cast_alignment.rs:12:5
--> $DIR/cast_alignment.rs:18:5
|
LL | (&1u8 as *const u8) as *const u16;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -7,19 +7,19 @@ LL | (&1u8 as *const u8) as *const u16;
= note: `-D clippy::cast-ptr-alignment` implied by `-D warnings`
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
--> $DIR/cast_alignment.rs:13:5
--> $DIR/cast_alignment.rs:19:5
|
LL | (&mut 1u8 as *mut u8) as *mut u16;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
--> $DIR/cast_alignment.rs:16:5
--> $DIR/cast_alignment.rs:22:5
|
LL | (&1u8 as *const u8).cast::<u16>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
--> $DIR/cast_alignment.rs:17:5
--> $DIR/cast_alignment.rs:23:5
|
LL | (&mut 1u8 as *mut u8).cast::<u16>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -1,5 +1,5 @@
#![warn(clippy::cast_ref_to_mut)]
#![allow(clippy::no_effect)]
#![allow(clippy::no_effect, clippy::borrow_as_ptr)]
extern "C" {
// N.B., mutability can be easily incorrect in FFI calls -- as

View File

@ -1,6 +1,7 @@
#![warn(clippy::all)]
#![warn(clippy::mutex_integer)]
#![warn(clippy::mutex_atomic)]
#![allow(clippy::borrow_as_ptr)]
fn main() {
use std::sync::Mutex;

View File

@ -1,5 +1,5 @@
error: consider using an `AtomicBool` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> $DIR/mutex_atomic.rs:7:5
--> $DIR/mutex_atomic.rs:8:5
|
LL | Mutex::new(true);
| ^^^^^^^^^^^^^^^^
@ -7,31 +7,31 @@ LL | Mutex::new(true);
= note: `-D clippy::mutex-atomic` implied by `-D warnings`
error: consider using an `AtomicUsize` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> $DIR/mutex_atomic.rs:8:5
--> $DIR/mutex_atomic.rs:9:5
|
LL | Mutex::new(5usize);
| ^^^^^^^^^^^^^^^^^^
error: consider using an `AtomicIsize` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> $DIR/mutex_atomic.rs:9:5
--> $DIR/mutex_atomic.rs:10:5
|
LL | Mutex::new(9isize);
| ^^^^^^^^^^^^^^^^^^
error: consider using an `AtomicPtr` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> $DIR/mutex_atomic.rs:11:5
--> $DIR/mutex_atomic.rs:12:5
|
LL | Mutex::new(&x as *const u32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: consider using an `AtomicPtr` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> $DIR/mutex_atomic.rs:12:5
--> $DIR/mutex_atomic.rs:13:5
|
LL | Mutex::new(&mut x as *mut u32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: consider using an `AtomicUsize` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> $DIR/mutex_atomic.rs:13:5
--> $DIR/mutex_atomic.rs:14:5
|
LL | Mutex::new(0u32);
| ^^^^^^^^^^^^^^^^
@ -39,7 +39,7 @@ LL | Mutex::new(0u32);
= note: `-D clippy::mutex-integer` implied by `-D warnings`
error: consider using an `AtomicIsize` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> $DIR/mutex_atomic.rs:14:5
--> $DIR/mutex_atomic.rs:15:5
|
LL | Mutex::new(0i32);
| ^^^^^^^^^^^^^^^^

View File

@ -2,7 +2,7 @@
#![warn(clippy::or_fun_call)]
#![allow(dead_code)]
#![allow(clippy::unnecessary_wraps)]
#![allow(clippy::unnecessary_wraps, clippy::borrow_as_ptr)]
use std::collections::BTreeMap;
use std::collections::HashMap;

View File

@ -2,7 +2,7 @@
#![warn(clippy::or_fun_call)]
#![allow(dead_code)]
#![allow(clippy::unnecessary_wraps)]
#![allow(clippy::unnecessary_wraps, clippy::borrow_as_ptr)]
use std::collections::BTreeMap;
use std::collections::HashMap;

View File

@ -1,4 +1,4 @@
#![allow(dead_code)]
#![allow(dead_code, clippy::borrow_as_ptr)]
extern crate core;

View File

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

View File

@ -1,5 +1,5 @@
error: transmute from a pointer to a pointer
--> $DIR/transmute_ptr_to_ptr.rs:29:29
--> $DIR/transmute_ptr_to_ptr.rs:30:29
|
LL | let _: *const f32 = std::mem::transmute(ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr as *const f32`
@ -7,31 +7,31 @@ LL | let _: *const f32 = std::mem::transmute(ptr);
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
error: transmute from a pointer to a pointer
--> $DIR/transmute_ptr_to_ptr.rs:30:27
--> $DIR/transmute_ptr_to_ptr.rs:31:27
|
LL | let _: *mut f32 = std::mem::transmute(mut_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `mut_ptr as *mut f32`
error: transmute from a reference to a reference
--> $DIR/transmute_ptr_to_ptr.rs:32:23
--> $DIR/transmute_ptr_to_ptr.rs:33:23
|
LL | let _: &f32 = std::mem::transmute(&1u32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)`
error: transmute from a reference to a reference
--> $DIR/transmute_ptr_to_ptr.rs:33:23
--> $DIR/transmute_ptr_to_ptr.rs:34:23
|
LL | let _: &f64 = std::mem::transmute(&1f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1f32 as *const f32 as *const f64)`
error: transmute from a reference to a reference
--> $DIR/transmute_ptr_to_ptr.rs:36:27
--> $DIR/transmute_ptr_to_ptr.rs:37:27
|
LL | let _: &mut f32 = std::mem::transmute(&mut 1u32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)`
error: transmute from a reference to a reference
--> $DIR/transmute_ptr_to_ptr.rs:37:37
--> $DIR/transmute_ptr_to_ptr.rs:38:37
|
LL | let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`

View File

@ -4,8 +4,7 @@
// would otherwise be responsible for
#![warn(clippy::useless_transmute)]
#![warn(clippy::transmute_ptr_to_ptr)]
#![allow(unused_unsafe)]
#![allow(dead_code)]
#![allow(dead_code, unused_unsafe, clippy::borrow_as_ptr)]
use std::mem::{size_of, transmute};

View File

@ -4,8 +4,7 @@
// would otherwise be responsible for
#![warn(clippy::useless_transmute)]
#![warn(clippy::transmute_ptr_to_ptr)]
#![allow(unused_unsafe)]
#![allow(dead_code)]
#![allow(dead_code, unused_unsafe, clippy::borrow_as_ptr)]
use std::mem::{size_of, transmute};

View File

@ -1,5 +1,5 @@
error: transmute from an integer to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:19:39
--> $DIR/transmutes_expressible_as_ptr_casts.rs:18:39
|
LL | let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `usize::MAX as *const i32`
@ -7,7 +7,7 @@ LL | let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize:
= note: `-D clippy::useless-transmute` implied by `-D warnings`
error: transmute from a pointer to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:23:38
--> $DIR/transmutes_expressible_as_ptr_casts.rs:22:38
|
LL | let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
@ -15,13 +15,13 @@ LL | let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr
= 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:29:46
--> $DIR/transmutes_expressible_as_ptr_casts.rs:28:46
|
LL | let _ptr_to_unsized_transmute = unsafe { 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:35:50
--> $DIR/transmutes_expressible_as_ptr_casts.rs:34:50
|
LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
@ -29,25 +29,25 @@ LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, us
= 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:41:41
--> $DIR/transmutes_expressible_as_ptr_casts.rs:40:41
|
LL | let _array_ptr_transmute = unsafe { 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:49:41
--> $DIR/transmutes_expressible_as_ptr_casts.rs:48:41
|
LL | let _usize_ptr_transmute = unsafe { 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:53:49
--> $DIR/transmutes_expressible_as_ptr_casts.rs:52:49
|
LL | let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
error: transmute from a reference to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:65:14
--> $DIR/transmutes_expressible_as_ptr_casts.rs:64:14
|
LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8`

View File

@ -1,7 +1,12 @@
// run-rustfix
#![warn(clippy::unnecessary_cast)]
#![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::nonstandard_macro_braces)]
#![allow(
clippy::no_effect,
clippy::unnecessary_operation,
clippy::nonstandard_macro_braces,
clippy::borrow_as_ptr
)]
fn main() {
// casting integer literal to float is unnecessary

View File

@ -1,7 +1,12 @@
// run-rustfix
#![warn(clippy::unnecessary_cast)]
#![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::nonstandard_macro_braces)]
#![allow(
clippy::no_effect,
clippy::unnecessary_operation,
clippy::nonstandard_macro_braces,
clippy::borrow_as_ptr
)]
fn main() {
// casting integer literal to float is unnecessary

View File

@ -1,5 +1,5 @@
error: casting integer literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:8:5
--> $DIR/unnecessary_cast_fixable.rs:13:5
|
LL | 100 as f32;
| ^^^^^^^^^^ help: try: `100_f32`
@ -7,97 +7,97 @@ LL | 100 as f32;
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:9:5
--> $DIR/unnecessary_cast_fixable.rs:14:5
|
LL | 100 as f64;
| ^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:10:5
--> $DIR/unnecessary_cast_fixable.rs:15:5
|
LL | 100_i32 as f64;
| ^^^^^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:11:13
--> $DIR/unnecessary_cast_fixable.rs:16:13
|
LL | let _ = -100 as f32;
| ^^^^^^^^^^^ help: try: `-100_f32`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:12:13
--> $DIR/unnecessary_cast_fixable.rs:17:13
|
LL | let _ = -100 as f64;
| ^^^^^^^^^^^ help: try: `-100_f64`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:13:13
--> $DIR/unnecessary_cast_fixable.rs:18:13
|
LL | let _ = -100_i32 as f64;
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:14:5
--> $DIR/unnecessary_cast_fixable.rs:19:5
|
LL | 100. as f32;
| ^^^^^^^^^^^ help: try: `100_f32`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:15:5
--> $DIR/unnecessary_cast_fixable.rs:20:5
|
LL | 100. as f64;
| ^^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:27:5
--> $DIR/unnecessary_cast_fixable.rs:32:5
|
LL | 1 as u32;
| ^^^^^^^^ help: try: `1_u32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:28:5
--> $DIR/unnecessary_cast_fixable.rs:33:5
|
LL | 0x10 as i32;
| ^^^^^^^^^^^ help: try: `0x10_i32`
error: casting integer literal to `usize` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:29:5
--> $DIR/unnecessary_cast_fixable.rs:34:5
|
LL | 0b10 as usize;
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
error: casting integer literal to `u16` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:30:5
--> $DIR/unnecessary_cast_fixable.rs:35:5
|
LL | 0o73 as u16;
| ^^^^^^^^^^^ help: try: `0o73_u16`
error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:31:5
--> $DIR/unnecessary_cast_fixable.rs:36:5
|
LL | 1_000_000_000 as u32;
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:33:5
--> $DIR/unnecessary_cast_fixable.rs:38:5
|
LL | 1.0 as f64;
| ^^^^^^^^^^ help: try: `1.0_f64`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:34:5
--> $DIR/unnecessary_cast_fixable.rs:39:5
|
LL | 0.5 as f32;
| ^^^^^^^^^^ help: try: `0.5_f32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:38:13
--> $DIR/unnecessary_cast_fixable.rs:43:13
|
LL | let _ = -1 as i32;
| ^^^^^^^^^ help: try: `-1_i32`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:39:13
--> $DIR/unnecessary_cast_fixable.rs:44:13
|
LL | let _ = -1.0 as f32;
| ^^^^^^^^^^^ help: try: `-1.0_f32`

View File

@ -4,6 +4,8 @@ use std::rc::Rc;
use std::sync::Arc;
#[warn(clippy::vtable_address_comparisons)]
#[allow(clippy::borrow_as_ptr)]
fn main() {
let a: *const dyn Debug = &1 as &dyn Debug;
let b: *const dyn Debug = &1 as &dyn Debug;

View File

@ -1,5 +1,5 @@
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:12:13
--> $DIR/vtable_address_comparisons.rs:14:13
|
LL | let _ = a == b;
| ^^^^^^
@ -8,7 +8,7 @@ LL | let _ = a == b;
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:13:13
--> $DIR/vtable_address_comparisons.rs:15:13
|
LL | let _ = a != b;
| ^^^^^^
@ -16,7 +16,7 @@ LL | let _ = a != b;
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:14:13
--> $DIR/vtable_address_comparisons.rs:16:13
|
LL | let _ = a < b;
| ^^^^^
@ -24,7 +24,7 @@ LL | let _ = a < b;
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:15:13
--> $DIR/vtable_address_comparisons.rs:17:13
|
LL | let _ = a <= b;
| ^^^^^^
@ -32,7 +32,7 @@ LL | let _ = a <= b;
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:16:13
--> $DIR/vtable_address_comparisons.rs:18:13
|
LL | let _ = a > b;
| ^^^^^
@ -40,7 +40,7 @@ LL | let _ = a > b;
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:17:13
--> $DIR/vtable_address_comparisons.rs:19:13
|
LL | let _ = a >= b;
| ^^^^^^
@ -48,7 +48,7 @@ LL | let _ = a >= b;
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:18:5
--> $DIR/vtable_address_comparisons.rs:20:5
|
LL | ptr::eq(a, b);
| ^^^^^^^^^^^^^
@ -56,7 +56,7 @@ LL | ptr::eq(a, b);
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:22:5
--> $DIR/vtable_address_comparisons.rs:24:5
|
LL | ptr::eq(a, b);
| ^^^^^^^^^^^^^
@ -64,7 +64,7 @@ LL | ptr::eq(a, b);
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:25:5
--> $DIR/vtable_address_comparisons.rs:27:5
|
LL | Rc::ptr_eq(&a, &a);
| ^^^^^^^^^^^^^^^^^^
@ -72,7 +72,7 @@ LL | Rc::ptr_eq(&a, &a);
= help: consider extracting and comparing data pointers only
error: comparing trait object pointers compares a non-unique vtable address
--> $DIR/vtable_address_comparisons.rs:28:5
--> $DIR/vtable_address_comparisons.rs:30:5
|
LL | Arc::ptr_eq(&a, &a);
| ^^^^^^^^^^^^^^^^^^^

View File

@ -1,3 +1,4 @@
#[allow(clippy::borrow_as_ptr)]
fn main() {
unsafe {
let m = &mut () as *mut ();

View File

@ -1,5 +1,5 @@
error: offset calculation on zero-sized value
--> $DIR/zero_offset.rs:4:9
--> $DIR/zero_offset.rs:5:9
|
LL | m.offset(0);
| ^^^^^^^^^^^
@ -7,43 +7,43 @@ LL | m.offset(0);
= note: `#[deny(clippy::zst_offset)]` on by default
error: offset calculation on zero-sized value
--> $DIR/zero_offset.rs:5:9
--> $DIR/zero_offset.rs:6:9
|
LL | m.wrapping_add(0);
| ^^^^^^^^^^^^^^^^^
error: offset calculation on zero-sized value
--> $DIR/zero_offset.rs:6:9
--> $DIR/zero_offset.rs:7:9
|
LL | m.sub(0);
| ^^^^^^^^
error: offset calculation on zero-sized value
--> $DIR/zero_offset.rs:7:9
--> $DIR/zero_offset.rs:8:9
|
LL | m.wrapping_sub(0);
| ^^^^^^^^^^^^^^^^^
error: offset calculation on zero-sized value
--> $DIR/zero_offset.rs:10:9
--> $DIR/zero_offset.rs:11:9
|
LL | c.offset(0);
| ^^^^^^^^^^^
error: offset calculation on zero-sized value
--> $DIR/zero_offset.rs:11:9
--> $DIR/zero_offset.rs:12:9
|
LL | c.wrapping_add(0);
| ^^^^^^^^^^^^^^^^^
error: offset calculation on zero-sized value
--> $DIR/zero_offset.rs:12:9
--> $DIR/zero_offset.rs:13:9
|
LL | c.sub(0);
| ^^^^^^^^
error: offset calculation on zero-sized value
--> $DIR/zero_offset.rs:13:9
--> $DIR/zero_offset.rs:14:9
|
LL | c.wrapping_sub(0);
| ^^^^^^^^^^^^^^^^^