Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8ef8745

Browse files
committed
Minor cleanup on transmute lints
1 parent 7bb69c0 commit 8ef8745

11 files changed

+37
-38
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,9 @@ declare_lint_pass!(Transmute => [
372372
]);
373373

374374
impl<'tcx> LateLintPass<'tcx> for Transmute {
375-
#[allow(clippy::similar_names, clippy::too_many_lines)]
376375
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
377376
if_chain! {
378-
if let ExprKind::Call(path_expr, args) = e.kind;
377+
if let ExprKind::Call(path_expr, [arg]) = e.kind;
379378
if let ExprKind::Path(ref qpath) = path_expr.kind;
380379
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
381380
if cx.tcx.is_diagnostic_item(sym::transmute, def_id);
@@ -385,28 +384,28 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
385384
// And see https://github.com/rust-lang/rust/issues/51911 for dereferencing raw pointers.
386385
let const_context = in_constant(cx, e.hir_id);
387386

388-
let from_ty = cx.typeck_results().expr_ty(&args[0]);
387+
let from_ty = cx.typeck_results().expr_ty(arg);
389388
let to_ty = cx.typeck_results().expr_ty(e);
390389

391390
// If useless_transmute is triggered, the other lints can be skipped.
392-
if useless_transmute::check(cx, e, from_ty, to_ty, args) {
391+
if useless_transmute::check(cx, e, from_ty, to_ty, arg) {
393392
return;
394393
}
395394

396-
let mut linted = wrong_transmute::check(cx, e, from_ty, to_ty);
397-
linted |= crosspointer_transmute::check(cx, e, from_ty, to_ty);
398-
linted |= transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, args, qpath);
399-
linted |= transmute_int_to_char::check(cx, e, from_ty, to_ty, args);
400-
linted |= transmute_ref_to_ref::check(cx, e, from_ty, to_ty, args, const_context);
401-
linted |= transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, args);
402-
linted |= transmute_int_to_bool::check(cx, e, from_ty, to_ty, args);
403-
linted |= transmute_int_to_float::check(cx, e, from_ty, to_ty, args, const_context);
404-
linted |= transmute_float_to_int::check(cx, e, from_ty, to_ty, args, const_context);
405-
linted |= transmute_num_to_bytes::check(cx, e, from_ty, to_ty, args, const_context);
406-
linted |= unsound_collection_transmute::check(cx, e, from_ty, to_ty);
395+
let linted = wrong_transmute::check(cx, e, from_ty, to_ty)
396+
| crosspointer_transmute::check(cx, e, from_ty, to_ty)
397+
| transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, qpath)
398+
| transmute_int_to_char::check(cx, e, from_ty, to_ty, arg)
399+
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)
400+
| transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg)
401+
| transmute_int_to_bool::check(cx, e, from_ty, to_ty, arg)
402+
| transmute_int_to_float::check(cx, e, from_ty, to_ty, arg, const_context)
403+
| transmute_float_to_int::check(cx, e, from_ty, to_ty, arg, const_context)
404+
| transmute_num_to_bytes::check(cx, e, from_ty, to_ty, arg, const_context)
405+
| unsound_collection_transmute::check(cx, e, from_ty, to_ty);
407406

408407
if !linted {
409-
transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, to_ty, args);
408+
transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, to_ty, arg);
410409
}
411410
}
412411
}

