-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes: #6995 Signed-off-by: Federico Guerinoni <guerinoni.federico@gmail.com> Co-authored-by: Paolo Barbolini <paolo@paolo565.org>
- Loading branch information
1 parent
fccf07b
commit 3298de7
Showing
35 changed files
with
294 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.