From bc5a8e9537d30858fede92347e3842d4d4f8a803 Mon Sep 17 00:00:00 2001 From: Preston From Date: Thu, 2 Jun 2022 02:23:42 -0600 Subject: [PATCH] Lint message correctly identifies match vs for loop --- .../src/significant_drop_in_scrutinee.rs | 23 +++++++++++-------- tests/ui/significant_drop_in_scrutinee.stderr | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/significant_drop_in_scrutinee.rs b/clippy_lints/src/significant_drop_in_scrutinee.rs index 6b45b6d4452..2f7819cb470 100644 --- a/clippy_lints/src/significant_drop_in_scrutinee.rs +++ b/clippy_lints/src/significant_drop_in_scrutinee.rs @@ -91,15 +91,11 @@ declare_lint_pass!(SignificantDropInScrutinee => [SIGNIFICANT_DROP_IN_SCRUTINEE] impl<'tcx> LateLintPass<'tcx> for SignificantDropInScrutinee { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { - if let Some(suggestions) = has_significant_drop_in_scrutinee(cx, expr) { + if let Some((suggestions, message)) = has_significant_drop_in_scrutinee(cx, expr) { for found in suggestions { - span_lint_and_then( - cx, - SIGNIFICANT_DROP_IN_SCRUTINEE, - found.found_span, - "temporary with significant drop in match scrutinee", - |diag| set_diagnostic(diag, cx, expr, found), - ); + span_lint_and_then(cx, SIGNIFICANT_DROP_IN_SCRUTINEE, found.found_span, message, |diag| { + set_diagnostic(diag, cx, expr, found); + }); } } } @@ -153,13 +149,20 @@ fn set_diagnostic<'tcx>(diag: &mut Diagnostic, cx: &LateContext<'tcx>, expr: &'t fn has_significant_drop_in_scrutinee<'tcx, 'a>( cx: &'a LateContext<'tcx>, expr: &'tcx Expr<'tcx>, -) -> Option> { +) -> Option<(Vec, &'static str)> { match expr.kind { ExprKind::Match(match_expr, _, source) => { match source { MatchSource::Normal | MatchSource::ForLoopDesugar => { let mut helper = SigDropHelper::new(cx); - helper.find_sig_drop(match_expr) + helper.find_sig_drop(match_expr).map(|drops| { + let message = if source == MatchSource::Normal { + "temporary with significant drop in match scrutinee" + } else { + "temporary with significant drop in for loop" + }; + (drops, message) + }) }, // MatchSource of TryDesugar or AwaitDesugar is out of scope for this lint MatchSource::TryDesugar | MatchSource::AwaitDesugar => None, diff --git a/tests/ui/significant_drop_in_scrutinee.stderr b/tests/ui/significant_drop_in_scrutinee.stderr index 77942325244..303f3c1df03 100644 --- a/tests/ui/significant_drop_in_scrutinee.stderr +++ b/tests/ui/significant_drop_in_scrutinee.stderr @@ -285,7 +285,7 @@ error: temporary with significant drop in match scrutinee LL | match rwlock.read().unwrap().to_number() { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: temporary with significant drop in match scrutinee +error: temporary with significant drop in for loop --> $DIR/significant_drop_in_scrutinee.rs:589:14 | LL | for s in rwlock.read().unwrap().iter() {