|
1 | 1 | //! Contains utility functions to generate suggestions.
|
2 | 2 | #![deny(clippy::missing_docs_in_private_items)]
|
3 | 3 |
|
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}; |
6 | 8 | use rustc_ast::util::parser::AssocOp;
|
7 | 9 | use rustc_ast::{ast, token};
|
8 | 10 | use rustc_ast_pretty::pprust::token_kind_to_string;
|
9 | 11 | use rustc_errors::Applicability;
|
10 | 12 | use rustc_hir as hir;
|
| 13 | +use rustc_hir::{ExprKind, HirId, MutTy, TyKind}; |
| 14 | +use rustc_infer::infer::TyCtxtInferExt; |
11 | 15 | 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}; |
14 | 21 | use std::borrow::Cow;
|
15 | 22 | use std::convert::TryInto;
|
16 | 23 | use std::fmt::Display;
|
| 24 | +use std::iter; |
17 | 25 | use std::ops::{Add, Neg, Not, Sub};
|
18 | 26 |
|
19 | 27 | /// A helper type to build suggestion correctly handling parentheses.
|
@@ -716,6 +724,267 @@ impl<T: LintContext> DiagnosticBuilderExt<T> for rustc_errors::DiagnosticBuilder
|
716 | 724 | }
|
717 | 725 | }
|
718 | 726 |
|
| 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 | + |
719 | 988 | #[cfg(test)]
|
720 | 989 | mod test {
|
721 | 990 | use super::Sugg;
|
|
0 commit comments