1- use clippy_utils:: diagnostics:: { span_lint, span_lint_and_help } ;
1+ use clippy_utils:: diagnostics:: span_lint;
22use rustc_hir:: { BinOpKind , Expr , ExprKind } ;
33use rustc_lint:: { LateContext , LateLintPass } ;
44use rustc_middle:: ty;
55use rustc_session:: declare_lint_pass;
6- use rustc_span:: sym;
76
87declare_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
5833impl 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 ( )
0 commit comments