Skip to content

Commit 2abadf8

Browse files
committed
New lint: Recommend using ptr::eq when possible
1 parent 8ab24d7 commit 2abadf8

File tree

8 files changed

+168
-2
lines changed

8 files changed

+168
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ Released 2018-09-13
11521152
[`print_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_with_newline
11531153
[`println_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#println_empty_string
11541154
[`ptr_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg
1155+
[`ptr_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq
11551156
[`ptr_offset_with_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
11561157
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
11571158
[`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 332 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 333 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

+5
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ pub mod partialeq_ne_impl;
255255
pub mod path_buf_push_overwrite;
256256
pub mod precedence;
257257
pub mod ptr;
258+
pub mod ptr_eq;
258259
pub mod ptr_offset_with_cast;
259260
pub mod question_mark;
260261
pub mod ranges;
@@ -678,6 +679,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
678679
&ptr::CMP_NULL,
679680
&ptr::MUT_FROM_REF,
680681
&ptr::PTR_ARG,
682+
&ptr_eq::PTR_EQ,
681683
&ptr_offset_with_cast::PTR_OFFSET_WITH_CAST,
682684
&question_mark::QUESTION_MARK,
683685
&ranges::ITERATOR_STEP_BY_ZERO,
@@ -786,6 +788,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
786788
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
787789
store.register_late_pass(move || box bit_mask::BitMask::new(verbose_bit_mask_threshold));
788790
store.register_late_pass(|| box ptr::Ptr);
791+
store.register_late_pass(|| box ptr_eq::PtrEqLint);
789792
store.register_late_pass(|| box needless_bool::NeedlessBool);
790793
store.register_late_pass(|| box needless_bool::BoolComparison);
791794
store.register_late_pass(|| box approx_const::ApproxConstant);
@@ -1209,6 +1212,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
12091212
LintId::of(&ptr::CMP_NULL),
12101213
LintId::of(&ptr::MUT_FROM_REF),
12111214
LintId::of(&ptr::PTR_ARG),
1215+
LintId::of(&ptr_eq::PTR_EQ),
12121216
LintId::of(&ptr_offset_with_cast::PTR_OFFSET_WITH_CAST),
12131217
LintId::of(&question_mark::QUESTION_MARK),
12141218
LintId::of(&ranges::ITERATOR_STEP_BY_ZERO),
@@ -1350,6 +1354,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
13501354
LintId::of(&panic_unimplemented::PANIC_PARAMS),
13511355
LintId::of(&ptr::CMP_NULL),
13521356
LintId::of(&ptr::PTR_ARG),
1357+
LintId::of(&ptr_eq::PTR_EQ),
13531358
LintId::of(&question_mark::QUESTION_MARK),
13541359
LintId::of(&redundant_field_names::REDUNDANT_FIELD_NAMES),
13551360
LintId::of(&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; 332] = [
9+
pub const ALL_LINTS: [Lint; 333] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1554,6 +1554,13 @@ pub const ALL_LINTS: [Lint; 332] = [
15541554
deprecation: None,
15551555
module: "ptr",
15561556
},
1557+
Lint {
1558+
name: "ptr_eq",
1559+
group: "style",
1560+
desc: "use `std::ptr::eq` when applicable",
1561+
deprecation: None,
1562+
module: "ptr_eq",
1563+
},
15571564
Lint {
15581565
name: "ptr_offset_with_cast",
15591566
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)