Skip to content

Commit 846c9b8

Browse files
committed
Auto merge of #9996 - Jarcho:issue_9906, r=Alexendoo
Fix `unnecessary_cast` suggestion when taking a reference fixes #9906 changelog: `unnecessary_cast`: Fix suggestion when taking a reference
2 parents d7d098a + 73f4546 commit 846c9b8

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ pub(super) fn check<'tcx>(
9090
expr.span,
9191
&format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
9292
"try",
93-
cast_str,
93+
if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::AddrOf(..))) {
94+
format!("{{ {cast_str} }}")
95+
} else {
96+
cast_str
97+
},
9498
Applicability::MachineApplicable,
9599
);
96100
return true;

tests/ui/unnecessary_cast.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ mod fixable {
9696

9797
let _ = 1 as I32Alias;
9898
let _ = &1 as &I32Alias;
99+
100+
let x = 1i32;
101+
let _ = &{ x };
99102
}
100103

101104
type I32Alias = i32;

tests/ui/unnecessary_cast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ mod fixable {
9696

9797
let _ = 1 as I32Alias;
9898
let _ = &1 as &I32Alias;
99+
100+
let x = 1i32;
101+
let _ = &(x as i32);
99102
}
100103

101104
type I32Alias = i32;

tests/ui/unnecessary_cast.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,35 +150,41 @@ error: casting float literal to `f32` is unnecessary
150150
LL | let _ = -1.0 as f32;
151151
| ^^^^^^^^^^^ help: try: `-1.0_f32`
152152

153+
error: casting to the same type is unnecessary (`i32` -> `i32`)
154+
--> $DIR/unnecessary_cast.rs:101:18
155+
|
156+
LL | let _ = &(x as i32);
157+
| ^^^^^^^^^^ help: try: `{ x }`
158+
153159
error: casting integer literal to `i32` is unnecessary
154-
--> $DIR/unnecessary_cast.rs:104:22
160+
--> $DIR/unnecessary_cast.rs:107:22
155161
|
156162
LL | let _: i32 = -(1) as i32;
157163
| ^^^^^^^^^^^ help: try: `-1_i32`
158164

159165
error: casting integer literal to `i64` is unnecessary
160-
--> $DIR/unnecessary_cast.rs:106:22
166+
--> $DIR/unnecessary_cast.rs:109:22
161167
|
162168
LL | let _: i64 = -(1) as i64;
163169
| ^^^^^^^^^^^ help: try: `-1_i64`
164170

165171
error: casting float literal to `f64` is unnecessary
166-
--> $DIR/unnecessary_cast.rs:113:22
172+
--> $DIR/unnecessary_cast.rs:116:22
167173
|
168174
LL | let _: f64 = (-8.0 as f64).exp();
169175
| ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
170176

171177
error: casting float literal to `f64` is unnecessary
172-
--> $DIR/unnecessary_cast.rs:115:23
178+
--> $DIR/unnecessary_cast.rs:118:23
173179
|
174180
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
175181
| ^^^^^^^^^^^^ help: try: `8.0_f64`
176182

177183
error: casting to the same type is unnecessary (`f32` -> `f32`)
178-
--> $DIR/unnecessary_cast.rs:123:20
184+
--> $DIR/unnecessary_cast.rs:126:20
179185
|
180186
LL | let _num = foo() as f32;
181187
| ^^^^^^^^^^^^ help: try: `foo()`
182188

183-
error: aborting due to 30 previous errors
189+
error: aborting due to 31 previous errors
184190

0 commit comments

Comments
 (0)