Skip to content

Commit d5d830a

Browse files
committed
Auto merge of rust-lang#7463 - ThibsG:find_any_7392, r=xFrednet
Fix `any()` not taking reference in `search_is_some` lint `find` gives reference to the item, but `any` does not, so suggestion is broken in some specific cases. Fixes: rust-lang#7392 changelog: [`search_is_some`] Fix suggestion for `any()` not taking item by reference
2 parents 392b0c5 + a8e7fed commit d5d830a

13 files changed

+1754
-333
lines changed

clippy_lints/src/methods/search_is_some.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::source::{snippet, snippet_with_applicability};
3+
use clippy_utils::sugg::deref_closure_args;
34
use clippy_utils::ty::is_type_diagnostic_item;
45
use clippy_utils::{is_trait_method, strip_pat_refs};
56
use if_chain::if_chain;
@@ -37,6 +38,7 @@ pub(super) fn check<'tcx>(
3738
if search_snippet.lines().count() <= 1 {
3839
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
3940
// suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
41+
let mut applicability = Applicability::MachineApplicable;
4042
let any_search_snippet = if_chain! {
4143
if search_method == "find";
4244
if let hir::ExprKind::Closure(_, _, body_id, ..) = search_arg.kind;
@@ -45,9 +47,15 @@ pub(super) fn check<'tcx>(
4547
then {
4648
if let hir::PatKind::Ref(..) = closure_arg.pat.kind {
4749
Some(search_snippet.replacen('&', "", 1))
48-
} else if let PatKind::Binding(_, _, ident, _) = strip_pat_refs(closure_arg.pat).kind {
49-
let name = &*ident.name.as_str();
50-
Some(search_snippet.replace(&format!("*{}", name), name))
50+
} else if let PatKind::Binding(..) = strip_pat_refs(closure_arg.pat).kind {
51+
// `find()` provides a reference to the item, but `any` does not,
52+
// so we should fix item usages for suggestion
53+
if let Some(closure_sugg) = deref_closure_args(cx, search_arg) {
54+
applicability = closure_sugg.applicability;
55+
Some(closure_sugg.suggestion)
56+
} else {
57+
Some(search_snippet.to_string())
58+
}
5159
} else {
5260
None
5361
}
@@ -67,7 +75,7 @@ pub(super) fn check<'tcx>(
6775
"any({})",
6876
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
6977
),
70-
Applicability::MachineApplicable,
78+
applicability,
7179
);
7280
} else {
7381
let iter = snippet(cx, search_recv.span, "..");
@@ -82,7 +90,7 @@ pub(super) fn check<'tcx>(
8290
iter,
8391
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
8492
),
85-
Applicability::MachineApplicable,
93+
applicability,
8694
);
8795
}
8896
} else {

clippy_utils/src/sugg.rs

Lines changed: 273 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
//! Contains utility functions to generate suggestions.
22
#![deny(clippy::missing_docs_in_private_items)]
33

4-
use crate::higher;
5-
use crate::source::{snippet, snippet_opt, snippet_with_context, snippet_with_macro_callsite};
4+
use crate::source::{
5+
snippet, snippet_opt, snippet_with_applicability, snippet_with_context, snippet_with_macro_callsite,
6+
};
7+
use crate::{get_parent_expr_for_hir, higher};
68
use rustc_ast::util::parser::AssocOp;
79
use rustc_ast::{ast, token};
810
use rustc_ast_pretty::pprust::token_kind_to_string;
911
use rustc_errors::Applicability;
1012
use rustc_hir as hir;
13+
use rustc_hir::{ExprKind, HirId, MutTy, TyKind};
14+
use rustc_infer::infer::TyCtxtInferExt;
1115
use rustc_lint::{EarlyContext, LateContext, LintContext};
12-
use rustc_span::source_map::{CharPos, Span};
13-
use rustc_span::{BytePos, Pos, SyntaxContext};
16+
use rustc_middle::hir::place::ProjectionKind;
17+
use rustc_middle::mir::{FakeReadCause, Mutability};
18+
use rustc_middle::ty;
19+
use rustc_span::source_map::{BytePos, CharPos, Pos, Span, SyntaxContext};
20+
use rustc_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
1421
use std::borrow::Cow;
1522
use std::convert::TryInto;
1623
use std::fmt::Display;
24+
use std::iter;
1725
use std::ops::{Add, Neg, Not, Sub};
1826

1927
/// A helper type to build suggestion correctly handling parentheses.
@@ -716,6 +724,267 @@ impl<T: LintContext> DiagnosticBuilderExt<T> for rustc_errors::DiagnosticBuilder
716724
}
717725
}
718726

