-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint swap_ptr_to_ref
#8916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New lint swap_ptr_to_ref
#8916
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::source::snippet_with_context; | ||
use clippy_utils::{match_def_path, path_def_id, paths}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, UnOp}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::{Span, SyntaxContext}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for calls to `core::mem::swap` where either parameter is derived from a pointer | ||
/// | ||
/// ### Why is this bad? | ||
/// When at least one parameter to `swap` is derived from a pointer it may overlap with the | ||
/// other. This would then lead to undefined behavior. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// unsafe fn swap(x: &[*mut u32], y: &[*mut u32]) { | ||
/// for (&x, &y) in x.iter().zip(y) { | ||
/// core::mem::swap(&mut *x, &mut *y); | ||
/// } | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// unsafe fn swap(x: &[*mut u32], y: &[*mut u32]) { | ||
/// for (&x, &y) in x.iter().zip(y) { | ||
/// core::ptr::swap(x, y); | ||
/// } | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.63.0"] | ||
pub SWAP_PTR_TO_REF, | ||
suspicious, | ||
"call to `mem::swap` using pointer derived references" | ||
} | ||
declare_lint_pass!(SwapPtrToRef => [SWAP_PTR_TO_REF]); | ||
|
||
impl LateLintPass<'_> for SwapPtrToRef { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, e: &Expr<'_>) { | ||
if let ExprKind::Call(fn_expr, [arg1, arg2]) = e.kind | ||
&& let Some(fn_id) = path_def_id(cx, fn_expr) | ||
&& match_def_path(cx, fn_id, &paths::MEM_SWAP) | ||
&& let ctxt = e.span.ctxt() | ||
&& let (from_ptr1, arg1_span) = is_ptr_to_ref(cx, arg1, ctxt) | ||
&& let (from_ptr2, arg2_span) = is_ptr_to_ref(cx, arg2, ctxt) | ||
&& (from_ptr1 || from_ptr2) | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
SWAP_PTR_TO_REF, | ||
e.span, | ||
"call to `core::mem::swap` with a parameter derived from a raw pointer", | ||
|diag| { | ||
if !((from_ptr1 && arg1_span.is_none()) || (from_ptr2 && arg2_span.is_none())) { | ||
let mut app = Applicability::MachineApplicable; | ||
let snip1 = snippet_with_context(cx, arg1_span.unwrap_or(arg1.span), ctxt, "..", &mut app).0; | ||
let snip2 = snippet_with_context(cx, arg2_span.unwrap_or(arg2.span), ctxt, "..", &mut app).0; | ||
diag.span_suggestion(e.span, "use ptr::swap", format!("core::ptr::swap({}, {})", snip1, snip2), app); | ||
} | ||
} | ||
); | ||
} | ||
} | ||
} | ||
|
||
/// Checks if the expression converts a mutable pointer to a mutable reference. If it is, also | ||
/// returns the span of the pointer expression if it's suitable for making a suggestion. | ||
fn is_ptr_to_ref(cx: &LateContext<'_>, e: &Expr<'_>, ctxt: SyntaxContext) -> (bool, Option<Span>) { | ||
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, borrowed_expr) = e.kind | ||
&& let ExprKind::Unary(UnOp::Deref, derefed_expr) = borrowed_expr.kind | ||
&& cx.typeck_results().expr_ty(derefed_expr).is_unsafe_ptr() | ||
{ | ||
(true, (borrowed_expr.span.ctxt() == ctxt || derefed_expr.span.ctxt() == ctxt).then(|| derefed_expr.span)) | ||
} else { | ||
(false, None) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,24 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::swap_ptr_to_ref)] | ||
|
||
use core::ptr::addr_of_mut; | ||
|
||
fn main() { | ||
let mut x = 0u32; | ||
let y: *mut _ = &mut x; | ||
let z: *mut _ = &mut x; | ||
|
||
unsafe { | ||
core::ptr::swap(y, z); | ||
core::ptr::swap(y, &mut x); | ||
core::ptr::swap(&mut x, y); | ||
core::ptr::swap(addr_of_mut!(x), addr_of_mut!(x)); | ||
} | ||
|
||
let y = &mut x; | ||
let mut z = 0u32; | ||
let z = &mut z; | ||
|
||
core::mem::swap(y, z); | ||
} |
This file contains hidden or 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,24 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::swap_ptr_to_ref)] | ||
|
||
use core::ptr::addr_of_mut; | ||
|
||
fn main() { | ||
let mut x = 0u32; | ||
let y: *mut _ = &mut x; | ||
let z: *mut _ = &mut x; | ||
|
||
unsafe { | ||
core::mem::swap(&mut *y, &mut *z); | ||
core::mem::swap(&mut *y, &mut x); | ||
core::mem::swap(&mut x, &mut *y); | ||
core::mem::swap(&mut *addr_of_mut!(x), &mut *addr_of_mut!(x)); | ||
} | ||
|
||
let y = &mut x; | ||
let mut z = 0u32; | ||
let z = &mut z; | ||
|
||
core::mem::swap(y, z); | ||
} |
This file contains hidden or 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,28 @@ | ||
error: call to `core::mem::swap` with a parameter derived from a raw pointer | ||
--> $DIR/swap_ptr_to_ref.rs:13:9 | ||
| | ||
LL | core::mem::swap(&mut *y, &mut *z); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use ptr::swap: `core::ptr::swap(y, z)` | ||
| | ||
= note: `-D clippy::swap-ptr-to-ref` implied by `-D warnings` | ||
|
||
error: call to `core::mem::swap` with a parameter derived from a raw pointer | ||
--> $DIR/swap_ptr_to_ref.rs:14:9 | ||
| | ||
LL | core::mem::swap(&mut *y, &mut x); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use ptr::swap: `core::ptr::swap(y, &mut x)` | ||
|
||
error: call to `core::mem::swap` with a parameter derived from a raw pointer | ||
--> $DIR/swap_ptr_to_ref.rs:15:9 | ||
| | ||
LL | core::mem::swap(&mut x, &mut *y); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use ptr::swap: `core::ptr::swap(&mut x, y)` | ||
|
||
error: call to `core::mem::swap` with a parameter derived from a raw pointer | ||
--> $DIR/swap_ptr_to_ref.rs:16:9 | ||
| | ||
LL | core::mem::swap(&mut *addr_of_mut!(x), &mut *addr_of_mut!(x)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use ptr::swap: `core::ptr::swap(addr_of_mut!(x), addr_of_mut!(x))` | ||
|
||
error: aborting due to 4 previous errors | ||
|
This file contains hidden or 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,18 @@ | ||
#![warn(clippy::swap_ptr_to_ref)] | ||
|
||
macro_rules! addr_of_mut_to_ref { | ||
($e:expr) => { | ||
&mut *core::ptr::addr_of_mut!($e) | ||
}; | ||
} | ||
|
||
fn main() { | ||
let mut x = 0u32; | ||
let y: *mut _ = &mut x; | ||
|
||
unsafe { | ||
core::mem::swap(addr_of_mut_to_ref!(x), &mut *y); | ||
core::mem::swap(&mut *y, addr_of_mut_to_ref!(x)); | ||
core::mem::swap(addr_of_mut_to_ref!(x), addr_of_mut_to_ref!(x)); | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
error: call to `core::mem::swap` with a parameter derived from a raw pointer | ||
--> $DIR/swap_ptr_to_ref_unfixable.rs:14:9 | ||
| | ||
LL | core::mem::swap(addr_of_mut_to_ref!(x), &mut *y); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::swap-ptr-to-ref` implied by `-D warnings` | ||
|
||
error: call to `core::mem::swap` with a parameter derived from a raw pointer | ||
--> $DIR/swap_ptr_to_ref_unfixable.rs:15:9 | ||
| | ||
LL | core::mem::swap(&mut *y, addr_of_mut_to_ref!(x)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: call to `core::mem::swap` with a parameter derived from a raw pointer | ||
--> $DIR/swap_ptr_to_ref_unfixable.rs:16:9 | ||
| | ||
LL | core::mem::swap(addr_of_mut_to_ref!(x), addr_of_mut_to_ref!(x)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: documentation