Skip to content

Commit e71fd12

Browse files
committed
New lint: Recommend using ptr::eq when possible
1 parent cc35165 commit e71fd12

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
@@ -1154,6 +1154,7 @@ Released 2018-09-13
11541154
[`print_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_with_newline
11551155
[`println_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#println_empty_string
11561156
[`ptr_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg
1157+
[`ptr_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq
11571158
[`ptr_offset_with_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
11581159
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
11591160
[`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 337 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 338 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
@@ -258,6 +258,7 @@ pub mod partialeq_ne_impl;
258258
pub mod path_buf_push_overwrite;
259259
pub mod precedence;
260260
pub mod ptr;
261+
pub mod ptr_eq;
261262
pub mod ptr_offset_with_cast;
262263
pub mod question_mark;
263264
pub mod ranges;
@@ -690,6 +691,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
690691
&ptr::CMP_NULL,
691692
&ptr::MUT_FROM_REF,
692693
&ptr::PTR_ARG,
694+
&ptr_eq::PTR_EQ,
693695
&ptr_offset_with_cast::PTR_OFFSET_WITH_CAST,
694696
&question_mark::QUESTION_MARK,
695697
&ranges::ITERATOR_STEP_BY_ZERO,
@@ -800,6 +802,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
800802
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
801803
store.register_late_pass(move || box bit_mask::BitMask::new(verbose_bit_mask_threshold));
802804
store.register_late_pass(|| box ptr::Ptr);
805+
store.register_late_pass(|| box ptr_eq::PtrEqLint);
803806
store.register_late_pass(|| box needless_bool::NeedlessBool);
804807
store.register_late_pass(|| box needless_bool::BoolComparison);
805808
store.register_late_pass(|| box approx_const::ApproxConstant);
@@ -1230,6 +1233,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
12301233
LintId::of(&ptr::CMP_NULL),
12311234
LintId::of(&ptr::MUT_FROM_REF),
12321235
LintId::of(&ptr::PTR_ARG),
1236+
LintId::of(&ptr_eq::PTR_EQ),
12331237
LintId::of(&ptr_offset_with_cast::PTR_OFFSET_WITH_CAST),
12341238
LintId::of(&question_mark::QUESTION_MARK),
12351239
LintId::of(&ranges::ITERATOR_STEP_BY_ZERO),
@@ -1373,6 +1377,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
13731377
LintId::of(&panic_unimplemented::PANIC_PARAMS),
13741378
LintId::of(&ptr::CMP_NULL),
13751379
LintId::of(&ptr::PTR_ARG),
1380+
LintId::of(&ptr_eq::PTR_EQ),
13761381
LintId::of(&question_mark::QUESTION_MARK),
13771382
LintId::of(&redundant_field_names::REDUNDANT_FIELD_NAMES),
13781383
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; 337] = [
9+
pub const ALL_LINTS: [Lint; 338] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1561,6 +1561,13 @@ pub const ALL_LINTS: [Lint; 337] = [
15611561
deprecation: None,
15621562
module: "ptr",
15631563
},
1564+
Lint {
1565+
name: "ptr_eq",
1566+
group: "style",
1567+
desc: "use `std::ptr::eq` when applicable",
1568+
deprecation: None,
1569+
module: "ptr_eq",
1570+
},
15641571
Lint {
15651572
name: "ptr_offset_with_cast",
15661573
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)