Skip to content

Commit 6a0105e

Browse files
committed
Auto merge of #4026 - cemiloten:working-on-#3981, r=flip1995
Attempt to fix #3981 Fixes #3981 Hi, hopefully this is correct, happy to have feedback. changelog: Don't trigger `unnecessary_cast` inside a macro
2 parents 8c0e038 + 8eae2d3 commit 6a0105e

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

clippy_lints/src/types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,9 @@ fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 {
11101110

11111111
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
11121112
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
1113+
if in_macro(expr.span) {
1114+
return;
1115+
}
11131116
if let ExprKind::Cast(ref ex, _) = expr.node {
11141117
let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr));
11151118
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);

tests/ui/cast.rs

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ fn main() {
4848
1f32 as f32;
4949
false as bool;
5050
&1i32 as &i32;
51+
// macro version
52+
macro_rules! foo {
53+
($a:ident, $b:ident) => {
54+
pub fn $a() -> $b {
55+
1 as $b
56+
}
57+
};
58+
}
59+
foo!(a, i32);
60+
foo!(b, f32);
61+
foo!(c, f64);
62+
5163
// casting integer literal to float is unnecessary
5264
100 as f32;
5365
100 as f64;

tests/ui/cast.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,19 @@ LL | false as bool;
159159
| ^^^^^^^^^^^^^
160160

161161
error: casting integer literal to f32 is unnecessary
162-
--> $DIR/cast.rs:52:5
162+
--> $DIR/cast.rs:64:5
163163
|
164164
LL | 100 as f32;
165165
| ^^^^^^^^^^ help: try: `100_f32`
166166

167167
error: casting integer literal to f64 is unnecessary
168-
--> $DIR/cast.rs:53:5
168+
--> $DIR/cast.rs:65:5
169169
|
170170
LL | 100 as f64;
171171
| ^^^^^^^^^^ help: try: `100_f64`
172172

173173
error: casting integer literal to f64 is unnecessary
174-
--> $DIR/cast.rs:54:5
174+
--> $DIR/cast.rs:66:5
175175
|
176176
LL | 100_i32 as f64;
177177
| ^^^^^^^^^^^^^^ help: try: `100_f64`

0 commit comments

Comments
 (0)