2023-11-02 11:35:56 -05:00
|
|
|
use clippy_config::msrvs::{self, Msrv};
|
2022-01-13 06:18:19 -06:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2022-12-01 11:29:38 -06:00
|
|
|
use clippy_utils::get_parent_expr;
|
2023-03-10 03:53:50 -06:00
|
|
|
use clippy_utils::source::snippet_with_context;
|
2022-01-13 06:18:19 -06:00
|
|
|
use rustc_ast::ast::LitKind;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind, GenericArg, QPath};
|
2023-03-10 03:53:50 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
2022-01-13 06:18:19 -06:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2022-02-26 07:26:21 -06:00
|
|
|
use rustc_span::sym;
|
2022-01-13 06:18:19 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
2023-04-23 06:03:09 -05:00
|
|
|
/// Checks for usage of `std::mem::size_of::<T>() * 8` when
|
2022-01-13 06:18:19 -06:00
|
|
|
/// `T::BITS` is available.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Can be written as the shorter `T::BITS`.
|
|
|
|
///
|
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-01-13 06:18:19 -06:00
|
|
|
/// std::mem::size_of::<usize>() * 8;
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-05-05 09:12:52 -05:00
|
|
|
/// usize::BITS as usize;
|
2022-01-13 06:18:19 -06:00
|
|
|
/// ```
|
|
|
|
#[clippy::version = "1.60.0"]
|
|
|
|
pub MANUAL_BITS,
|
|
|
|
style,
|
|
|
|
"manual implementation of `size_of::<T>() * 8` can be simplified with `T::BITS`"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ManualBits {
|
2022-12-01 11:29:38 -06:00
|
|
|
msrv: Msrv,
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ManualBits {
|
|
|
|
#[must_use]
|
2022-12-01 11:29:38 -06:00
|
|
|
pub fn new(msrv: Msrv) -> Self {
|
2022-01-13 06:18:19 -06:00
|
|
|
Self { msrv }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(ManualBits => [MANUAL_BITS]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for ManualBits {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2022-12-01 11:29:38 -06:00
|
|
|
if !self.msrv.meets(msrvs::MANUAL_BITS) {
|
2022-01-13 06:18:19 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:13:24 -06:00
|
|
|
if let ExprKind::Binary(bin_op, left_expr, right_expr) = expr.kind
|
|
|
|
&& let BinOpKind::Mul = &bin_op.node
|
|
|
|
&& !in_external_macro(cx.sess(), expr.span)
|
|
|
|
&& let ctxt = expr.span.ctxt()
|
|
|
|
&& left_expr.span.ctxt() == ctxt
|
|
|
|
&& right_expr.span.ctxt() == ctxt
|
|
|
|
&& let Some((real_ty, resolved_ty, other_expr)) = get_one_size_of_ty(cx, left_expr, right_expr)
|
|
|
|
&& matches!(resolved_ty.kind(), ty::Int(_) | ty::Uint(_))
|
|
|
|
&& let ExprKind::Lit(lit) = &other_expr.kind
|
|
|
|
&& let LitKind::Int(8, _) = lit.node
|
|
|
|
{
|
|
|
|
let mut app = Applicability::MachineApplicable;
|
|
|
|
let ty_snip = snippet_with_context(cx, real_ty.span, ctxt, "..", &mut app).0;
|
|
|
|
let sugg = create_sugg(cx, expr, format!("{ty_snip}::BITS"));
|
2022-05-05 09:12:52 -05:00
|
|
|
|
2023-11-16 12:13:24 -06:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MANUAL_BITS,
|
|
|
|
expr.span,
|
|
|
|
"usage of `mem::size_of::<T>()` to obtain the size of `T` in bits",
|
|
|
|
"consider using",
|
|
|
|
sugg,
|
|
|
|
app,
|
|
|
|
);
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 08:12:45 -06:00
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_one_size_of_ty<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr1: &'tcx Expr<'_>,
|
|
|
|
expr2: &'tcx Expr<'_>,
|
|
|
|
) -> Option<(&'tcx rustc_hir::Ty<'tcx>, Ty<'tcx>, &'tcx Expr<'tcx>)> {
|
|
|
|
match (get_size_of_ty(cx, expr1), get_size_of_ty(cx, expr2)) {
|
|
|
|
(Some((real_ty, resolved_ty)), None) => Some((real_ty, resolved_ty, expr2)),
|
|
|
|
(None, Some((real_ty, resolved_ty))) => Some((real_ty, resolved_ty, expr1)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_size_of_ty<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<(&'tcx rustc_hir::Ty<'tcx>, Ty<'tcx>)> {
|
2023-11-16 12:13:24 -06:00
|
|
|
if let ExprKind::Call(count_func, _func_args) = expr.kind
|
|
|
|
&& let ExprKind::Path(ref count_func_qpath) = count_func.kind
|
|
|
|
&& let QPath::Resolved(_, count_func_path) = count_func_qpath
|
|
|
|
&& let Some(segment_zero) = count_func_path.segments.first()
|
|
|
|
&& let Some(args) = segment_zero.args
|
|
|
|
&& let Some(GenericArg::Type(real_ty)) = args.args.first()
|
|
|
|
&& let Some(def_id) = cx.qpath_res(count_func_qpath, count_func.hir_id).opt_def_id()
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::mem_size_of, def_id)
|
|
|
|
{
|
|
|
|
cx.typeck_results()
|
|
|
|
.node_args(count_func.hir_id)
|
|
|
|
.types()
|
|
|
|
.next()
|
|
|
|
.map(|resolved_ty| (*real_ty, resolved_ty))
|
|
|
|
} else {
|
|
|
|
None
|
2022-01-13 06:18:19 -06:00
|
|
|
}
|
|
|
|
}
|
2022-05-05 09:12:52 -05:00
|
|
|
|
|
|
|
fn create_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, base_sugg: String) -> String {
|
|
|
|
if let Some(parent_expr) = get_parent_expr(cx, expr) {
|
|
|
|
if is_ty_conversion(parent_expr) {
|
|
|
|
return base_sugg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These expressions have precedence over casts, the suggestion therefore
|
|
|
|
// needs to be wrapped into parentheses
|
|
|
|
match parent_expr.kind {
|
|
|
|
ExprKind::Unary(..) | ExprKind::AddrOf(..) | ExprKind::MethodCall(..) => {
|
|
|
|
return format!("({base_sugg} as usize)");
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format!("{base_sugg} as usize")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_ty_conversion(expr: &Expr<'_>) -> bool {
|
|
|
|
if let ExprKind::Cast(..) = expr.kind {
|
|
|
|
true
|
2022-09-01 04:43:35 -05:00
|
|
|
} else if let ExprKind::MethodCall(path, _, [], _) = expr.kind
|
2022-05-05 09:12:52 -05:00
|
|
|
&& path.ident.name == rustc_span::sym::try_into
|
|
|
|
{
|
|
|
|
// This is only called for `usize` which implements `TryInto`. Therefore,
|
|
|
|
// we don't have to check here if `self` implements the `TryInto` trait.
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|