clippy_lints/src/transmute/transmute_float_to_int.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
1515
e: &'tcx Expr<'_>,
1616
from_ty: Ty<'tcx>,
1717
to_ty: Ty<'tcx>,
18-
args: &'tcx [Expr<'_>],
18+
arg: &'tcx Expr<'_>,
1919
const_context: bool,
2020
) -> bool {
2121
match (&from_ty.kind(), &to_ty.kind()) {
@@ -26,7 +26,7 @@ pub(super) fn check<'tcx>(
2626
e.span,
2727
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
2828
|diag| {
29-
let mut expr = &args[0];
29+
let mut expr = arg;
3030
let mut arg = sugg::Sugg::hir(cx, expr, "..");
3131

3232
if let ExprKind::Unary(UnOp::Neg, inner_expr) = &expr.kind {

clippy_lints/src/transmute/transmute_int_to_bool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
1515
e: &'tcx Expr<'_>,
1616
from_ty: Ty<'tcx>,
1717
to_ty: Ty<'tcx>,
18-
args: &'tcx [Expr<'_>],
18+
arg: &'tcx Expr<'_>,
1919
) -> bool {
2020
match (&from_ty.kind(), &to_ty.kind()) {
2121
(ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
@@ -25,7 +25,7 @@ pub(super) fn check<'tcx>(
2525
e.span,
2626
&format!("transmute from a `{}` to a `bool`", from_ty),
2727
|diag| {
28-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
28+
let arg = sugg::Sugg::hir(cx, arg, "..");
2929
let zero = sugg::Sugg::NonParen(Cow::from("0"));
3030
diag.span_suggestion(
3131
e.span,

clippy_lints/src/transmute/transmute_int_to_char.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
1414
e: &'tcx Expr<'_>,
1515
from_ty: Ty<'tcx>,
1616
to_ty: Ty<'tcx>,
17-
args: &'tcx [Expr<'_>],
17+
arg: &'tcx Expr<'_>,
1818
) -> bool {
1919
match (&from_ty.kind(), &to_ty.kind()) {
2020
(ty::Int(ty::IntTy::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) => {
@@ -24,7 +24,7 @@ pub(super) fn check<'tcx>(
2424
e.span,
2525
&format!("transmute from a `{}` to a `char`", from_ty),
2626
|diag| {
27-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
27+
let arg = sugg::Sugg::hir(cx, arg, "..");
2828
let arg = if let ty::Int(_) = from_ty.kind() {
2929
arg.as_ty(ast::UintTy::U32.name_str())
3030
} else {

clippy_lints/src/transmute/transmute_int_to_float.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
1313
e: &'tcx Expr<'_>,
1414
from_ty: Ty<'tcx>,
1515
to_ty: Ty<'tcx>,
16-
args: &'tcx [Expr<'_>],
16+
arg: &'tcx Expr<'_>,
1717
const_context: bool,
1818
) -> bool {
1919
match (&from_ty.kind(), &to_ty.kind()) {
@@ -24,7 +24,7 @@ pub(super) fn check<'tcx>(
2424
e.span,
2525
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
2626
|diag| {
27-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
27+
let arg = sugg::Sugg::hir(cx, arg, "..");
2828
let arg = if let ty::Int(int_ty) = from_ty.kind() {
2929
arg.as_ty(format!(
3030
"u{}",

clippy_lints/src/transmute/transmute_num_to_bytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
1313
e: &'tcx Expr<'_>,
1414
from_ty: Ty<'tcx>,
1515
to_ty: Ty<'tcx>,
16-
args: &'tcx [Expr<'_>],
16+
arg: &'tcx Expr<'_>,
1717
const_context: bool,
1818
) -> bool {
1919
match (&from_ty.kind(), &to_ty.kind()) {
@@ -33,7 +33,7 @@ pub(super) fn check<'tcx>(
3333
e.span,
3434
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
3535
|diag| {
36-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
36+
let arg = sugg::Sugg::hir(cx, arg, "..");
3737
diag.span_suggestion(
3838
e.span,
3939
"consider using `to_ne_bytes()`",

clippy_lints/src/transmute/transmute_ptr_to_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
1313
e: &'tcx Expr<'_>,
1414
from_ty: Ty<'tcx>,
1515
to_ty: Ty<'tcx>,
16-
args: &'tcx [Expr<'_>],
16+
arg: &'tcx Expr<'_>,
1717
) -> bool {
1818
match (&from_ty.kind(), &to_ty.kind()) {
1919
(ty::RawPtr(_), ty::RawPtr(to_ty)) => {
@@ -23,7 +23,7 @@ pub(super) fn check<'tcx>(
2323
e.span,
2424
"transmute from a pointer to a pointer",
2525
|diag| {
26-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
26+
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
2727
let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
2828
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
2929
}

clippy_lints/src/transmute/transmute_ptr_to_ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
1414
e: &'tcx Expr<'_>,
1515
from_ty: Ty<'tcx>,
1616
to_ty: Ty<'tcx>,
17-
args: &'tcx [Expr<'_>],
17+
arg: &'tcx Expr<'_>,
1818
qpath: &'tcx QPath<'_>,
1919
) -> bool {
2020
match (&from_ty.kind(), &to_ty.kind()) {
@@ -28,7 +28,7 @@ pub(super) fn check<'tcx>(
2828
from_ty, to_ty
2929
),
3030
|diag| {
31-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
31+
let arg = sugg::Sugg::hir(cx, arg, "..");
3232
let (deref, cast) = if *mutbl == Mutability::Mut {
3333
("&mut *", "*mut")
3434
} else {

clippy_lints/src/transmute/transmute_ref_to_ref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
1515
e: &'tcx Expr<'_>,
1616
from_ty: Ty<'tcx>,
1717
to_ty: Ty<'tcx>,
18-
args: &'tcx [Expr<'_>],
18+
arg: &'tcx Expr<'_>,
1919
const_context: bool,
2020
) -> bool {
2121
let mut triggered = false;
@@ -41,7 +41,7 @@ pub(super) fn check<'tcx>(
4141
format!(
4242
"std::str::from_utf8{}({}).unwrap()",
4343
postfix,
44-
snippet(cx, args[0].span, ".."),
44+
snippet(cx, arg.span, ".."),
4545
),
4646
Applicability::Unspecified,
4747
);
@@ -54,7 +54,7 @@ pub(super) fn check<'tcx>(
5454
TRANSMUTE_PTR_TO_PTR,
5555
e.span,
5656
"transmute from a reference to a reference",
57-
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
57+
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
5858
let ty_from_and_mut = ty::TypeAndMut {
5959
ty: ty_from,
6060
mutbl: *from_mutbl

clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
1414
e: &'tcx Expr<'_>,
1515
from_ty: Ty<'tcx>,
1616
to_ty: Ty<'tcx>,
17-
args: &'tcx [Expr<'_>],
17+
arg: &'tcx Expr<'_>,
1818
) -> bool {
1919
if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) {
2020
span_lint_and_then(
@@ -26,7 +26,7 @@ pub(super) fn check<'tcx>(
2626
from_ty, to_ty
2727
),
2828
|diag| {
29-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
29+
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
3030
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
3131
diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
3232
}

0 commit comments

Comments
 (0)