From 166737f3cbbe2157454c759cb56662c941c6031c Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Sun, 2 Jan 2022 23:34:10 +0100 Subject: [PATCH] Add manual_bits lint --- CHANGELOG.md | 1 + clippy_lints/src/lib.register_all.rs | 1 + clippy_lints/src/lib.register_lints.rs | 1 + clippy_lints/src/lib.register_style.rs | 1 + clippy_lints/src/lib.rs | 2 + clippy_lints/src/manual_bits.rs | 107 +++++++++++++++++ clippy_utils/src/msrvs.rs | 2 +- tests/ui/manual_bits.fixed | 48 ++++++++ tests/ui/manual_bits.rs | 48 ++++++++ tests/ui/manual_bits.stderr | 154 +++++++++++++++++++++++++ 10 files changed, 364 insertions(+), 1 deletion(-) create mode 100644 clippy_lints/src/manual_bits.rs create mode 100644 tests/ui/manual_bits.fixed create mode 100644 tests/ui/manual_bits.rs create mode 100644 tests/ui/manual_bits.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index e521995a18864..8f4da9a382792 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3071,6 +3071,7 @@ Released 2018-09-13 [`main_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#main_recursion [`manual_assert`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_assert [`manual_async_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_async_fn +[`manual_bits`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_bits [`manual_filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_filter_map [`manual_find_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_find_map [`manual_flatten`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten diff --git a/clippy_lints/src/lib.register_all.rs b/clippy_lints/src/lib.register_all.rs index 0cab28cfafacd..26fb4259952b6 100644 --- a/clippy_lints/src/lib.register_all.rs +++ b/clippy_lints/src/lib.register_all.rs @@ -115,6 +115,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![ LintId::of(loops::WHILE_LET_ON_ITERATOR), LintId::of(main_recursion::MAIN_RECURSION), LintId::of(manual_async_fn::MANUAL_ASYNC_FN), + LintId::of(manual_bits::MANUAL_BITS), LintId::of(manual_map::MANUAL_MAP), LintId::of(manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE), LintId::of(manual_strip::MANUAL_STRIP), diff --git a/clippy_lints/src/lib.register_lints.rs b/clippy_lints/src/lib.register_lints.rs index 09e4f6ae27504..746bdb19c3d92 100644 --- a/clippy_lints/src/lib.register_lints.rs +++ b/clippy_lints/src/lib.register_lints.rs @@ -226,6 +226,7 @@ store.register_lints(&[ main_recursion::MAIN_RECURSION, manual_assert::MANUAL_ASSERT, manual_async_fn::MANUAL_ASYNC_FN, + manual_bits::MANUAL_BITS, manual_map::MANUAL_MAP, manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE, manual_ok_or::MANUAL_OK_OR, diff --git a/clippy_lints/src/lib.register_style.rs b/clippy_lints/src/lib.register_style.rs index 57de34f41ace7..05211476ff230 100644 --- a/clippy_lints/src/lib.register_style.rs +++ b/clippy_lints/src/lib.register_style.rs @@ -43,6 +43,7 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![ LintId::of(loops::WHILE_LET_ON_ITERATOR), LintId::of(main_recursion::MAIN_RECURSION), LintId::of(manual_async_fn::MANUAL_ASYNC_FN), + LintId::of(manual_bits::MANUAL_BITS), LintId::of(manual_map::MANUAL_MAP), LintId::of(manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE), LintId::of(map_clone::MAP_CLONE), diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 80d979362f8a4..79e9882fef4c4 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -261,6 +261,7 @@ mod macro_use; mod main_recursion; mod manual_assert; mod manual_async_fn; +mod manual_bits; mod manual_map; mod manual_non_exhaustive; mod manual_ok_or; @@ -858,6 +859,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|| Box::new(init_numbered_fields::NumberedFields)); store.register_early_pass(|| Box::new(single_char_lifetime_names::SingleCharLifetimeNames)); store.register_late_pass(move || Box::new(borrow_as_ptr::BorrowAsPtr::new(msrv))); + store.register_late_pass(move || Box::new(manual_bits::ManualBits::new(msrv))); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/manual_bits.rs b/clippy_lints/src/manual_bits.rs new file mode 100644 index 0000000000000..50bf2527e39a8 --- /dev/null +++ b/clippy_lints/src/manual_bits.rs @@ -0,0 +1,107 @@ +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet_opt; +use clippy_utils::{match_def_path, meets_msrv, msrvs, paths}; +use rustc_ast::ast::LitKind; +use rustc_errors::Applicability; +use rustc_hir::{BinOpKind, Expr, ExprKind, GenericArg, QPath}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_middle::ty::{self, Ty}; +use rustc_semver::RustcVersion; +use rustc_session::{declare_tool_lint, impl_lint_pass}; + +declare_clippy_lint! { + /// ### What it does + /// Checks for uses of `std::mem::size_of::() * 8` when + /// `T::BITS` is available. + /// + /// ### Why is this bad? + /// Can be written as the shorter `T::BITS`. + /// + /// ### Example + /// ```rust + /// std::mem::size_of::() * 8; + /// ``` + /// Use instead: + /// ```rust + /// usize::BITS; + /// ``` + #[clippy::version = "1.60.0"] + pub MANUAL_BITS, + style, + "manual implementation of `size_of::() * 8` can be simplified with `T::BITS`" +} + +#[derive(Clone)] +pub struct ManualBits { + msrv: Option, +} + +impl ManualBits { + #[must_use] + pub fn new(msrv: Option) -> Self { + 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<'_>) { + if !meets_msrv(self.msrv.as_ref(), &msrvs::MANUAL_BITS) { + return; + } + + if_chain! { + if let ExprKind::Binary(bin_op, left_expr, right_expr) = expr.kind; + if let BinOpKind::Mul = &bin_op.node; + if let Some((real_ty, resolved_ty, other_expr)) = get_one_size_of_ty(cx, left_expr, right_expr); + if matches!(resolved_ty.kind(), ty::Int(_) | ty::Uint(_)); + if let ExprKind::Lit(lit) = &other_expr.kind; + if let LitKind::Int(8, _) = lit.node; + + then { + span_lint_and_sugg( + cx, + MANUAL_BITS, + expr.span, + "usage of `mem::size_of::()` to obtain the size of `T` in bits", + "consider using", + format!("{}::BITS", snippet_opt(cx, real_ty.span).unwrap()), + Applicability::MachineApplicable, + ); + } + } + } +} + +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>)> { + if_chain! { + if let ExprKind::Call(count_func, _func_args) = expr.kind; + if let ExprKind::Path(ref count_func_qpath) = count_func.kind; + + if let QPath::Resolved(_, count_func_path) = count_func_qpath; + if let Some(segment_zero) = count_func_path.segments.get(0); + if let Some(args) = segment_zero.args; + if let Some(GenericArg::Type(real_ty)) = args.args.get(0); + + if let Some(def_id) = cx.qpath_res(count_func_qpath, count_func.hir_id).opt_def_id(); + if match_def_path(cx, def_id, &paths::MEM_SIZE_OF); + then { + cx.typeck_results().node_substs(count_func.hir_id).types().next().map(|resolved_ty| (real_ty, resolved_ty)) + } else { + None + } + } +} diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs index af677d8b00f7b..a5b409ad96bbb 100644 --- a/clippy_utils/src/msrvs.rs +++ b/clippy_utils/src/msrvs.rs @@ -12,7 +12,7 @@ macro_rules! msrv_aliases { // names may refer to stabilized feature flags or library items msrv_aliases! { - 1,53,0 { OR_PATTERNS } + 1,53,0 { OR_PATTERNS, MANUAL_BITS } 1,52,0 { STR_SPLIT_ONCE } 1,51,0 { BORROW_AS_PTR } 1,50,0 { BOOL_THEN } diff --git a/tests/ui/manual_bits.fixed b/tests/ui/manual_bits.fixed new file mode 100644 index 0000000000000..4f1b19b75b8a1 --- /dev/null +++ b/tests/ui/manual_bits.fixed @@ -0,0 +1,48 @@ +// run-rustfix + +#![warn(clippy::manual_bits)] +#![allow(clippy::no_effect, path_statements, unused_must_use, clippy::unnecessary_operation)] + +use std::mem::{size_of, size_of_val}; + +fn main() { + i8::BITS; + i16::BITS; + i32::BITS; + i64::BITS; + i128::BITS; + isize::BITS; + + u8::BITS; + u16::BITS; + u32::BITS; + u64::BITS; + u128::BITS; + usize::BITS; + + i8::BITS; + i16::BITS; + i32::BITS; + i64::BITS; + i128::BITS; + isize::BITS; + + u8::BITS; + u16::BITS; + u32::BITS; + u64::BITS; + u128::BITS; + usize::BITS; + + size_of::() * 4; + 4 * size_of::(); + size_of::() * 8; + 8 * size_of::(); + + size_of_val(&0u32) * 8; + + type Word = u32; + Word::BITS; + type Bool = bool; + size_of::() * 8; +} diff --git a/tests/ui/manual_bits.rs b/tests/ui/manual_bits.rs new file mode 100644 index 0000000000000..f8a01313e6ad0 --- /dev/null +++ b/tests/ui/manual_bits.rs @@ -0,0 +1,48 @@ +// run-rustfix + +#![warn(clippy::manual_bits)] +#![allow(clippy::no_effect, path_statements, unused_must_use, clippy::unnecessary_operation)] + +use std::mem::{size_of, size_of_val}; + +fn main() { + size_of::() * 8; + size_of::() * 8; + size_of::() * 8; + size_of::() * 8; + size_of::() * 8; + size_of::() * 8; + + size_of::() * 8; + size_of::() * 8; + size_of::() * 8; + size_of::() * 8; + size_of::() * 8; + size_of::() * 8; + + 8 * size_of::(); + 8 * size_of::(); + 8 * size_of::(); + 8 * size_of::(); + 8 * size_of::(); + 8 * size_of::(); + + 8 * size_of::(); + 8 * size_of::(); + 8 * size_of::(); + 8 * size_of::(); + 8 * size_of::(); + 8 * size_of::(); + + size_of::() * 4; + 4 * size_of::(); + size_of::() * 8; + 8 * size_of::(); + + size_of_val(&0u32) * 8; + + type Word = u32; + size_of::() * 8; + type Bool = bool; + size_of::() * 8; +} diff --git a/tests/ui/manual_bits.stderr b/tests/ui/manual_bits.stderr new file mode 100644 index 0000000000000..c4f5af2dcb0ec --- /dev/null +++ b/tests/ui/manual_bits.stderr @@ -0,0 +1,154 @@ +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:9:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^ help: consider using: `i8::BITS` + | + = note: `-D clippy::manual-bits` implied by `-D warnings` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:10:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i16::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:11:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i32::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:12:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i64::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:13:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `i128::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:14:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `isize::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:16:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^ help: consider using: `u8::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:17:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u16::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:18:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u32::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:19:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u64::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:20:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:21:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `usize::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:23:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^ help: consider using: `i8::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:24:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i16::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:25:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i32::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:26:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i64::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:27:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `i128::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:28:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `isize::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:30:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^ help: consider using: `u8::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:31:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u16::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:32:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u32::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:33:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u64::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:34:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:35:5 + | +LL | 8 * size_of::(); + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `usize::BITS` + +error: usage of `mem::size_of::()` to obtain the size of `T` in bits + --> $DIR/manual_bits.rs:45:5 + | +LL | size_of::() * 8; + | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `Word::BITS` + +error: aborting due to 25 previous errors +