Skip to content

Commit 022137c

Browse files
committed
New lint: Recommend using ptr::eq when possible
1 parent 8fae2dd commit 022137c

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
@@ -1148,6 +1148,7 @@ Released 2018-09-13
11481148
[`print_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_with_newline
11491149
[`println_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#println_empty_string
11501150
[`ptr_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg
1151+
[`ptr_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq
11511152
[`ptr_offset_with_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
11521153
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
11531154
[`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 324 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 325 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
@@ -249,6 +249,7 @@ pub mod partialeq_ne_impl;
249249
pub mod path_buf_push_overwrite;
250250
pub mod precedence;
251251
pub mod ptr;
252+
pub mod ptr_eq;
252253
pub mod ptr_offset_with_cast;
253254
pub mod question_mark;
254255
pub mod ranges;
@@ -461,6 +462,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
461462
reg.register_late_lint_pass(box excessive_precision::ExcessivePrecision);
462463
reg.register_late_lint_pass(box bit_mask::BitMask::new(conf.verbose_bit_mask_threshold));
463464
reg.register_late_lint_pass(box ptr::Ptr);
465+
reg.register_late_lint_pass(box ptr_eq::PtrEqLint);
464466
reg.register_late_lint_pass(box needless_bool::NeedlessBool);
465467
reg.register_late_lint_pass(box needless_bool::BoolComparison);
466468
reg.register_late_lint_pass(box approx_const::ApproxConstant);
@@ -867,6 +869,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
867869
ptr::CMP_NULL,
868870
ptr::MUT_FROM_REF,
869871
ptr::PTR_ARG,
872+
ptr_eq::PTR_EQ,
870873
ptr_offset_with_cast::PTR_OFFSET_WITH_CAST,
871874
question_mark::QUESTION_MARK,
872875
ranges::ITERATOR_STEP_BY_ZERO,
@@ -1008,6 +1011,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
10081011
panic_unimplemented::PANIC_PARAMS,
10091012
ptr::CMP_NULL,
10101013
ptr::PTR_ARG,
1014+
ptr_eq::PTR_EQ,
10111015
question_mark::QUESTION_MARK,
10121016
redundant_field_names::REDUNDANT_FIELD_NAMES,
10131017
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; 324] = [
9+
pub const ALL_LINTS: [Lint; 325] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1526,6 +1526,13 @@ pub const ALL_LINTS: [Lint; 324] = [
15261526
deprecation: None,
15271527
module: "ptr",
15281528
},
1529+
Lint {
1530+
name: "ptr_eq",
1531+
group: "style",
1532+
desc: "use `std::ptr::eq` when applicable",
1533+
deprecation: None,
1534+
module: "ptr_eq",
1535+
},
15291536
Lint {
15301537
name: "ptr_offset_with_cast",
15311538
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)