Skip to content

Commit 4462053

Browse files
committed
New lint: Recommend using ptr::eq when possible
1 parent 5f058d8 commit 4462053

File tree

8 files changed

+167
-2
lines changed

8 files changed

+167
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ Released 2018-09-13
11431143
[`print_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_with_newline
11441144
[`println_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#println_empty_string
11451145
[`ptr_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg
1146+
[`ptr_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq
11461147
[`ptr_offset_with_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
11471148
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
11481149
[`question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#question_mark

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 317 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 318 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ pub mod partialeq_ne_impl;
246246
pub mod path_buf_push_overwrite;
247247
pub mod precedence;
248248
pub mod ptr;
249+
pub mod ptr_eq;
249250
pub mod ptr_offset_with_cast;
250251
pub mod question_mark;
251252
pub mod ranges;
@@ -458,6 +459,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
458459
reg.register_late_lint_pass(box excessive_precision::ExcessivePrecision);
459460
reg.register_late_lint_pass(box bit_mask::BitMask::new(conf.verbose_bit_mask_threshold));
460461
reg.register_late_lint_pass(box ptr::Ptr);
462+
reg.register_late_lint_pass(box ptr_eq::PtrEqLint);
461463
reg.register_late_lint_pass(box needless_bool::NeedlessBool);
462464
reg.register_late_lint_pass(box needless_bool::BoolComparison);
463465
reg.register_late_lint_pass(box approx_const::ApproxConstant);
@@ -857,6 +859,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
857859
ptr::CMP_NULL,
858860
ptr::MUT_FROM_REF,
859861
ptr::PTR_ARG,
862+
ptr_eq::PTR_EQ,
860863
ptr_offset_with_cast::PTR_OFFSET_WITH_CAST,
861864
question_mark::QUESTION_MARK,
862865
ranges::ITERATOR_STEP_BY_ZERO,
@@ -992,6 +995,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
992995
panic_unimplemented::PANIC_PARAMS,
993996
ptr::CMP_NULL,
994997
ptr::PTR_ARG,
998+
ptr_eq::PTR_EQ,
995999
question_mark::QUESTION_MARK,
9961000
redundant_field_names::REDUNDANT_FIELD_NAMES,
9971001
redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING,

clippy_lints/src/ptr_eq.rs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use crate::utils;
2+
use if_chain::if_chain;
3+
use rustc::hir::{BinOpKind, Expr, ExprKind};
4+
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5+
use rustc::{declare_lint_pass, declare_tool_lint};
6+
use rustc_errors::Applicability;
7+
8+
declare_clippy_lint! {
9+
/// **What it does:** Use `std::ptr::eq` when applicable
10+
///
11+
/// **Why is this bad?** This can be used to compare `&T` references
12+
/// (which coerce to `*const T` implicitly) by their address rather than
13+
/// comparing the values they point to.
14+
///
15+
/// **Known problems:** None
16+
///
17+
/// **Example:**
18+
/// ```rust
19+
/// let a = &[1, 2, 3];
20+
/// let b = &[1, 2, 3];
21+
///
22+
/// let _ = a as *const _ as usize == b as *const _ as usize;
23+
/// // Could be written
24+
/// let _ = std::ptr::eq(a, b);
25+
/// ```
26+
pub PTR_EQ,
27+
style,
28+
"use `std::ptr::eq` when applicable"
29+
}
30+
31+
declare_lint_pass!(PtrEqLint => [PTR_EQ]);
32+
33+
static LINT_MSG: &str = "use `std::ptr::eq` when applicable";
34+
35+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PtrEqLint {
36+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
37+
if let ExprKind::Binary(ref op, ref left, ref right) = expr.kind {
38+
if BinOpKind::Eq == op.node {
39+
let (left, right) = if_chain! {
40+
if let Some(lhs) = expr_as_cast_to_usize(cx, left);
41+
if let Some(rhs) = expr_as_cast_to_usize(cx, right);
42+
then {
43+
(lhs, rhs)
44+
}
45+
else {
46+
(&**left, &**right)
47+
}
48+
};
49+
50+
if let Some(left_var) = expr_as_cast_to_raw_pointer(cx, left) {
51+
if let Some(right_var) = expr_as_cast_to_raw_pointer(cx, right) {
52+
let left_snip = utils::snippet(cx, left_var.span, "..");
53+
let right_snip = utils::snippet(cx, right_var.span, "..");
54+
let sugg = format!("std::ptr::eq({}, {})", left_snip, right_snip);
55+
utils::span_lint_and_sugg(
56+
cx,
57+
PTR_EQ,
58+
expr.span,
59+
LINT_MSG,
60+
"try",
61+
sugg,
62+
Applicability::MachineApplicable,
63+
);
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
71+
// If the given expression is a cast to an usize, return the lhs of the cast
72+
// E.g., `foo as *const _ as usize` returns `foo as *const _`.
73+
fn expr_as_cast_to_usize<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cast_expr: &'tcx Expr) -> Option<&'tcx Expr> {
74+
if cx.tables.expr_ty(cast_expr) == cx.tcx.types.usize {
75+
if let ExprKind::Cast(ref expr, _) = cast_expr.kind {
76+
return Some(expr);
77+
}
78+
}
79+
None
80+
}
81+
82+
// If the given expression is a cast to a `*const` pointer, return the lhs of the cast
83+
// E.g., `foo as *const _` returns `foo`.
84+
fn expr_as_cast_to_raw_pointer<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cast_expr: &'tcx Expr) -> Option<&'tcx Expr> {
85+
if cx.tables.expr_ty(cast_expr).is_unsafe_ptr() {
86+
if let ExprKind::Cast(ref expr, _) = cast_expr.kind {
87+
return Some(expr);
88+
}
89+
}
90+
None
91+
}

src/lintlist/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 317] = [
9+
pub const ALL_LINTS: [Lint; 318] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1491,6 +1491,13 @@ pub const ALL_LINTS: [Lint; 317] = [
14911491
deprecation: None,
14921492
module: "ptr",
14931493
},
1494+
Lint {
1495+
name: "ptr_eq",
1496+
group: "style",
1497+
desc: "use `std::ptr::eq` when applicable",
1498+
deprecation: None,
1499+
module: "ptr_eq",
1500+
},
14941501
Lint {
14951502
name: "ptr_offset_with_cast",
14961503
group: "complexity",

tests/ui/ptr_eq.fixed

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-rustfix
2+
#![warn(clippy::ptr_eq)]
3+
4+
fn main() {
5+
let a = &[1, 2, 3];
6+
let b = &[1, 2, 3];
7+
8+
let _ = std::ptr::eq(a, b);
9+
let _ = std::ptr::eq(a, b);
10+
let _ = a.as_ptr() == b as *const _;
11+
let _ = a.as_ptr() == b.as_ptr();
12+
13+
// Do not lint
14+
15+
let a = &mut [1, 2, 3];
16+
let b = &mut [1, 2, 3];
17+
18+
let _ = a.as_mut_ptr() == b as *mut [i32] as *mut _;
19+
let _ = a.as_mut_ptr() == b.as_mut_ptr();
20+
21+
let _ = a == b;
22+
let _ = core::ptr::eq(a, b);
23+
}

tests/ui/ptr_eq.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-rustfix
2+
#![warn(clippy::ptr_eq)]
3+
4+
fn main() {
5+
let a = &[1, 2, 3];
6+
let b = &[1, 2, 3];
7+
8+
let _ = a as *const _ as usize == b as *const _ as usize;
9+
let _ = a as *const _ == b as *const _;
10+
let _ = a.as_ptr() == b as *const _;
11+
let _ = a.as_ptr() == b.as_ptr();
12+
13+
// Do not lint
14+
15+
let a = &mut [1, 2, 3];
16+
let b = &mut [1, 2, 3];
17+
18+
let _ = a.as_mut_ptr() == b as *mut [i32] as *mut _;
19+
let _ = a.as_mut_ptr() == b.as_mut_ptr();
20+
21+
let _ = a == b;
22+
let _ = core::ptr::eq(a, b);
23+
}

tests/ui/ptr_eq.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: use `std::ptr::eq` when applicable
2+
--> $DIR/ptr_eq.rs:8:13
3+
|
4+
LL | let _ = a as *const _ as usize == b as *const _ as usize;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(a, b)`
6+
|
7+
= note: `-D clippy::ptr-eq` implied by `-D warnings`
8+
9+
error: use `std::ptr::eq` when applicable
10+
--> $DIR/ptr_eq.rs:9:13
11+
|
12+
LL | let _ = a as *const _ == b as *const _;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(a, b)`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)