Drop clippy::vtable_address_comparisons
This commit is contained in:
parent
c586717cfb
commit
a2ea760b88
8 changed files with 70 additions and 244 deletions
|
|
@ -684,7 +684,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
|
|||
crate::unit_types::UNIT_ARG_INFO,
|
||||
crate::unit_types::UNIT_CMP_INFO,
|
||||
crate::unnamed_address::FN_ADDRESS_COMPARISONS_INFO,
|
||||
crate::unnamed_address::VTABLE_ADDRESS_COMPARISONS_INFO,
|
||||
crate::unnecessary_box_returns::UNNECESSARY_BOX_RETURNS_INFO,
|
||||
crate::unnecessary_map_on_constructor::UNNECESSARY_MAP_ON_CONSTRUCTOR_INFO,
|
||||
crate::unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS_INFO,
|
||||
|
|
|
|||
|
|
@ -58,4 +58,5 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
|
|||
("clippy::undropped_manually_drops", "undropped_manually_drops"),
|
||||
("clippy::unknown_clippy_lints", "unknown_lints"),
|
||||
("clippy::unused_label", "unused_labels"),
|
||||
("clippy::vtable_address_comparisons", "ambiguous_wide_pointer_comparisons"),
|
||||
];
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
|
||||
use clippy_utils::diagnostics::span_lint;
|
||||
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty;
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::sym;
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
|
|
@ -29,31 +28,7 @@ declare_clippy_lint! {
|
|||
"comparison with an address of a function item"
|
||||
}
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
/// Checks for comparisons with an address of a trait vtable.
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
/// Comparing trait objects pointers compares an vtable addresses which
|
||||
/// are not guaranteed to be unique and could vary between different code generation units.
|
||||
/// Furthermore vtables for different types could have the same address after being merged
|
||||
/// together.
|
||||
///
|
||||
/// ### Example
|
||||
/// ```rust,ignore
|
||||
/// let a: Rc<dyn Trait> = ...
|
||||
/// let b: Rc<dyn Trait> = ...
|
||||
/// if Rc::ptr_eq(&a, &b) {
|
||||
/// ...
|
||||
/// }
|
||||
/// ```
|
||||
#[clippy::version = "1.44.0"]
|
||||
pub VTABLE_ADDRESS_COMPARISONS,
|
||||
correctness,
|
||||
"comparison with an address of a trait vtable"
|
||||
}
|
||||
|
||||
declare_lint_pass!(UnnamedAddress => [FN_ADDRESS_COMPARISONS, VTABLE_ADDRESS_COMPARISONS]);
|
||||
declare_lint_pass!(UnnamedAddress => [FN_ADDRESS_COMPARISONS]);
|
||||
|
||||
impl LateLintPass<'_> for UnnamedAddress {
|
||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||
|
|
@ -64,49 +39,10 @@ impl LateLintPass<'_> for UnnamedAddress {
|
|||
)
|
||||
}
|
||||
|
||||
fn is_trait_ptr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||
match cx.typeck_results().expr_ty_adjusted(expr).kind() {
|
||||
ty::RawPtr(ty::TypeAndMut { ty, .. }) => ty.is_trait(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_fn_def(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||
matches!(cx.typeck_results().expr_ty(expr).kind(), ty::FnDef(..))
|
||||
}
|
||||
|
||||
if let ExprKind::Binary(binop, left, right) = expr.kind
|
||||
&& is_comparison(binop.node)
|
||||
&& is_trait_ptr(cx, left)
|
||||
&& is_trait_ptr(cx, right)
|
||||
{
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
VTABLE_ADDRESS_COMPARISONS,
|
||||
expr.span,
|
||||
"comparing trait object pointers compares a non-unique vtable address",
|
||||
None,
|
||||
"consider extracting and comparing data pointers only",
|
||||
);
|
||||
}
|
||||
|
||||
if let ExprKind::Call(func, [ref _left, ref _right]) = expr.kind
|
||||
&& let ExprKind::Path(ref func_qpath) = func.kind
|
||||
&& let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id()
|
||||
&& cx.tcx.is_diagnostic_item(sym::ptr_eq, def_id)
|
||||
&& let ty_param = cx.typeck_results().node_args(func.hir_id).type_at(0)
|
||||
&& ty_param.is_trait()
|
||||
{
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
VTABLE_ADDRESS_COMPARISONS,
|
||||
expr.span,
|
||||
"comparing trait object pointers compares a non-unique vtable address",
|
||||
None,
|
||||
"consider extracting and comparing data pointers only",
|
||||
);
|
||||
}
|
||||
|
||||
if let ExprKind::Binary(binop, left, right) = expr.kind
|
||||
&& is_comparison(binop.node)
|
||||
&& cx.typeck_results().expr_ty_adjusted(left).is_fn_ptr()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue