1
- use clippy_utils:: diagnostics:: { span_lint, span_lint_and_help } ;
1
+ use clippy_utils:: diagnostics:: span_lint;
2
2
use rustc_hir:: { BinOpKind , Expr , ExprKind } ;
3
3
use rustc_lint:: { LateContext , LateLintPass } ;
4
4
use rustc_middle:: ty;
5
5
use rustc_session:: declare_lint_pass;
6
- use rustc_span:: sym;
7
6
8
7
declare_clippy_lint ! {
9
8
/// ### What it does
@@ -29,31 +28,7 @@ declare_clippy_lint! {
29
28
"comparison with an address of a function item"
30
29
}
31
30
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 ] ) ;
57
32
58
33
impl LateLintPass < ' _ > for UnnamedAddress {
59
34
fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
@@ -64,49 +39,10 @@ impl LateLintPass<'_> for UnnamedAddress {
64
39
)
65
40
}
66
41
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
-
74
42
fn is_fn_def ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
75
43
matches ! ( cx. typeck_results( ) . expr_ty( expr) . kind( ) , ty:: FnDef ( ..) )
76
44
}
77
45
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
-
110
46
if let ExprKind :: Binary ( binop, left, right) = expr. kind
111
47
&& is_comparison ( binop. node )
112
48
&& cx. typeck_results ( ) . expr_ty_adjusted ( left) . is_fn_ptr ( )
0 commit comments