Skip to content

Commit 3cfd1e5

Browse files
committed
Make messages more accurate, check lint enabled
1 parent 1f707db commit 3cfd1e5

File tree

3 files changed

+104
-91
lines changed

3 files changed

+104
-91
lines changed

clippy_lints/src/matches/significant_drop_in_scrutinee.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::FxHashSet;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::get_attr;
3+
use clippy_utils::{get_attr, is_lint_allowed};
44
use clippy_utils::source::{indent_of, snippet};
55
use rustc_errors::{Applicability, Diagnostic};
66
use rustc_hir::intravisit::{walk_expr, Visitor};
@@ -19,16 +19,18 @@ pub(super) fn check<'tcx>(
1919
arms: &'tcx [Arm<'_>],
2020
source: MatchSource,
2121
) {
22+
if is_lint_allowed(cx, SIGNIFICANT_DROP_IN_SCRUTINEE, expr.hir_id) {
23+
return;
24+
}
25+
2226
if let Some((suggestions, message)) = has_significant_drop_in_scrutinee(cx, scrutinee, source) {
2327
for found in suggestions {
2428
span_lint_and_then(cx, SIGNIFICANT_DROP_IN_SCRUTINEE, found.found_span, message, |diag| {
2529
set_diagnostic(diag, cx, expr, found);
2630
let s = Span::new(expr.span.hi(), expr.span.hi(), expr.span.ctxt(), None);
27-
diag.span_label(s, "original temporary lives until here");
28-
if let Some(spans) = has_significant_drop_in_arms(cx, arms) {
29-
for span in spans {
30-
diag.span_label(span, "another temporary with significant `Drop` created here");
31-
}
31+
diag.span_label(s, "temporary lives until here");
32+
for span in has_significant_drop_in_arms(cx, arms) {
33+
diag.span_label(span, "another value with significant `Drop` created here");
3234
}
3335
diag.note("this might lead to deadlocks or other unexpected behavior");
3436
});
@@ -360,19 +362,19 @@ impl<'a, 'tcx> Visitor<'tcx> for SigDropHelper<'a, 'tcx> {
360362

361363
struct ArmSigDropHelper<'a, 'tcx> {
362364
sig_drop_checker: SigDropChecker<'a, 'tcx>,
363-
found_sig_drop_spans: Option<FxHashSet<Span>>,
365+
found_sig_drop_spans: FxHashSet<Span>,
364366
}
365367

366368
impl<'a, 'tcx> ArmSigDropHelper<'a, 'tcx> {
367369
fn new(cx: &'a LateContext<'tcx>) -> ArmSigDropHelper<'a, 'tcx> {
368370
ArmSigDropHelper {
369371
sig_drop_checker: SigDropChecker::new(cx),
370-
found_sig_drop_spans: None,
372+
found_sig_drop_spans: FxHashSet::<Span>::default(),
371373
}
372374
}
373375
}
374376

375-
fn has_significant_drop_in_arms<'tcx, 'a>(cx: &'a LateContext<'tcx>, arms: &'tcx [Arm<'_>]) -> Option<FxHashSet<Span>> {
377+
fn has_significant_drop_in_arms<'tcx, 'a>(cx: &'a LateContext<'tcx>, arms: &'tcx [Arm<'_>]) -> FxHashSet<Span> {
376378
let mut helper = ArmSigDropHelper::new(cx);
377379
for arm in arms {
378380
helper.visit_expr(arm.body);
@@ -386,9 +388,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ArmSigDropHelper<'a, 'tcx> {
386388
.sig_drop_checker
387389
.has_sig_drop_attr(self.sig_drop_checker.cx, self.sig_drop_checker.get_type(ex))
388390
{
389-
self.found_sig_drop_spans
390-
.get_or_insert_with(FxHashSet::default)
391-
.insert(ex.span);
391+
self.found_sig_drop_spans.insert(ex.span);
392392
return;
393393
}
394394
walk_expr(self, ex);

tests/ui/significant_drop_in_scrutinee.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ fn should_trigger_lint_with_mutex_guard_in_match_scrutinee() {
6464
};
6565
}
6666

67+
fn should_not_trigger_lint_with_mutex_guard_in_match_scrutinee_when_lint_allowed() {
68+
let mutex = Mutex::new(State {});
69+
70+
// Lint should not be triggered because it is "allowed" below.
71+
#[allow(clippy::significant_drop_in_scrutinee)]
72+
match mutex.lock().unwrap().foo() {
73+
true => {
74+
mutex.lock().unwrap().bar();
75+
},
76+
false => {},
77+
};
78+
}
79+
6780
fn should_not_trigger_lint_for_insignificant_drop() {
6881
// Should not trigger lint because there are no temporaries whose drops have a significant
6982
// side effect.

0 commit comments

Comments
 (0)