File tree 4 files changed +37
-2
lines changed
4 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -31,8 +31,10 @@ pub(super) fn check<'tcx>(
31
31
}
32
32
}
33
33
34
+ let cast_str = snippet_opt ( cx, cast_expr. span ) . unwrap_or_default ( ) ;
35
+
34
36
if let Some ( lit) = get_numeric_literal ( cast_expr) {
35
- let literal_str = snippet_opt ( cx , cast_expr . span ) . unwrap_or_default ( ) ;
37
+ let literal_str = cast_str ;
36
38
37
39
if_chain ! {
38
40
if let LitKind :: Int ( n, _) = lit. node;
@@ -81,6 +83,17 @@ pub(super) fn check<'tcx>(
81
83
}
82
84
} ,
83
85
}
86
+ } else if cast_from. kind ( ) == cast_to. kind ( ) && !in_external_macro ( cx. sess ( ) , expr. span ) {
87
+ span_lint_and_sugg (
88
+ cx,
89
+ UNNECESSARY_CAST ,
90
+ expr. span ,
91
+ & format ! ( "casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)" ) ,
92
+ "try" ,
93
+ cast_str,
94
+ Applicability :: MachineApplicable ,
95
+ ) ;
96
+ return true ;
84
97
}
85
98
86
99
false
Original file line number Diff line number Diff line change @@ -103,4 +103,12 @@ mod fixable {
103
103
#[allow(clippy::precedence)]
104
104
let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
105
105
}
106
+
107
+ fn issue_9562_non_literal() {
108
+ fn foo() -> f32 {
109
+ 0.
110
+ }
111
+
112
+ let _num = foo();
113
+ }
106
114
}
Original file line number Diff line number Diff line change @@ -103,4 +103,12 @@ mod fixable {
103
103
#[ allow( clippy:: precedence) ]
104
104
let _: f64 = -( 8.0 as f64 ) . exp ( ) ; // should suggest `-8.0_f64.exp()` here not to change code behavior
105
105
}
106
+
107
+ fn issue_9562_non_literal ( ) {
108
+ fn foo ( ) -> f32 {
109
+ 0.
110
+ }
111
+
112
+ let _num = foo ( ) as f32 ;
113
+ }
106
114
}
Original file line number Diff line number Diff line change @@ -174,5 +174,11 @@ error: casting float literal to `f64` is unnecessary
174
174
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
175
175
| ^^^^^^^^^^^^ help: try: `8.0_f64`
176
176
177
- error: aborting due to 29 previous errors
177
+ error: casting to the same type is unnecessary (`f32` -> `f32`)
178
+ --> $DIR/unnecessary_cast.rs:112:20
179
+ |
180
+ LL | let _num = foo() as f32;
181
+ | ^^^^^^^^^^^^ help: try: `foo()`
182
+
183
+ error: aborting due to 30 previous errors
178
184
You can’t perform that action at this time.
0 commit comments