Skip to content

Commit

Permalink
Auto merge of rust-lang#8677 - xFrednet:8213-manual-bits-suggestion, …
Browse files Browse the repository at this point in the history
…r=giraffate

Add `usize` cast to `clippy::manual_bits` suggestion

A fix for the suggestion from rust-lang#8213

changelog: [`manual_bits`]: The suggestion now includes a cast for proper type conversion
  • Loading branch information
bors committed Apr 14, 2022
2 parents 38ba055 + 3bd0ac7 commit ecb3c3f
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 87 deletions.
48 changes: 42 additions & 6 deletions clippy_lints/src/manual_bits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_opt;
use clippy_utils::{meets_msrv, msrvs};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{get_parent_expr, meets_msrv, msrvs};
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind, GenericArg, QPath};
Expand All @@ -24,7 +24,7 @@ declare_clippy_lint! {
/// ```
/// Use instead:
/// ```rust
/// usize::BITS;
/// usize::BITS as usize;
/// ```
#[clippy::version = "1.60.0"]
pub MANUAL_BITS,
Expand Down Expand Up @@ -59,16 +59,19 @@ impl<'tcx> LateLintPass<'tcx> for ManualBits {
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 {
let mut app = Applicability::MachineApplicable;
let ty_snip = snippet_with_applicability(cx, real_ty.span, "..", &mut app);
let sugg = create_sugg(cx, expr, format!("{ty_snip}::BITS"));

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",
format!("{}::BITS", snippet_opt(cx, real_ty.span).unwrap()),
Applicability::MachineApplicable,
sugg,
app,
);
}
}
Expand Down Expand Up @@ -108,3 +111,36 @@ fn get_size_of_ty<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<
}
}
}

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
} else if let ExprKind::MethodCall(path, [_], _) = expr.kind
&& 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
}
}
69 changes: 40 additions & 29 deletions tests/ui/manual_bits.fixed
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
// run-rustfix

#![warn(clippy::manual_bits)]
#![allow(clippy::no_effect, path_statements, unused_must_use, clippy::unnecessary_operation)]
#![allow(
clippy::no_effect,
clippy::useless_conversion,
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;
i8::BITS as usize;
i16::BITS as usize;
i32::BITS as usize;
i64::BITS as usize;
i128::BITS as usize;
isize::BITS as usize;

u8::BITS as usize;
u16::BITS as usize;
u32::BITS as usize;
u64::BITS as usize;
u128::BITS as usize;
usize::BITS as usize;

i8::BITS as usize;
i16::BITS as usize;
i32::BITS as usize;
i64::BITS as usize;
i128::BITS as usize;
isize::BITS as usize;

u8::BITS as usize;
u16::BITS as usize;
u32::BITS as usize;
u64::BITS as usize;
u128::BITS as usize;
usize::BITS as usize;

size_of::<usize>() * 4;
4 * size_of::<usize>();
Expand All @@ -42,7 +48,12 @@ fn main() {
size_of_val(&0u32) * 8;

type Word = u32;
Word::BITS;
Word::BITS as usize;
type Bool = bool;
size_of::<Bool>() * 8;

let _: u32 = u128::BITS as u32;
let _: u32 = u128::BITS.try_into().unwrap();
let _ = (u128::BITS as usize).pow(5);
let _ = &(u128::BITS as usize);
}
13 changes: 12 additions & 1 deletion tests/ui/manual_bits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// run-rustfix

#![warn(clippy::manual_bits)]
#![allow(clippy::no_effect, path_statements, unused_must_use, clippy::unnecessary_operation)]
#![allow(
clippy::no_effect,
clippy::useless_conversion,
path_statements,
unused_must_use,
clippy::unnecessary_operation
)]

use std::mem::{size_of, size_of_val};

Expand Down Expand Up @@ -45,4 +51,9 @@ fn main() {
size_of::<Word>() * 8;
type Bool = bool;
size_of::<Bool>() * 8;

let _: u32 = (size_of::<u128>() * 8) as u32;
let _: u32 = (size_of::<u128>() * 8).try_into().unwrap();
let _ = (size_of::<u128>() * 8).pow(5);
let _ = &(size_of::<u128>() * 8);
}
Loading

0 comments on commit ecb3c3f

Please sign in to comment.