diff options
| author | bors <bors@rust-lang.org> | 2019-04-25 10:37:30 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-04-25 10:37:30 +0000 |
| commit | 6a0105e59f6b07236f894e4f4bdf05a9592d93c1 (patch) | |
| tree | 0fd7eb47cb05e7ec247095243b58422d65823e0a | |
| parent | 8c0e038f6f25e0d8db8e24bcd37b4b11ca9665c6 (diff) | |
| parent | 8eae2d37072ad3f39a5fabae39652f09c77455e4 (diff) | |
| download | rust-6a0105e59f6b07236f894e4f4bdf05a9592d93c1.tar.gz rust-6a0105e59f6b07236f894e4f4bdf05a9592d93c1.zip | |
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
| -rw-r--r-- | clippy_lints/src/types.rs | 3 | ||||
| -rw-r--r-- | tests/ui/cast.rs | 12 | ||||
| -rw-r--r-- | tests/ui/cast.stderr | 6 |
3 files changed, 18 insertions, 3 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 21b6fc2fee6..d69e1353418 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -1110,6 +1110,9 @@ fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { + if in_macro(expr.span) { + return; + } if let ExprKind::Cast(ref ex, _) = expr.node { let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr)); lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to); diff --git a/tests/ui/cast.rs b/tests/ui/cast.rs index 914296644e6..16da099bd21 100644 --- a/tests/ui/cast.rs +++ b/tests/ui/cast.rs @@ -48,6 +48,18 @@ fn main() { 1f32 as f32; false as bool; &1i32 as &i32; + // macro version + macro_rules! foo { + ($a:ident, $b:ident) => { + pub fn $a() -> $b { + 1 as $b + } + }; + } + foo!(a, i32); + foo!(b, f32); + foo!(c, f64); + // casting integer literal to float is unnecessary 100 as f32; 100 as f64; diff --git a/tests/ui/cast.stderr b/tests/ui/cast.stderr index 28bcfacd858..42232808c9b 100644 --- a/tests/ui/cast.stderr +++ b/tests/ui/cast.stderr @@ -159,19 +159,19 @@ LL | false as bool; | ^^^^^^^^^^^^^ error: casting integer literal to f32 is unnecessary - --> $DIR/cast.rs:52:5 + --> $DIR/cast.rs:64:5 | LL | 100 as f32; | ^^^^^^^^^^ help: try: `100_f32` error: casting integer literal to f64 is unnecessary - --> $DIR/cast.rs:53:5 + --> $DIR/cast.rs:65:5 | LL | 100 as f64; | ^^^^^^^^^^ help: try: `100_f64` error: casting integer literal to f64 is unnecessary - --> $DIR/cast.rs:54:5 + --> $DIR/cast.rs:66:5 | LL | 100_i32 as f64; | ^^^^^^^^^^^^^^ help: try: `100_f64` |
