Skip to content

Fix search_is_some suggests wrongly inside macro #15135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions clippy_utils/src/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::ty::expr_sig;
use crate::{get_parent_expr_for_hir, higher};
use rustc_ast::ast;
use rustc_ast::util::parser::AssocOp;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::{Closure, ExprKind, HirId, MutTy, TyKind};
use rustc_hir::{self as hir, Closure, ExprKind, HirId, MutTy, Node, TyKind};
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
use rustc_lint::{EarlyContext, LateContext, LintContext};
use rustc_middle::hir::place::ProjectionKind;
Expand Down Expand Up @@ -753,8 +753,10 @@ pub fn deref_closure_args(cx: &LateContext<'_>, closure: &hir::Expr<'_>) -> Opti
let mut visitor = DerefDelegate {
cx,
closure_span: closure.span,
closure_arg_id: closure_body.params[0].pat.hir_id,
closure_arg_is_type_annotated_double_ref,
next_pos: closure.span.lo(),
checked_borrows: FxHashSet::default(),
suggestion_start: String::new(),
applicability: Applicability::MachineApplicable,
};
Expand All @@ -780,10 +782,15 @@ struct DerefDelegate<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
/// The span of the input closure to adapt
closure_span: Span,
/// The `hir_id` of the closure argument being checked
closure_arg_id: HirId,
/// Indicates if the arg of the closure is a type annotated double reference
closure_arg_is_type_annotated_double_ref: bool,
/// last position of the span to gradually build the suggestion
next_pos: BytePos,
/// `hir_id` that has been checked. This is used to avoid checking the same `hir_id` multiple
/// times when inside macro expansions.
checked_borrows: FxHashSet<HirId>,
/// starting part of the gradually built suggestion
suggestion_start: String,
/// confidence on the built suggestion
Expand Down Expand Up @@ -847,9 +854,15 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {

fn use_cloned(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}

#[expect(clippy::too_many_lines)]
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {
if let PlaceBase::Local(id) = cmt.place.base {
let span = self.cx.tcx.hir_span(cmt.hir_id);
if !self.checked_borrows.insert(cmt.hir_id) {
// already checked this span and hir_id, skip
return;
}

let start_span = Span::new(self.next_pos, span.lo(), span.ctxt(), None);
let mut start_snip = snippet_with_applicability(self.cx, start_span, "..", &mut self.applicability);

Expand All @@ -858,7 +871,11 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
// full identifier that includes projection (i.e.: `fp.field`)
let ident_str_with_proj = snippet(self.cx, span, "..").to_string();

if cmt.place.projections.is_empty() {
if let Node::Pat(pat) = self.cx.tcx.hir_node(id)
&& pat.hir_id != self.closure_arg_id
{
let _ = write!(self.suggestion_start, "{start_snip}{ident_str_with_proj}");
} else if cmt.place.projections.is_empty() {
// handle item without any projection, that needs an explicit borrowing
// i.e.: suggest `&x` instead of `x`
let _: fmt::Result = write!(self.suggestion_start, "{start_snip}&{ident_str}");
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/search_is_some_fixable_some.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,10 @@ mod issue9120 {
//~^ search_is_some
}
}

fn issue15102() {
let values = [None, Some(3)];
let has_even = values.iter().any(|v| matches!(&v, Some(x) if x % 2 == 0));
//~^ search_is_some
println!("{has_even}");
}
7 changes: 7 additions & 0 deletions tests/ui/search_is_some_fixable_some.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,10 @@ mod issue9120 {
//~^ search_is_some
}
}

fn issue15102() {
let values = [None, Some(3)];
let has_even = values.iter().find(|v| matches!(v, Some(x) if x % 2 == 0)).is_some();
//~^ search_is_some
println!("{has_even}");
}
8 changes: 7 additions & 1 deletion tests/ui/search_is_some_fixable_some.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,11 @@ error: called `is_some()` after searching an `Iterator` with `find`
LL | let _ = v.iter().find(|x: &&u32| (*arg_no_deref_dyn)(x)).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `any(|x: &u32| (*arg_no_deref_dyn)(&x))`

error: aborting due to 46 previous errors
error: called `is_some()` after searching an `Iterator` with `find`
--> tests/ui/search_is_some_fixable_some.rs:303:34
|
LL | let has_even = values.iter().find(|v| matches!(v, Some(x) if x % 2 == 0)).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `any(|v| matches!(&v, Some(x) if x % 2 == 0))`

error: aborting due to 47 previous errors