Skip to content

Commit 08dac85

Browse files
committed
Auto merge of #12631 - franciscoBSalgueiro:11738, r=blyxyas
Allow `cast` lints in macros closes: #11738 Removed the `from_expansion` guard clause for cast lints, so that these warnings can be generated for internal macros. changelog: allow `cast` lints in macros
2 parents 8253040 + ac225a3 commit 08dac85

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

clippy_lints/src/casts/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,10 @@ impl_lint_pass!(Casts => [
754754

755755
impl<'tcx> LateLintPass<'tcx> for Casts {
756756
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
757-
if !in_external_macro(cx.sess(), expr.span) {
758-
ptr_as_ptr::check(cx, expr, &self.msrv);
759-
}
760-
761-
if expr.span.from_expansion() {
757+
if in_external_macro(cx.sess(), expr.span) {
762758
return;
763759
}
760+
ptr_as_ptr::check(cx, expr, &self.msrv);
764761

765762
if let ExprKind::Cast(cast_expr, cast_to_hir) = expr.kind {
766763
if is_hir_ty_cfg_dependant(cx, cast_to_hir) {
@@ -771,7 +768,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
771768
cx.typeck_results().expr_ty(expr),
772769
);
773770

774-
if unnecessary_cast::check(cx, expr, cast_expr, cast_from, cast_to) {
771+
if !expr.span.from_expansion() && unnecessary_cast::check(cx, expr, cast_expr, cast_from, cast_to) {
775772
return;
776773
}
777774
cast_slice_from_raw_parts::check(cx, expr, cast_expr, cast_to, &self.msrv);
@@ -782,7 +779,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
782779
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_expr, cast_from, cast_to);
783780
zero_ptr::check(cx, expr, cast_expr, cast_to_hir);
784781

785-
if cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
782+
if cast_to.is_numeric() {
786783
cast_possible_truncation::check(cx, expr, cast_expr, cast_from, cast_to, cast_to_hir.span);
787784
if cast_from.is_numeric() {
788785
cast_possible_wrap::check(cx, expr, cast_from, cast_to);

tests/ui/cast.rs

+12
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,18 @@ fn issue11642() {
463463
}
464464
}
465465

466+
fn issue11738() {
467+
macro_rules! m {
468+
() => {
469+
let _ = i32::MIN as u32; // cast_sign_loss
470+
let _ = u32::MAX as u8; // cast_possible_truncation
471+
let _ = std::f64::consts::PI as f32; // cast_possible_truncation
472+
let _ = 0i8 as i32; // cast_lossless
473+
};
474+
}
475+
m!();
476+
}
477+
466478
fn issue12506() -> usize {
467479
let bar: Result<Option<i64>, u32> = Ok(Some(10));
468480
bar.unwrap().unwrap() as usize

tests/ui/cast.stderr

+42-3
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,47 @@ error: casting `i32` to `u32` may lose the sign of the value
650650
LL | (a.abs() * b.pow(2) / c.abs()) as u32
651651
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
652652

653+
error: casting `i32` to `u32` may lose the sign of the value
654+
--> tests/ui/cast.rs:469:21
655+
|
656+
LL | let _ = i32::MIN as u32; // cast_sign_loss
657+
| ^^^^^^^^^^^^^^^
658+
...
659+
LL | m!();
660+
| ---- in this macro invocation
661+
|
662+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
663+
664+
error: casting `u32` to `u8` may truncate the value
665+
--> tests/ui/cast.rs:470:21
666+
|
667+
LL | let _ = u32::MAX as u8; // cast_possible_truncation
668+
| ^^^^^^^^^^^^^^
669+
...
670+
LL | m!();
671+
| ---- in this macro invocation
672+
|
673+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
674+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
675+
help: ... or use `try_from` and handle the error accordingly
676+
|
677+
LL | let _ = u8::try_from(u32::MAX); // cast_possible_truncation
678+
| ~~~~~~~~~~~~~~~~~~~~~~
679+
680+
error: casting `f64` to `f32` may truncate the value
681+
--> tests/ui/cast.rs:471:21
682+
|
683+
LL | let _ = std::f64::consts::PI as f32; // cast_possible_truncation
684+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
685+
...
686+
LL | m!();
687+
| ---- in this macro invocation
688+
|
689+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
690+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
691+
653692
error: casting `i64` to `usize` may truncate the value on targets with 32-bit wide pointers
654-
--> tests/ui/cast.rs:468:5
693+
--> tests/ui/cast.rs:480:5
655694
|
656695
LL | bar.unwrap().unwrap() as usize
657696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -663,10 +702,10 @@ LL | usize::try_from(bar.unwrap().unwrap())
663702
|
664703

665704
error: casting `i64` to `usize` may lose the sign of the value
666-
--> tests/ui/cast.rs:468:5
705+
--> tests/ui/cast.rs:480:5
667706
|
668707
LL | bar.unwrap().unwrap() as usize
669708
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
670709

671-
error: aborting due to 87 previous errors
710+
error: aborting due to 90 previous errors
672711

0 commit comments

Comments
 (0)