Skip to content

Commit bc5a8e9

Browse files
committed
Lint message correctly identifies match vs for loop
1 parent e33d87d commit bc5a8e9

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

clippy_lints/src/significant_drop_in_scrutinee.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,11 @@ declare_lint_pass!(SignificantDropInScrutinee => [SIGNIFICANT_DROP_IN_SCRUTINEE]
9191

9292
impl<'tcx> LateLintPass<'tcx> for SignificantDropInScrutinee {
9393
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
94-
if let Some(suggestions) = has_significant_drop_in_scrutinee(cx, expr) {
94+
if let Some((suggestions, message)) = has_significant_drop_in_scrutinee(cx, expr) {
9595
for found in suggestions {
96-
span_lint_and_then(
97-
cx,
98-
SIGNIFICANT_DROP_IN_SCRUTINEE,
99-
found.found_span,
100-
"temporary with significant drop in match scrutinee",
101-
|diag| set_diagnostic(diag, cx, expr, found),
102-
);
96+
span_lint_and_then(cx, SIGNIFICANT_DROP_IN_SCRUTINEE, found.found_span, message, |diag| {
97+
set_diagnostic(diag, cx, expr, found);
98+
});
10399
}
104100
}
105101
}
@@ -153,13 +149,20 @@ fn set_diagnostic<'tcx>(diag: &mut Diagnostic, cx: &LateContext<'tcx>, expr: &'t
153149
fn has_significant_drop_in_scrutinee<'tcx, 'a>(
154150
cx: &'a LateContext<'tcx>,
155151
expr: &'tcx Expr<'tcx>,
156-
) -> Option<Vec<FoundSigDrop>> {
152+
) -> Option<(Vec<FoundSigDrop>, &'static str)> {
157153
match expr.kind {
158154
ExprKind::Match(match_expr, _, source) => {
159155
match source {
160156
MatchSource::Normal | MatchSource::ForLoopDesugar => {
161157
let mut helper = SigDropHelper::new(cx);
162-
helper.find_sig_drop(match_expr)
158+
helper.find_sig_drop(match_expr).map(|drops| {
159+
let message = if source == MatchSource::Normal {
160+
"temporary with significant drop in match scrutinee"
161+
} else {
162+
"temporary with significant drop in for loop"
163+
};
164+
(drops, message)
165+
})
163166
},
164167
// MatchSource of TryDesugar or AwaitDesugar is out of scope for this lint
165168
MatchSource::TryDesugar | MatchSource::AwaitDesugar => None,

tests/ui/significant_drop_in_scrutinee.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ error: temporary with significant drop in match scrutinee
285285
LL | match rwlock.read().unwrap().to_number() {
286286
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
287287

288-
error: temporary with significant drop in match scrutinee
288+
error: temporary with significant drop in for loop
289289
--> $DIR/significant_drop_in_scrutinee.rs:589:14
290290
|
291291
LL | for s in rwlock.read().unwrap().iter() {

0 commit comments

Comments
 (0)