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

Commit a383e03

Browse files
committed
Move cast_possible_wrap to its own module
1 parent 0975031 commit a383e03

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use rustc_hir::Expr;
2+
use rustc_lint::LateContext;
3+
use rustc_middle::ty::Ty;
4+
5+
use crate::utils::{is_isize_or_usize, span_lint};
6+
7+
use super::{utils, CAST_POSSIBLE_WRAP};
8+
9+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
10+
if !(cast_from.is_integral() && cast_to.is_integral()) {
11+
return;
12+
}
13+
14+
let arch_64_suffix = " on targets with 64-bit wide pointers";
15+
let arch_32_suffix = " on targets with 32-bit wide pointers";
16+
let cast_unsigned_to_signed = !cast_from.is_signed() && cast_to.is_signed();
17+
let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
18+
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
19+
20+
let (should_lint, suffix) = match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) {
21+
(true, true) | (false, false) => (to_nbits == from_nbits && cast_unsigned_to_signed, ""),
22+
(true, false) => (to_nbits <= 32 && cast_unsigned_to_signed, arch_32_suffix),
23+
(false, true) => (
24+
cast_unsigned_to_signed,
25+
if from_nbits == 64 {
26+
arch_64_suffix
27+
} else {
28+
arch_32_suffix
29+
},
30+
),
31+
};
32+
33+
if should_lint {
34+
span_lint(
35+
cx,
36+
CAST_POSSIBLE_WRAP,
37+
expr.span,
38+
&format!(
39+
"casting `{}` to `{}` may wrap around the value{}",
40+
cast_from, cast_to, suffix,
41+
),
42+
);
43+
}
44+
}

clippy_lints/src/casts/mod.rs

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod cast_lossless;
22
mod cast_possible_truncation;
3+
mod cast_possible_wrap;
34
mod cast_precision_loss;
45
mod cast_sign_loss;
56
mod utils;
@@ -20,8 +21,8 @@ use rustc_target::abi::LayoutOf;
2021

2122
use crate::utils::sugg::Sugg;
2223
use crate::utils::{
23-
is_hir_ty_cfg_dependant, is_isize_or_usize, meets_msrv, numeric_literal::NumericLiteral, snippet_opt,
24-
snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then,
24+
is_hir_ty_cfg_dependant, meets_msrv, numeric_literal::NumericLiteral, snippet_opt, snippet_with_applicability,
25+
span_lint, span_lint_and_sugg, span_lint_and_then,
2526
};
2627

2728
use utils::int_ty_to_nbits;
@@ -255,49 +256,6 @@ declare_clippy_lint! {
255256
"casting a function pointer to a numeric type not wide enough to store the address"
256257
}
257258

258-
enum ArchSuffix {
259-
_32,
260-
_64,
261-
None,
262-
}
263-
264-
fn check_truncation_and_wrapping(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
265-
let arch_64_suffix = " on targets with 64-bit wide pointers";
266-
let arch_32_suffix = " on targets with 32-bit wide pointers";
267-
let cast_unsigned_to_signed = !cast_from.is_signed() && cast_to.is_signed();
268-
let from_nbits = int_ty_to_nbits(cast_from, cx.tcx);
269-
let to_nbits = int_ty_to_nbits(cast_to, cx.tcx);
270-
let (span_wrap, suffix_wrap) = match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) {
271-
(true, true) | (false, false) => (to_nbits == from_nbits && cast_unsigned_to_signed, ArchSuffix::None),
272-
(true, false) => (to_nbits <= 32 && cast_unsigned_to_signed, ArchSuffix::_32),
273-
(false, true) => (
274-
cast_unsigned_to_signed,
275-
if from_nbits == 64 {
276-
ArchSuffix::_64
277-
} else {
278-
ArchSuffix::_32
279-
},
280-
),
281-
};
282-
if span_wrap {
283-
span_lint(
284-
cx,
285-
CAST_POSSIBLE_WRAP,
286-
expr.span,
287-
&format!(
288-
"casting `{}` to `{}` may wrap around the value{}",
289-
cast_from,
290-
cast_to,
291-
match suffix_wrap {
292-
ArchSuffix::_32 => arch_32_suffix,
293-
ArchSuffix::_64 => arch_64_suffix,
294-
ArchSuffix::None => "",
295-
}
296-
),
297-
);
298-
}
299-
}
300-
301259
declare_lint_pass!(Casts => [
302260
CAST_PRECISION_LOSS,
303261
CAST_SIGN_LOSS,
@@ -449,16 +407,10 @@ fn lint_numeric_casts<'tcx>(
449407
cast_to: Ty<'tcx>,
450408
) {
451409
cast_possible_truncation::check(cx, expr, cast_from, cast_to);
410+
cast_possible_wrap::check(cx, expr, cast_from, cast_to);
452411
cast_precision_loss::check(cx, expr, cast_from, cast_to);
453412
cast_lossless::check(cx, expr, cast_op, cast_from, cast_to);
454413
cast_sign_loss::check(cx, expr, cast_op, cast_from, cast_to);
455-
456-
match (cast_from.is_integral(), cast_to.is_integral()) {
457-
(true, true) => {
458-
check_truncation_and_wrapping(cx, expr, cast_from, cast_to);
459-
},
460-
(_, _) => {},
461-
}
462414
}
463415

464416
fn lint_cast_ptr_alignment<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, cast_from: Ty<'tcx>, cast_to: Ty<'tcx>) {

0 commit comments

Comments
 (0)