remove parenthesis from unnecessary_cast suggestion

This commit is contained in:
Andre Bogus 2022-08-27 12:04:19 +02:00
parent be8bd60000
commit 90fe3bea52
4 changed files with 39 additions and 2 deletions

View File

@ -90,13 +90,20 @@ pub(super) fn check<'tcx>(
fn lint_unnecessary_cast(cx: &LateContext<'_>, expr: &Expr<'_>, literal_str: &str, cast_from: Ty<'_>, cast_to: Ty<'_>) {
let literal_kind_name = if cast_from.is_integral() { "integer" } else { "float" };
let replaced_literal;
let matchless = if literal_str.contains(['(', ')']) {
replaced_literal = literal_str.replace(['(', ')'], "");
&replaced_literal
} else {
literal_str
};
span_lint_and_sugg(
cx,
UNNECESSARY_CAST,
expr.span,
&format!("casting {} literal to `{}` is unnecessary", literal_kind_name, cast_to),
"try",
format!("{}_{}", literal_str.trim_end_matches('.'), cast_to),
format!("{}_{}", matchless.trim_end_matches('.'), cast_to),
Applicability::MachineApplicable,
);
}

View File

@ -88,4 +88,13 @@ mod fixable {
}
type I32Alias = i32;
fn issue_9380() {
let _: i32 = -1_i32;
let _: f32 = -(1) as f32;
let _: i64 = -1_i64;
let _: i64 = -(1.0) as i64;
let _ = -(1 + 1) as i64;
}
}

View File

@ -88,4 +88,13 @@ mod fixable {
}
type I32Alias = i32;
fn issue_9380() {
let _: i32 = -(1) as i32;
let _: f32 = -(1) as f32;
let _: i64 = -(1) as i64;
let _: i64 = -(1.0) as i64;
let _ = -(1 + 1) as i64;
}
}

View File

@ -150,5 +150,17 @@ error: casting float literal to `f32` is unnecessary
LL | let _ = -1.0 as f32;
| ^^^^^^^^^^^ help: try: `-1.0_f32`
error: aborting due to 25 previous errors
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:93:22
|
LL | let _: i32 = -(1) as i32;
| ^^^^^^^^^^^ help: try: `-1_i32`
error: casting integer literal to `i64` is unnecessary
--> $DIR/unnecessary_cast.rs:95:22
|
LL | let _: i64 = -(1) as i64;
| ^^^^^^^^^^^ help: try: `-1_i64`
error: aborting due to 27 previous errors