Skip to content

Commit e9a8b8c

Browse files
committed
Auto merge of rust-lang#9980 - Jarcho:issue_9960, r=xFrednet
Don't lint `unnecessary_cast` in mixed macro context fixes rust-lang#9960 Time to start making a dent in this onslaught. changelog: `unnecessary_cast`: Don't lint when the identifiers context differs from its binding's context for locals
2 parents 1207480 + f44b7aa commit e9a8b8c

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::get_parent_expr;
32
use clippy_utils::numeric_literal::NumericLiteral;
43
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::{get_parent_expr, path_to_local};
55
use if_chain::if_chain;
66
use rustc_ast::{LitFloatType, LitIntType, LitKind};
77
use rustc_errors::Applicability;
@@ -75,6 +75,15 @@ pub(super) fn check<'tcx>(
7575
}
7676

7777
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
78+
if let Some(id) = path_to_local(cast_expr)
79+
&& let Some(span) = cx.tcx.hir().opt_span(id)
80+
&& span.ctxt() != cast_expr.span.ctxt()
81+
{
82+
// Binding context is different than the identifiers context.
83+
// Weird macro wizardry could be involved here.
84+
return false;
85+
}
86+
7887
span_lint_and_sugg(
7988
cx,
8089
UNNECESSARY_CAST,

tests/ui/unnecessary_cast.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ fn main() {
4141
// do not lint cast to alias type
4242
1 as I32Alias;
4343
&1 as &I32Alias;
44+
45+
// issue #9960
46+
macro_rules! bind_var {
47+
($id:ident, $e:expr) => {{
48+
let $id = 0usize;
49+
let _ = $e != 0usize;
50+
let $id = 0isize;
51+
let _ = $e != 0usize;
52+
}}
53+
}
54+
bind_var!(x, (x as usize) + 1);
4455
}
4556

4657
type I32Alias = i32;

tests/ui/unnecessary_cast.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ fn main() {
4141
// do not lint cast to alias type
4242
1 as I32Alias;
4343
&1 as &I32Alias;
44+
45+
// issue #9960
46+
macro_rules! bind_var {
47+
($id:ident, $e:expr) => {{
48+
let $id = 0usize;
49+
let _ = $e != 0usize;
50+
let $id = 0isize;
51+
let _ = $e != 0usize;
52+
}}
53+
}
54+
bind_var!(x, (x as usize) + 1);
4455
}
4556

4657
type I32Alias = i32;

tests/ui/unnecessary_cast.stderr

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,133 +49,133 @@ LL | 1_f32 as f32;
4949
| ^^^^^^^^^^^^ help: try: `1_f32`
5050

5151
error: casting integer literal to `f32` is unnecessary
52-
--> $DIR/unnecessary_cast.rs:53:9
52+
--> $DIR/unnecessary_cast.rs:64:9
5353
|
5454
LL | 100 as f32;
5555
| ^^^^^^^^^^ help: try: `100_f32`
5656

5757
error: casting integer literal to `f64` is unnecessary
58-
--> $DIR/unnecessary_cast.rs:54:9
58+
--> $DIR/unnecessary_cast.rs:65:9
5959
|
6060
LL | 100 as f64;
6161
| ^^^^^^^^^^ help: try: `100_f64`
6262

6363
error: casting integer literal to `f64` is unnecessary
64-
--> $DIR/unnecessary_cast.rs:55:9
64+
--> $DIR/unnecessary_cast.rs:66:9
6565
|
6666
LL | 100_i32 as f64;
6767
| ^^^^^^^^^^^^^^ help: try: `100_f64`
6868

6969
error: casting integer literal to `f32` is unnecessary
70-
--> $DIR/unnecessary_cast.rs:56:17
70+
--> $DIR/unnecessary_cast.rs:67:17
7171
|
7272
LL | let _ = -100 as f32;
7373
| ^^^^^^^^^^^ help: try: `-100_f32`
7474

7575
error: casting integer literal to `f64` is unnecessary
76-
--> $DIR/unnecessary_cast.rs:57:17
76+
--> $DIR/unnecessary_cast.rs:68:17
7777
|
7878
LL | let _ = -100 as f64;
7979
| ^^^^^^^^^^^ help: try: `-100_f64`
8080

8181
error: casting integer literal to `f64` is unnecessary
82-
--> $DIR/unnecessary_cast.rs:58:17
82+
--> $DIR/unnecessary_cast.rs:69:17
8383
|
8484
LL | let _ = -100_i32 as f64;
8585
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
8686

8787
error: casting float literal to `f32` is unnecessary
88-
--> $DIR/unnecessary_cast.rs:59:9
88+
--> $DIR/unnecessary_cast.rs:70:9
8989
|
9090
LL | 100. as f32;
9191
| ^^^^^^^^^^^ help: try: `100_f32`
9292

9393
error: casting float literal to `f64` is unnecessary
94-
--> $DIR/unnecessary_cast.rs:60:9
94+
--> $DIR/unnecessary_cast.rs:71:9
9595
|
9696
LL | 100. as f64;
9797
| ^^^^^^^^^^^ help: try: `100_f64`
9898

9999
error: casting integer literal to `u32` is unnecessary
100-
--> $DIR/unnecessary_cast.rs:72:9
100+
--> $DIR/unnecessary_cast.rs:83:9
101101
|
102102
LL | 1 as u32;
103103
| ^^^^^^^^ help: try: `1_u32`
104104

105105
error: casting integer literal to `i32` is unnecessary
106-
--> $DIR/unnecessary_cast.rs:73:9
106+
--> $DIR/unnecessary_cast.rs:84:9
107107
|
108108
LL | 0x10 as i32;
109109
| ^^^^^^^^^^^ help: try: `0x10_i32`
110110

111111
error: casting integer literal to `usize` is unnecessary
112-
--> $DIR/unnecessary_cast.rs:74:9
112+
--> $DIR/unnecessary_cast.rs:85:9
113113
|
114114
LL | 0b10 as usize;
115115
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
116116

117117
error: casting integer literal to `u16` is unnecessary
118-
--> $DIR/unnecessary_cast.rs:75:9
118+
--> $DIR/unnecessary_cast.rs:86:9
119119
|
120120
LL | 0o73 as u16;
121121
| ^^^^^^^^^^^ help: try: `0o73_u16`
122122

123123
error: casting integer literal to `u32` is unnecessary
124-
--> $DIR/unnecessary_cast.rs:76:9
124+
--> $DIR/unnecessary_cast.rs:87:9
125125
|
126126
LL | 1_000_000_000 as u32;
127127
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
128128

129129
error: casting float literal to `f64` is unnecessary
130-
--> $DIR/unnecessary_cast.rs:78:9
130+
--> $DIR/unnecessary_cast.rs:89:9
131131
|
132132
LL | 1.0 as f64;
133133
| ^^^^^^^^^^ help: try: `1.0_f64`
134134

135135
error: casting float literal to `f32` is unnecessary
136-
--> $DIR/unnecessary_cast.rs:79:9
136+
--> $DIR/unnecessary_cast.rs:90:9
137137
|
138138
LL | 0.5 as f32;
139139
| ^^^^^^^^^^ help: try: `0.5_f32`
140140

141141
error: casting integer literal to `i32` is unnecessary
142-
--> $DIR/unnecessary_cast.rs:83:17
142+
--> $DIR/unnecessary_cast.rs:94:17
143143
|
144144
LL | let _ = -1 as i32;
145145
| ^^^^^^^^^ help: try: `-1_i32`
146146

147147
error: casting float literal to `f32` is unnecessary
148-
--> $DIR/unnecessary_cast.rs:84:17
148+
--> $DIR/unnecessary_cast.rs:95:17
149149
|
150150
LL | let _ = -1.0 as f32;
151151
| ^^^^^^^^^^^ help: try: `-1.0_f32`
152152

153153
error: casting integer literal to `i32` is unnecessary
154-
--> $DIR/unnecessary_cast.rs:93:22
154+
--> $DIR/unnecessary_cast.rs:104:22
155155
|
156156
LL | let _: i32 = -(1) as i32;
157157
| ^^^^^^^^^^^ help: try: `-1_i32`
158158

159159
error: casting integer literal to `i64` is unnecessary
160-
--> $DIR/unnecessary_cast.rs:95:22
160+
--> $DIR/unnecessary_cast.rs:106:22
161161
|
162162
LL | let _: i64 = -(1) as i64;
163163
| ^^^^^^^^^^^ help: try: `-1_i64`
164164

165165
error: casting float literal to `f64` is unnecessary
166-
--> $DIR/unnecessary_cast.rs:102:22
166+
--> $DIR/unnecessary_cast.rs:113:22
167167
|
168168
LL | let _: f64 = (-8.0 as f64).exp();
169169
| ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
170170

171171
error: casting float literal to `f64` is unnecessary
172-
--> $DIR/unnecessary_cast.rs:104:23
172+
--> $DIR/unnecessary_cast.rs:115:23
173173
|
174174
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
175175
| ^^^^^^^^^^^^ help: try: `8.0_f64`
176176

177177
error: casting to the same type is unnecessary (`f32` -> `f32`)
178-
--> $DIR/unnecessary_cast.rs:112:20
178+
--> $DIR/unnecessary_cast.rs:123:20
179179
|
180180
LL | let _num = foo() as f32;
181181
| ^^^^^^^^^^^^ help: try: `foo()`

0 commit comments

Comments
 (0)