-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New lint: Recommend using
ptr::eq
when possible
- Loading branch information
Showing
8 changed files
with
167 additions
and
2 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
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,91 @@ | ||
use crate::utils; | ||
use if_chain::if_chain; | ||
use rustc::hir::{BinOpKind, Expr, ExprKind}; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_errors::Applicability; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Use `std::ptr::eq` when applicable | ||
/// | ||
/// **Why is this bad?** This 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]; | ||
/// | ||
/// let _ = a as *const _ as usize == b as *const _ as usize; | ||
/// // Could be written | ||
/// let _ = std::ptr::eq(a, b); | ||
/// ``` | ||
pub PTR_EQ, | ||
style, | ||
"use `std::ptr::eq` when applicable" | ||
} | ||
|
||
declare_lint_pass!(PtrEqLint => [PTR_EQ]); | ||
|
||
static LINT_MSG: &str = "use `std::ptr::eq` when applicable"; | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PtrEqLint { | ||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { | ||
if let ExprKind::Binary(ref op, ref left, ref right) = expr.kind { | ||
if BinOpKind::Eq == op.node { | ||
let (left, right) = if_chain! { | ||
if let Some(lhs) = expr_as_cast_to_usize(cx, left); | ||
if let Some(rhs) = expr_as_cast_to_usize(cx, right); | ||
then { | ||
(lhs, rhs) | ||
} | ||
else { | ||
(&**left, &**right) | ||
} | ||
}; | ||
|
||
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) { | ||
let left_snip = utils::snippet(cx, left_var.span, ".."); | ||
let right_snip = utils::snippet(cx, right_var.span, ".."); | ||
let sugg = format!("std::ptr::eq({}, {})", left_snip, right_snip); | ||
utils::span_lint_and_sugg( | ||
cx, | ||
PTR_EQ, | ||
expr.span, | ||
LINT_MSG, | ||
"try", | ||
sugg, | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 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 | ||
} |
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,23 @@ | ||
// run-rustfix | ||
#![warn(clippy::ptr_eq)] | ||
|
||
fn main() { | ||
let a = &[1, 2, 3]; | ||
let b = &[1, 2, 3]; | ||
|
||
let _ = std::ptr::eq(a, b); | ||
let _ = std::ptr::eq(a, b); | ||
let _ = a.as_ptr() == b as *const _; | ||
let _ = a.as_ptr() == b.as_ptr(); | ||
|
||
// Do not lint | ||
|
||
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); | ||
} |
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,23 @@ | ||
// run-rustfix | ||
#![warn(clippy::ptr_eq)] | ||
|
||
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 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); | ||
} |
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: use `std::ptr::eq` when applicable | ||
--> $DIR/ptr_eq.rs:8: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 applicable | ||
--> $DIR/ptr_eq.rs:9:13 | ||
| | ||
LL | let _ = a as *const _ == b as *const _; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(a, b)` | ||
|
||
error: aborting due to 2 previous errors | ||
|