rust/clippy_lints/src/unnamed_address.rs

133 lines
4.7 KiB
Rust
Raw Normal View History

use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
2021-03-16 11:06:34 -05:00
use clippy_utils::{match_def_path, paths};
2020-03-11 19:00:00 -05:00
use if_chain::if_chain;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
/// ### What it does
/// Checks for comparisons with an address of a function item.
2020-03-11 19:00:00 -05:00
///
/// ### Why is this bad?
/// Function item address is not guaranteed to be unique and could vary
2020-03-11 19:00:00 -05:00
/// between different code generation units. Furthermore different function items could have
/// the same address after being merged together.
///
/// ### Example
2020-03-11 19:00:00 -05:00
/// ```rust
/// type F = fn();
/// fn a() {}
/// let f: F = a;
/// if f == a {
/// // ...
/// }
/// ```
Added `clippy::version` attribute to all normal lints So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`... And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun... Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work. ```nu mv v0.0.212 rust-1.00.0; mv beta rust-1.57.0; mv master rust-1.58.0; let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path); let versions = ( ls | where name =~ "rust-" | select name | format {name}/lints.json | each { open $it | select id | insert version $it | str substring "5,11" version} | group-by id | rotate counter-clockwise id version | update version {get version | first 1} | flatten | select id version); $paths | each { |row| let version = ($versions | where id == ($row.id) | format {version}) let idu = ($row.id | str upcase) $"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)" } | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh"; ``` And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
#[clippy::version = "1.44.0"]
2020-03-11 19:00:00 -05:00
pub FN_ADDRESS_COMPARISONS,
correctness,
"comparison with an address of a function item"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for comparisons with an address of a trait vtable.
2020-03-11 19:00:00 -05:00
///
/// ### Why is this bad?
/// Comparing trait objects pointers compares an vtable addresses which
2020-03-11 19:00:00 -05:00
/// 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
2020-03-11 19:00:00 -05:00
/// ```rust,ignore
/// let a: Rc<dyn Trait> = ...
/// let b: Rc<dyn Trait> = ...
/// if Rc::ptr_eq(&a, &b) {
/// ...
/// }
/// ```
Added `clippy::version` attribute to all normal lints So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`... And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun... Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work. ```nu mv v0.0.212 rust-1.00.0; mv beta rust-1.57.0; mv master rust-1.58.0; let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path); let versions = ( ls | where name =~ "rust-" | select name | format {name}/lints.json | each { open $it | select id | insert version $it | str substring "5,11" version} | group-by id | rotate counter-clockwise id version | update version {get version | first 1} | flatten | select id version); $paths | each { |row| let version = ($versions | where id == ($row.id) | format {version}) let idu = ($row.id | str upcase) $"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)" } | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh"; ``` And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
#[clippy::version = "1.44.0"]
2020-03-11 19:00:00 -05:00
pub VTABLE_ADDRESS_COMPARISONS,
correctness,
"comparison with an address of a trait vtable"
}
declare_lint_pass!(UnnamedAddress => [FN_ADDRESS_COMPARISONS, VTABLE_ADDRESS_COMPARISONS]);
impl LateLintPass<'_> for UnnamedAddress {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
2020-03-11 19:00:00 -05:00
fn is_comparison(binop: BinOpKind) -> bool {
matches!(
binop,
BinOpKind::Eq | BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ne | BinOpKind::Ge | BinOpKind::Gt
)
2020-03-11 19:00:00 -05:00
}
fn is_trait_ptr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
match cx.typeck_results().expr_ty_adjusted(expr).kind() {
2020-03-11 19:00:00 -05:00
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(..))
2020-03-11 19:00:00 -05:00
}
if_chain! {
if let ExprKind::Binary(binop, left, right) = expr.kind;
2020-03-11 19:00:00 -05:00
if is_comparison(binop.node);
if is_trait_ptr(cx, left) && is_trait_ptr(cx, right);
then {
span_lint_and_help(
cx,
VTABLE_ADDRESS_COMPARISONS,
expr.span,
"comparing trait object pointers compares a non-unique vtable address",
None,
2020-03-11 19:00:00 -05:00
"consider extracting and comparing data pointers only",
);
}
}
if_chain! {
if let ExprKind::Call(func, [ref _left, ref _right]) = expr.kind;
2020-03-11 19:00:00 -05:00
if let ExprKind::Path(ref func_qpath) = func.kind;
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
2020-03-11 19:00:00 -05:00
if match_def_path(cx, def_id, &paths::PTR_EQ) ||
match_def_path(cx, def_id, &paths::RC_PTR_EQ) ||
match_def_path(cx, def_id, &paths::ARC_PTR_EQ);
2020-07-17 03:47:04 -05:00
let ty_param = cx.typeck_results().node_substs(func.hir_id).type_at(0);
2020-03-11 19:00:00 -05:00
if ty_param.is_trait();
then {
span_lint_and_help(
cx,
VTABLE_ADDRESS_COMPARISONS,
expr.span,
"comparing trait object pointers compares a non-unique vtable address",
None,
2020-03-11 19:00:00 -05:00
"consider extracting and comparing data pointers only",
);
}
}
if_chain! {
if let ExprKind::Binary(binop, left, right) = expr.kind;
2020-03-11 19:00:00 -05:00
if is_comparison(binop.node);
2021-03-30 14:59:59 -05:00
if cx.typeck_results().expr_ty_adjusted(left).is_fn_ptr();
if cx.typeck_results().expr_ty_adjusted(right).is_fn_ptr();
2020-03-11 19:00:00 -05:00
if is_fn_def(cx, left) || is_fn_def(cx, right);
then {
span_lint(
cx,
FN_ADDRESS_COMPARISONS,
expr.span,
"comparing with a non-unique address of a function item",
);
}
}
}
}