-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
New lint: Recommend using ptr::eq
when possible
#4596
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::utils; | ||
use if_chain::if_chain; | ||
use rustc::declare_lint_pass; | ||
use rustc::hir::{BinOpKind, Expr, ExprKind}; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc_errors::Applicability; | ||
use rustc_session::declare_tool_lint; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Use `std::ptr::eq` when applicable | ||
/// | ||
/// **Why is this bad?** `ptr::eq` can be used to compare `&T` references | ||
/// (which coerce to `*const T` implicitly) by their address rather than | ||
/// comparing the values they point to. | ||
/// | ||
/// **Known problems:** None | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// let a = &[1, 2, 3]; | ||
/// let b = &[1, 2, 3]; | ||
/// | ||
/// assert!(a as *const _ as usize == b as *const _ as usize); | ||
/// // Could be written | ||
/// assert!(std::ptr::eq(a, b)); | ||
/// ``` | ||
pub PTR_EQ, | ||
style, | ||
"use `std::ptr::eq` when comparing raw pointers" | ||
} | ||
|
||
declare_lint_pass!(PtrEqLint => [PTR_EQ]); | ||
|
||
static LINT_MSG: &str = "use `std::ptr::eq` when comparing raw pointers"; | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PtrEqLint { | ||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { | ||
if utils::in_macro(expr.span) { | ||
return; | ||
} | ||
|
||
if let ExprKind::Binary(ref op, ref left, ref right) = expr.kind { | ||
if BinOpKind::Eq == op.node { | ||
let (left, right) = match (expr_as_cast_to_usize(cx, left), expr_as_cast_to_usize(cx, right)) { | ||
(Some(lhs), Some(rhs)) => (lhs, rhs), | ||
_ => (&**left, &**right), | ||
}; | ||
|
||
if_chain! { | ||
if let Some(left_var) = expr_as_cast_to_raw_pointer(cx, left); | ||
if let Some(right_var) = expr_as_cast_to_raw_pointer(cx, right); | ||
if let Some(left_snip) = utils::snippet_opt(cx, left_var.span); | ||
if let Some(right_snip) = utils::snippet_opt(cx, right_var.span); | ||
then { | ||
utils::span_lint_and_sugg( | ||
cx, | ||
PTR_EQ, | ||
expr.span, | ||
LINT_MSG, | ||
"try", | ||
format!("std::ptr::eq({}, {})", left_snip, right_snip), | ||
Applicability::MachineApplicable, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How confident are you with the static LINT_MSG: &str = "use `std::ptr::eq` when applicable"; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just added a macro check. But maybe there are more cases that I can't think of. |
||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// If the given expression is a cast to an usize, return the lhs of the cast | ||
// E.g., `foo as *const _ as usize` returns `foo as *const _`. | ||
fn expr_as_cast_to_usize<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cast_expr: &'tcx Expr) -> Option<&'tcx Expr> { | ||
if cx.tables.expr_ty(cast_expr) == cx.tcx.types.usize { | ||
if let ExprKind::Cast(ref expr, _) = cast_expr.kind { | ||
return Some(expr); | ||
} | ||
} | ||
None | ||
} | ||
|
||
// If the given expression is a cast to a `*const` pointer, return the lhs of the cast | ||
// E.g., `foo as *const _` returns `foo`. | ||
fn expr_as_cast_to_raw_pointer<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cast_expr: &'tcx Expr) -> Option<&'tcx Expr> { | ||
if cx.tables.expr_ty(cast_expr).is_unsafe_ptr() { | ||
if let ExprKind::Cast(ref expr, _) = cast_expr.kind { | ||
return Some(expr); | ||
} | ||
} | ||
None | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// run-rustfix | ||
#![warn(clippy::ptr_eq)] | ||
|
||
macro_rules! mac { | ||
($a:expr, $b:expr) => { | ||
$a as *const _ as usize == $b as *const _ as usize | ||
}; | ||
} | ||
|
||
macro_rules! another_mac { | ||
($a:expr, $b:expr) => { | ||
$a as *const _ == $b as *const _ | ||
}; | ||
} | ||
|
||
fn main() { | ||
let a = &[1, 2, 3]; | ||
let b = &[1, 2, 3]; | ||
|
||
let _ = std::ptr::eq(a, b); | ||
let _ = std::ptr::eq(a, b); | ||
flip1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let _ = a.as_ptr() == b as *const _; | ||
let _ = a.as_ptr() == b.as_ptr(); | ||
|
||
// Do not lint | ||
|
||
let _ = mac!(a, b); | ||
let _ = another_mac!(a, b); | ||
|
||
let a = &mut [1, 2, 3]; | ||
let b = &mut [1, 2, 3]; | ||
|
||
let _ = a.as_mut_ptr() == b as *mut [i32] as *mut _; | ||
let _ = a.as_mut_ptr() == b.as_mut_ptr(); | ||
|
||
let _ = a == b; | ||
let _ = core::ptr::eq(a, b); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// run-rustfix | ||
#![warn(clippy::ptr_eq)] | ||
|
||
macro_rules! mac { | ||
($a:expr, $b:expr) => { | ||
$a as *const _ as usize == $b as *const _ as usize | ||
}; | ||
} | ||
|
||
macro_rules! another_mac { | ||
($a:expr, $b:expr) => { | ||
$a as *const _ == $b as *const _ | ||
}; | ||
} | ||
|
||
fn main() { | ||
let a = &[1, 2, 3]; | ||
let b = &[1, 2, 3]; | ||
|
||
let _ = a as *const _ as usize == b as *const _ as usize; | ||
let _ = a as *const _ == b as *const _; | ||
let _ = a.as_ptr() == b as *const _; | ||
let _ = a.as_ptr() == b.as_ptr(); | ||
|
||
// Do not lint | ||
|
||
let _ = mac!(a, b); | ||
let _ = another_mac!(a, b); | ||
|
||
let a = &mut [1, 2, 3]; | ||
let b = &mut [1, 2, 3]; | ||
|
||
let _ = a.as_mut_ptr() == b as *mut [i32] as *mut _; | ||
let _ = a.as_mut_ptr() == b.as_mut_ptr(); | ||
|
||
let _ = a == b; | ||
let _ = core::ptr::eq(a, b); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error: use `std::ptr::eq` when comparing raw pointers | ||
--> $DIR/ptr_eq.rs:20:13 | ||
| | ||
LL | let _ = a as *const _ as usize == b as *const _ as usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(a, b)` | ||
| | ||
= note: `-D clippy::ptr-eq` implied by `-D warnings` | ||
|
||
error: use `std::ptr::eq` when comparing raw pointers | ||
--> $DIR/ptr_eq.rs:21:13 | ||
| | ||
LL | let _ = a as *const _ == b as *const _; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(a, b)` | ||
|
||
error: aborting due to 2 previous errors | ||
|
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.
Is there another benefit in using
ptr::eq
, apart from being shorter?