Skip to content

Commit e1344da

Browse files
committed
Handle the suggestion-less case inside print_unchecked_duration_subtration_sugg
This doesn't change any functionality, but will make it easier to switch to a uniform message in the next commit. Also a nice simplification imo
1 parent 62589a2 commit e1344da

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

clippy_lints/src/time_subtraction.rs

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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};
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::res::{MaybeDef, MaybeTypeckRes};
55
use clippy_utils::sugg::Sugg;
@@ -109,36 +109,16 @@ impl LateLintPass<'_> for UncheckedTimeSubtraction {
109109
&& !expr.span.from_expansion()
110110
&& self.msrv.meets(cx, msrvs::TRY_FROM)
111111
{
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);
124113
}
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)
126117
&& rhs_ty.is_diag_item(cx, sym::Duration)
127118
&& !expr.span.from_expansion()
128119
&& self.msrv.meets(cx, msrvs::TRY_FROM)
129120
{
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);
142122
}
143123
}
144124
}
@@ -200,17 +180,20 @@ fn print_unchecked_duration_subtraction_sugg(
200180
"unchecked subtraction between 'Duration' values"
201181
};
202182

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);
183+
span_lint_and_then(cx, UNCHECKED_TIME_SUBTRACTION, expr.span, lint_msg, |diag| {
184+
// For chained subtraction, like `(dur1 - dur2) - dur3` or `(instant - dur1) - dur2`,
185+
// avoid suggestions
186+
if !is_chained_time_subtraction(cx, left_expr) {
187+
let mut applicability = Applicability::MachineApplicable;
188+
let left_sugg = Sugg::hir_with_applicability(cx, left_expr, "<left>", &mut applicability);
189+
let right_sugg = Sugg::hir_with_applicability(cx, right_expr, "<right>", &mut applicability);
206190

207-
span_lint_and_sugg(
208-
cx,
209-
UNCHECKED_TIME_SUBTRACTION,
210-
expr.span,
211-
lint_msg,
212-
"try",
213-
format!("{}.checked_sub({}).unwrap()", left_sugg.maybe_paren(), right_sugg),
214-
applicability,
215-
);
191+
diag.span_suggestion(
192+
expr.span,
193+
"try",
194+
format!("{}.checked_sub({}).unwrap()", left_sugg.maybe_paren(), right_sugg),
195+
applicability,
196+
);
197+
}
198+
});
216199
}

0 commit comments

Comments
 (0)