|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::get_parent_expr; |
2 | 3 | use clippy_utils::numeric_literal::NumericLiteral; |
3 | 4 | use clippy_utils::source::snippet_opt; |
4 | 5 | use if_chain::if_chain; |
@@ -85,22 +86,38 @@ pub(super) fn check<'tcx>( |
85 | 86 | false |
86 | 87 | } |
87 | 88 |
|
88 | | -fn lint_unnecessary_cast(cx: &LateContext<'_>, expr: &Expr<'_>, literal_str: &str, cast_from: Ty<'_>, cast_to: Ty<'_>) { |
| 89 | +fn lint_unnecessary_cast( |
| 90 | + cx: &LateContext<'_>, |
| 91 | + expr: &Expr<'_>, |
| 92 | + raw_literal_str: &str, |
| 93 | + cast_from: Ty<'_>, |
| 94 | + cast_to: Ty<'_>, |
| 95 | +) { |
89 | 96 | let literal_kind_name = if cast_from.is_integral() { "integer" } else { "float" }; |
90 | | - let replaced_literal; |
91 | | - let matchless = if literal_str.contains(['(', ')']) { |
92 | | - replaced_literal = literal_str.replace(['(', ')'], ""); |
93 | | - &replaced_literal |
94 | | - } else { |
95 | | - literal_str |
| 97 | + // first we remove all matches so `-(1)` become `-1`, and remove trailing dots, so `1.` become `1` |
| 98 | + let literal_str = raw_literal_str |
| 99 | + .replace(['(', ')'], "") |
| 100 | + .trim_end_matches('.') |
| 101 | + .to_string(); |
| 102 | + // we know need to check if the parent is a method call, to add parenthesis accordingly (eg: |
| 103 | + // (-1).foo() instead of -1.foo()) |
| 104 | + let sugg = if let Some(parent_expr) = get_parent_expr(cx, expr) |
| 105 | + && let ExprKind::MethodCall(..) = parent_expr.kind |
| 106 | + && literal_str.starts_with('-') |
| 107 | + { |
| 108 | + format!("({literal_str}_{cast_to})") |
| 109 | + |
| 110 | + } else { |
| 111 | + format!("{literal_str}_{cast_to}") |
96 | 112 | }; |
| 113 | + |
97 | 114 | span_lint_and_sugg( |
98 | 115 | cx, |
99 | 116 | UNNECESSARY_CAST, |
100 | 117 | expr.span, |
101 | 118 | &format!("casting {literal_kind_name} literal to `{cast_to}` is unnecessary"), |
102 | 119 | "try", |
103 | | - format!("{}_{cast_to}", matchless.trim_end_matches('.')), |
| 120 | + sugg, |
104 | 121 | Applicability::MachineApplicable, |
105 | 122 | ); |
106 | 123 | } |
|
0 commit comments