727+
/// Suggestion results for handling closure
728+
/// args dereferencing and borrowing
729+
pub struct DerefClosure {
730+
/// confidence on the built suggestion
731+
pub applicability: Applicability,
732+
/// gradually built suggestion
733+
pub suggestion: String,
734+
}
735+
736+
/// Build suggestion gradually by handling closure arg specific usages,
737+
/// such as explicit deref and borrowing cases.
738+
/// Returns `None` if no such use cases have been triggered in closure body
739+
///
740+
/// note: this only works on single line immutable closures with exactly one input parameter.
741+
pub fn deref_closure_args<'tcx>(cx: &LateContext<'_>, closure: &'tcx hir::Expr<'_>) -> Option<DerefClosure> {
742+
if let hir::ExprKind::Closure(_, fn_decl, body_id, ..) = closure.kind {
743+
let closure_body = cx.tcx.hir().body(body_id);
744+
// is closure arg a type annotated double reference (i.e.: `|x: &&i32| ...`)
745+
// a type annotation is present if param `kind` is different from `TyKind::Infer`
746+
let closure_arg_is_type_annotated_double_ref = if let TyKind::Rptr(_, MutTy { ty, .. }) = fn_decl.inputs[0].kind
747+
{
748+
matches!(ty.kind, TyKind::Rptr(_, MutTy { .. }))
749+
} else {
750+
false
751+
};
752+
753+
let mut visitor = DerefDelegate {
754+
cx,
755+
closure_span: closure.span,
756+
closure_arg_is_type_annotated_double_ref,
757+
next_pos: closure.span.lo(),
758+
suggestion_start: String::new(),
759+
applicability: Applicability::MaybeIncorrect,
760+
};
761+
762+
let fn_def_id = cx.tcx.hir().local_def_id(closure.hir_id);
763+
cx.tcx.infer_ctxt().enter(|infcx| {
764+
ExprUseVisitor::new(&mut visitor, &infcx, fn_def_id, cx.param_env, cx.typeck_results())
765+
.consume_body(closure_body);
766+
});
767+
768+
if !visitor.suggestion_start.is_empty() {
769+
return Some(DerefClosure {
770+
applicability: visitor.applicability,
771+
suggestion: visitor.finish(),
772+
});
773+
}
774+
}
775+
None
776+
}
777+
778+
/// Visitor struct used for tracking down
779+
/// dereferencing and borrowing of closure's args
780+
struct DerefDelegate<'a, 'tcx> {
781+
/// The late context of the lint
782+
cx: &'a LateContext<'tcx>,
783+
/// The span of the input closure to adapt
784+
closure_span: Span,
785+
/// Indicates if the arg of the closure is a type annotated double reference
786+
closure_arg_is_type_annotated_double_ref: bool,
787+
/// last position of the span to gradually build the suggestion
788+
next_pos: BytePos,
789+
/// starting part of the gradually built suggestion
790+
suggestion_start: String,
791+
/// confidence on the built suggestion
792+
applicability: Applicability,
793+
}
794+
795+
impl DerefDelegate<'_, 'tcx> {
796+
/// build final suggestion:
797+
/// - create the ending part of suggestion
798+
/// - concatenate starting and ending parts
799+
/// - potentially remove needless borrowing
800+
pub fn finish(&mut self) -> String {
801+
let end_span = Span::new(self.next_pos, self.closure_span.hi(), self.closure_span.ctxt(), None);
802+
let end_snip = snippet_with_applicability(self.cx, end_span, "..", &mut self.applicability);
803+
let sugg = format!("{}{}", self.suggestion_start, end_snip);
804+
if self.closure_arg_is_type_annotated_double_ref {
805+
sugg.replacen('&', "", 1)
806+
} else {
807+
sugg
808+
}
809+
}
810+
811+
/// indicates whether the function from `parent_expr` takes its args by double reference
812+
fn func_takes_arg_by_double_ref(&self, parent_expr: &'tcx hir::Expr<'_>, cmt_hir_id: HirId) -> bool {
813+
let (call_args, inputs) = match parent_expr.kind {
814+
ExprKind::MethodCall(_, _, call_args, _) => {
815+
if let Some(method_did) = self.cx.typeck_results().type_dependent_def_id(parent_expr.hir_id) {
816+
(call_args, self.cx.tcx.fn_sig(method_did).skip_binder().inputs())
817+
} else {
818+
return false;
819+
}
820+
},
821+
ExprKind::Call(func, call_args) => {
822+
let typ = self.cx.typeck_results().expr_ty(func);
823+
(call_args, typ.fn_sig(self.cx.tcx).skip_binder().inputs())
824+
},
825+
_ => return false,
826+
};
827+
828+
iter::zip(call_args, inputs)
829+
.any(|(arg, ty)| arg.hir_id == cmt_hir_id && matches!(ty.kind(), ty::Ref(_, inner, _) if inner.is_ref()))
830+
}
831+
}
832+
833+
impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
834+
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
835+
836+
#[allow(clippy::too_many_lines)]
837+
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {
838+
if let PlaceBase::Local(id) = cmt.place.base {
839+
let map = self.cx.tcx.hir();
840+
let span = map.span(cmt.hir_id);
841+
let start_span = Span::new(self.next_pos, span.lo(), span.ctxt(), None);
842+
let mut start_snip = snippet_with_applicability(self.cx, start_span, "..", &mut self.applicability);
843+
844+
// identifier referring to the variable currently triggered (i.e.: `fp`)
845+
let ident_str = map.name(id).to_string();
846+
// full identifier that includes projection (i.e.: `fp.field`)
847+
let ident_str_with_proj = snippet(self.cx, span, "..").to_string();
848+
849+
if cmt.place.projections.is_empty() {
850+
// handle item without any projection, that needs an explicit borrowing
851+
// i.e.: suggest `&x` instead of `x`
852+
self.suggestion_start.push_str(&format!("{}&{}", start_snip, ident_str));
853+
} else {
854+
// cases where a parent `Call` or `MethodCall` is using the item
855+
// i.e.: suggest `.contains(&x)` for `.find(|x| [1, 2, 3].contains(x)).is_none()`
856+
//
857+
// Note about method calls:
858+
// - compiler automatically dereference references if the target type is a reference (works also for
859+
// function call)
860+
// - `self` arguments in the case of `x.is_something()` are also automatically (de)referenced, and
861+
// no projection should be suggested
862+
if let Some(parent_expr) = get_parent_expr_for_hir(self.cx, cmt.hir_id) {
863+
match &parent_expr.kind {
864+
// given expression is the self argument and will be handled completely by the compiler
865+
// i.e.: `|x| x.is_something()`
866+
ExprKind::MethodCall(_, _, [self_expr, ..], _) if self_expr.hir_id == cmt.hir_id => {
867+
self.suggestion_start
868+
.push_str(&format!("{}{}", start_snip, ident_str_with_proj));
869+
self.next_pos = span.hi();
870+
return;
871+
},
872+
// item is used in a call
873+
// i.e.: `Call`: `|x| please(x)` or `MethodCall`: `|x| [1, 2, 3].contains(x)`
874+
ExprKind::Call(_, [call_args @ ..]) | ExprKind::MethodCall(_, _, [_, call_args @ ..], _) => {
875+
let expr = self.cx.tcx.hir().expect_expr(cmt.hir_id);
876+
let arg_ty_kind = self.cx.typeck_results().expr_ty(expr).kind();
877+
878+
if matches!(arg_ty_kind, ty::Ref(_, _, Mutability::Not)) {
879+
// suggest ampersand if call function is taking args by double reference
880+
let takes_arg_by_double_ref =
881+
self.func_takes_arg_by_double_ref(parent_expr, cmt.hir_id);
882+
883+
// compiler will automatically dereference field or index projection, so no need
884+
// to suggest ampersand, but full identifier that includes projection is required
885+
let has_field_or_index_projection =
886+
cmt.place.projections.iter().any(|proj| {
887+
matches!(proj.kind, ProjectionKind::Field(..) | ProjectionKind::Index)
888+
});
889+
890+
// no need to bind again if the function doesn't take arg by double ref
891+
// and if the item is already a double ref
892+
let ident_sugg = if !call_args.is_empty()
893+
&& !takes_arg_by_double_ref
894+
&& (self.closure_arg_is_type_annotated_double_ref || has_field_or_index_projection)
895+
{
896+
let ident = if has_field_or_index_projection {
897+
ident_str_with_proj
898+
} else {
899+
ident_str
900+
};
901+
format!("{}{}", start_snip, ident)
902+
} else {
903+
format!("{}&{}", start_snip, ident_str)
904+
};
905+
self.suggestion_start.push_str(&ident_sugg);
906+
self.next_pos = span.hi();
907+
return;
908+
}
909+
910+
self.applicability = Applicability::Unspecified;
911+
},
912+
_ => (),
913+
}
914+
}
915+
916+
let mut replacement_str = ident_str;
917+
let mut projections_handled = false;
918+
cmt.place.projections.iter().enumerate().for_each(|(i, proj)| {
919+
match proj.kind {
920+
// Field projection like `|v| v.foo`
921+
// no adjustment needed here, as field projections are handled by the compiler
922+
ProjectionKind::Field(..) => match cmt.place.ty_before_projection(i).kind() {
923+
ty::Adt(..) | ty::Tuple(_) => {
924+
replacement_str = ident_str_with_proj.clone();
925+
projections_handled = true;
926+
},
927+
_ => (),
928+
},
929+
// Index projection like `|x| foo[x]`
930+
// the index is dropped so we can't get it to build the suggestion,
931+
// so the span is set-up again to get more code, using `span.hi()` (i.e.: `foo[x]`)
932+
// instead of `span.lo()` (i.e.: `foo`)
933+
ProjectionKind::Index => {
934+
let start_span = Span::new(self.next_pos, span.hi(), span.ctxt(), None);
935+
start_snip = snippet_with_applicability(self.cx, start_span, "..", &mut self.applicability);
936+
replacement_str.clear();
937+
projections_handled = true;
938+
},
939+
// note: unable to trigger `Subslice` kind in tests
940+
ProjectionKind::Subslice => (),
941+
ProjectionKind::Deref => {
942+
// Explicit derefs are typically handled later on, but
943+
// some items do not need explicit deref, such as array accesses,
944+
// so we mark them as already processed
945+
// i.e.: don't suggest `*sub[1..4].len()` for `|sub| sub[1..4].len() == 3`
946+
if let ty::Ref(_, inner, _) = cmt.place.ty_before_projection(i).kind() {
947+
if matches!(inner.kind(), ty::Ref(_, innermost, _) if innermost.is_array()) {
948+
projections_handled = true;
949+
}
950+
}
951+
},
952+
}
953+
});
954+
955+
// handle `ProjectionKind::Deref` by removing one explicit deref
956+
// if no special case was detected (i.e.: suggest `*x` instead of `**x`)
957+
if !projections_handled {
958+
let last_deref = cmt
959+
.place
960+
.projections
961+
.iter()
962+
.rposition(|proj| proj.kind == ProjectionKind::Deref);
963+
964+
if let Some(pos) = last_deref {
965+
let mut projections = cmt.place.projections.clone();
966+
projections.truncate(pos);
967+
968+
for item in projections {
969+
if item.kind == ProjectionKind::Deref {
970+
replacement_str = format!("*{}", replacement_str);
971+
}
972+
}
973+
}
974+
}
975+
976+
self.suggestion_start
977+
.push_str(&format!("{}{}", start_snip, replacement_str));
978+
}
979+
self.next_pos = span.hi();
980+
}
981+
}
982+
983+
fn mutate(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
984+
985+
fn fake_read(&mut self, _: rustc_typeck::expr_use_visitor::Place<'tcx>, _: FakeReadCause, _: HirId) {}
986+
}
987+
719988
#[cfg(test)]
720989
mod test {
721990
use super::Sugg;

tests/ui/search_is_some.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ fn main() {
3636
// check that we don't lint if `find()` is called with
3737
// `Pattern` that is not a string
3838
let _ = "hello world".find(|c: char| c == 'o' || c == 'l').is_some();
39+
40+
let some_closure = |x: &u32| *x == 0;
41+
let _ = (0..1).find(some_closure).is_some();
3942
}
4043

4144
#[rustfmt::skip]
@@ -70,4 +73,7 @@ fn is_none() {
7073
// check that we don't lint if `find()` is called with
7174
// `Pattern` that is not a string
7275
let _ = "hello world".find(|c: char| c == 'o' || c == 'l').is_none();
76+
77+
let some_closure = |x: &u32| *x == 0;
78+
let _ = (0..1).find(some_closure).is_none();
7379
}

0 commit comments

Comments
 (0)