Skip to content

Commit c8213a4

Browse files
committed
Auto merge of #117758 - Urgau:lint_pointer_trait_comparisons, r=davidtwco
Add lint against ambiguous wide pointer comparisons This PR is the resolution of rust-lang/rust#106447 decided in rust-lang/rust#117717 by T-lang. ## `ambiguous_wide_pointer_comparisons` *warn-by-default* The `ambiguous_wide_pointer_comparisons` lint checks comparison of `*const/*mut ?Sized` as the operands. ### Example ```rust let ab = (A, B); let a = &ab.0 as *const dyn T; let b = &ab.1 as *const dyn T; let _ = a == b; ``` ### Explanation The comparison includes metadata which may not be expected. ------- This PR also drops `clippy::vtable_address_comparisons` which is superseded by this one. ~~One thing: is the current naming right? `invalid` seems a bit too much.~~ Fixes rust-lang/rust#117717
2 parents 1f9b674 + a2ea760 commit c8213a4

8 files changed

+70
-244
lines changed

clippy_lints/src/declared_lints.rs

-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
684684
crate::unit_types::UNIT_ARG_INFO,
685685
crate::unit_types::UNIT_CMP_INFO,
686686
crate::unnamed_address::FN_ADDRESS_COMPARISONS_INFO,
687-
crate::unnamed_address::VTABLE_ADDRESS_COMPARISONS_INFO,
688687
crate::unnecessary_box_returns::UNNECESSARY_BOX_RETURNS_INFO,
689688
crate::unnecessary_map_on_constructor::UNNECESSARY_MAP_ON_CONSTRUCTOR_INFO,
690689
crate::unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS_INFO,

clippy_lints/src/renamed_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
5858
("clippy::undropped_manually_drops", "undropped_manually_drops"),
5959
("clippy::unknown_clippy_lints", "unknown_lints"),
6060
("clippy::unused_label", "unused_labels"),
61+
("clippy::vtable_address_comparisons", "ambiguous_wide_pointer_comparisons"),
6162
];

clippy_lints/src/unnamed_address.rs

+2-66
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
1+
use clippy_utils::diagnostics::span_lint;
22
use rustc_hir::{BinOpKind, Expr, ExprKind};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_middle::ty;
55
use rustc_session::declare_lint_pass;
6-
use rustc_span::sym;
76

87
declare_clippy_lint! {
98
/// ### What it does
@@ -29,31 +28,7 @@ declare_clippy_lint! {
2928
"comparison with an address of a function item"
3029
}
3130

32-
declare_clippy_lint! {
33-
/// ### What it does
34-
/// Checks for comparisons with an address of a trait vtable.
35-
///
36-
/// ### Why is this bad?
37-
/// Comparing trait objects pointers compares an vtable addresses which
38-
/// are not guaranteed to be unique and could vary between different code generation units.
39-
/// Furthermore vtables for different types could have the same address after being merged
40-
/// together.
41-
///
42-
/// ### Example
43-
/// ```rust,ignore
44-
/// let a: Rc<dyn Trait> = ...
45-
/// let b: Rc<dyn Trait> = ...
46-
/// if Rc::ptr_eq(&a, &b) {
47-
/// ...
48-
/// }
49-
/// ```
50-
#[clippy::version = "1.44.0"]
51-
pub VTABLE_ADDRESS_COMPARISONS,
52-
correctness,
53-
"comparison with an address of a trait vtable"
54-
}
55-
56-
declare_lint_pass!(UnnamedAddress => [FN_ADDRESS_COMPARISONS, VTABLE_ADDRESS_COMPARISONS]);
31+
declare_lint_pass!(UnnamedAddress => [FN_ADDRESS_COMPARISONS]);
5732

5833
impl LateLintPass<'_> for UnnamedAddress {
5934
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
@@ -64,49 +39,10 @@ impl LateLintPass<'_> for UnnamedAddress {
6439
)
6540
}
6641

67-
fn is_trait_ptr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
68-
match cx.typeck_results().expr_ty_adjusted(expr).kind() {
69-
ty::RawPtr(ty::TypeAndMut { ty, .. }) => ty.is_trait(),
70-
_ => false,
71-
}
72-
}
73-
7442
fn is_fn_def(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
7543
matches!(cx.typeck_results().expr_ty(expr).kind(), ty::FnDef(..))
7644
}
7745

78-
if let ExprKind::Binary(binop, left, right) = expr.kind
79-
&& is_comparison(binop.node)
80-
&& is_trait_ptr(cx, left)
81-
&& is_trait_ptr(cx, right)
82-
{
83-
span_lint_and_help(
84-
cx,
85-
VTABLE_ADDRESS_COMPARISONS,
86-
expr.span,
87-
"comparing trait object pointers compares a non-unique vtable address",
88-
None,
89-
"consider extracting and comparing data pointers only",
90-
);
91-
}
92-
93-
if let ExprKind::Call(func, [ref _left, ref _right]) = expr.kind
94-
&& let ExprKind::Path(ref func_qpath) = func.kind
95-
&& let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id()
96-
&& cx.tcx.is_diagnostic_item(sym::ptr_eq, def_id)
97-
&& let ty_param = cx.typeck_results().node_args(func.hir_id).type_at(0)
98-
&& ty_param.is_trait()
99-
{
100-
span_lint_and_help(
101-
cx,
102-
VTABLE_ADDRESS_COMPARISONS,
103-
expr.span,
104-
"comparing trait object pointers compares a non-unique vtable address",
105-
None,
106-
"consider extracting and comparing data pointers only",
107-
);
108-
}
109-
11046
if let ExprKind::Binary(binop, left, right) = expr.kind
11147
&& is_comparison(binop.node)
11248
&& cx.typeck_results().expr_ty_adjusted(left).is_fn_ptr()

tests/ui/rename.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#![allow(undropped_manually_drops)]
5252
#![allow(unknown_lints)]
5353
#![allow(unused_labels)]
54+
#![allow(ambiguous_wide_pointer_comparisons)]
5455
#![warn(clippy::almost_complete_range)]
5556
#![warn(clippy::disallowed_names)]
5657
#![warn(clippy::blocks_in_if_conditions)]
@@ -107,5 +108,6 @@
107108
#![warn(undropped_manually_drops)]
108109
#![warn(unknown_lints)]
109110
#![warn(unused_labels)]
111+
#![warn(ambiguous_wide_pointer_comparisons)]
110112

111113
fn main() {}

tests/ui/rename.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#![allow(undropped_manually_drops)]
5252
#![allow(unknown_lints)]
5353
#![allow(unused_labels)]
54+
#![allow(ambiguous_wide_pointer_comparisons)]
5455
#![warn(clippy::almost_complete_letter_range)]
5556
#![warn(clippy::blacklisted_name)]
5657
#![warn(clippy::block_in_if_condition_expr)]
@@ -107,5 +108,6 @@
107108
#![warn(clippy::undropped_manually_drops)]
108109
#![warn(clippy::unknown_clippy_lints)]
109110
#![warn(clippy::unused_label)]
111+
#![warn(clippy::vtable_address_comparisons)]
110112

111113
fn main() {}

0 commit comments

Comments
 (0)