|
1 | 1 | use clippy_config::Conf; |
2 | | -use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; |
| 2 | +use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; |
3 | 3 | use clippy_utils::msrvs::{self, Msrv}; |
4 | 4 | use clippy_utils::res::{MaybeDef, MaybeTypeckRes}; |
5 | 5 | use clippy_utils::sugg::Sugg; |
@@ -109,36 +109,16 @@ impl LateLintPass<'_> for UncheckedTimeSubtraction { |
109 | 109 | && !expr.span.from_expansion() |
110 | 110 | && self.msrv.meets(cx, msrvs::TRY_FROM) |
111 | 111 | { |
112 | | - // For chained subtraction like (instant - dur1) - dur2, avoid suggestions |
113 | | - if is_chained_time_subtraction(cx, lhs) { |
114 | | - span_lint( |
115 | | - cx, |
116 | | - UNCHECKED_TIME_SUBTRACTION, |
117 | | - expr.span, |
118 | | - "unchecked subtraction of a 'Duration' from an 'Instant'", |
119 | | - ); |
120 | | - } else { |
121 | | - // instant - duration |
122 | | - print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr); |
123 | | - } |
| 112 | + print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr); |
124 | 113 | } |
125 | | - } else if lhs_ty.is_diag_item(cx, sym::Duration) |
| 114 | + } |
| 115 | + // duration - duration |
| 116 | + else if lhs_ty.is_diag_item(cx, sym::Duration) |
126 | 117 | && rhs_ty.is_diag_item(cx, sym::Duration) |
127 | 118 | && !expr.span.from_expansion() |
128 | 119 | && self.msrv.meets(cx, msrvs::TRY_FROM) |
129 | 120 | { |
130 | | - // For chained subtraction like (dur1 - dur2) - dur3, avoid suggestions |
131 | | - if is_chained_time_subtraction(cx, lhs) { |
132 | | - span_lint( |
133 | | - cx, |
134 | | - UNCHECKED_TIME_SUBTRACTION, |
135 | | - expr.span, |
136 | | - "unchecked subtraction between 'Duration' values", |
137 | | - ); |
138 | | - } else { |
139 | | - // duration - duration |
140 | | - print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr); |
141 | | - } |
| 121 | + print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr); |
142 | 122 | } |
143 | 123 | } |
144 | 124 | } |
@@ -191,26 +171,26 @@ fn print_unchecked_duration_subtraction_sugg( |
191 | 171 | right_expr: &Expr<'_>, |
192 | 172 | expr: &Expr<'_>, |
193 | 173 | ) { |
194 | | - let typeck = cx.typeck_results(); |
195 | | - let left_ty = typeck.expr_ty(left_expr); |
196 | | - |
197 | | - let lint_msg = if left_ty.is_diag_item(cx, sym::Instant) { |
198 | | - "unchecked subtraction of a 'Duration' from an 'Instant'" |
199 | | - } else { |
200 | | - "unchecked subtraction between 'Duration' values" |
201 | | - }; |
202 | | - |
203 | | - let mut applicability = Applicability::MachineApplicable; |
204 | | - let left_sugg = Sugg::hir_with_applicability(cx, left_expr, "<left>", &mut applicability); |
205 | | - let right_sugg = Sugg::hir_with_applicability(cx, right_expr, "<right>", &mut applicability); |
206 | | - |
207 | | - span_lint_and_sugg( |
| 174 | + span_lint_and_then( |
208 | 175 | cx, |
209 | 176 | UNCHECKED_TIME_SUBTRACTION, |
210 | 177 | expr.span, |
211 | | - lint_msg, |
212 | | - "try", |
213 | | - format!("{}.checked_sub({}).unwrap()", left_sugg.maybe_paren(), right_sugg), |
214 | | - applicability, |
| 178 | + "unchecked subtraction of a `Duration`", |
| 179 | + |diag| { |
| 180 | + // For chained subtraction, like `(dur1 - dur2) - dur3` or `(instant - dur1) - dur2`, |
| 181 | + // avoid suggestions |
| 182 | + if !is_chained_time_subtraction(cx, left_expr) { |
| 183 | + let mut applicability = Applicability::MachineApplicable; |
| 184 | + let left_sugg = Sugg::hir_with_applicability(cx, left_expr, "<left>", &mut applicability); |
| 185 | + let right_sugg = Sugg::hir_with_applicability(cx, right_expr, "<right>", &mut applicability); |
| 186 | + |
| 187 | + diag.span_suggestion( |
| 188 | + expr.span, |
| 189 | + "try", |
| 190 | + format!("{}.checked_sub({}).unwrap()", left_sugg.maybe_paren(), right_sugg), |
| 191 | + applicability, |
| 192 | + ); |
| 193 | + } |
| 194 | + }, |
215 | 195 | ); |
216 | 196 | } |
0 commit comments