Fix unnecessary_unwrap emitted twice in closure (#14763) (#14770)

The problem is that `check_fn` is triggered by both function and
closure, and `visit_expr` can visit expressions in another closures
within a function or closure.

So just skip walking in a inner closure.

changelog: Fix [`unnecessary_unwrap`] emitted twice in closure which
inside in a function or another closure.

Fixes: rust-lang/rust-clippy#14763
This commit is contained in:
Samuel Tardieu 2025-05-10 14:15:50 +00:00 committed by GitHub
commit 5262ab2416
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 89 additions and 1 deletions

View file

@ -292,6 +292,10 @@ impl<'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'_, 'tcx> {
if expr.span.in_external_macro(self.cx.tcx.sess.source_map()) {
return;
}
// Skip checking inside closures since they are visited through `Unwrap::check_fn()` already.
if matches!(expr.kind, ExprKind::Closure(_)) {
return;
}
if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr) {
walk_expr(self, cond);
self.visit_branch(expr, cond, then, false);