Skip to content

Commit 90fe3be

Browse files
committed
remove parenthesis from unnecessary_cast suggestion
1 parent be8bd60 commit 90fe3be

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,20 @@ pub(super) fn check<'tcx>(
9090

9191
fn lint_unnecessary_cast(cx: &LateContext<'_>, expr: &Expr<'_>, literal_str: &str, cast_from: Ty<'_>, cast_to: Ty<'_>) {
9292
let literal_kind_name = if cast_from.is_integral() { "integer" } else { "float" };
93+
let replaced_literal;
94+
let matchless = if literal_str.contains(['(', ')']) {
95+
replaced_literal = literal_str.replace(['(', ')'], "");
96+
&replaced_literal
97+
} else {
98+
literal_str
99+
};
93100
span_lint_and_sugg(
94101
cx,
95102
UNNECESSARY_CAST,
96103
expr.span,
97104
&format!("casting {} literal to `{}` is unnecessary", literal_kind_name, cast_to),
98105
"try",
99-
format!("{}_{}", literal_str.trim_end_matches('.'), cast_to),
106+
format!("{}_{}", matchless.trim_end_matches('.'), cast_to),
100107
Applicability::MachineApplicable,
101108
);
102109
}

tests/ui/unnecessary_cast.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,13 @@ mod fixable {
8888
}
8989

9090
type I32Alias = i32;
91+
92+
fn issue_9380() {
93+
let _: i32 = -1_i32;
94+
let _: f32 = -(1) as f32;
95+
let _: i64 = -1_i64;
96+
let _: i64 = -(1.0) as i64;
97+
98+
let _ = -(1 + 1) as i64;
99+
}
91100
}

tests/ui/unnecessary_cast.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,13 @@ mod fixable {
8888
}
8989

9090
type I32Alias = i32;
91+
92+
fn issue_9380() {
93+
let _: i32 = -(1) as i32;
94+
let _: f32 = -(1) as f32;
95+
let _: i64 = -(1) as i64;
96+
let _: i64 = -(1.0) as i64;
97+
98+
let _ = -(1 + 1) as i64;
99+
}
91100
}

tests/ui/unnecessary_cast.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,17 @@ error: casting float literal to `f32` is unnecessary
150150
LL | let _ = -1.0 as f32;
151151
| ^^^^^^^^^^^ help: try: `-1.0_f32`
152152

153-
error: aborting due to 25 previous errors
153+
error: casting integer literal to `i32` is unnecessary
154+
--> $DIR/unnecessary_cast.rs:93:22
155+
|
156+
LL | let _: i32 = -(1) as i32;
157+
| ^^^^^^^^^^^ help: try: `-1_i32`
158+
159+
error: casting integer literal to `i64` is unnecessary
160+
--> $DIR/unnecessary_cast.rs:95:22
161+
|
162+
LL | let _: i64 = -(1) as i64;
163+
| ^^^^^^^^^^^ help: try: `-1_i64`
164+
165+
error: aborting due to 27 previous errors
154166

0 commit comments

Comments
 (0)