]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/matches/manual_map.rs
Rollup merge of #99394 - JohnTitor:issue-95230, r=compiler-errors
[rust.git] / src / tools / clippy / clippy_lints / src / matches / manual_map.rs
1 use crate::{map_unit_fn::OPTION_MAP_UNIT_FN, matches::MATCH_AS_REF};
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
4 use clippy_utils::ty::{is_type_diagnostic_item, peel_mid_ty_refs_is_mutable, type_is_unsafe_function};
5 use clippy_utils::{
6     can_move_expr_to_closure, is_else_clause, is_lang_ctor, is_lint_allowed, path_to_local_id, peel_blocks,
7     peel_hir_expr_refs, peel_hir_expr_while, CaptureKind,
8 };
9 use rustc_ast::util::parser::PREC_POSTFIX;
10 use rustc_errors::Applicability;
11 use rustc_hir::LangItem::{OptionNone, OptionSome};
12 use rustc_hir::{
13     def::Res, Arm, BindingAnnotation, Block, BlockCheckMode, Expr, ExprKind, HirId, Mutability, Pat, PatKind, Path,
14     QPath, UnsafeSource,
15 };
16 use rustc_lint::LateContext;
17 use rustc_span::{sym, SyntaxContext};
18
19 use super::MANUAL_MAP;
20
21 pub(super) fn check_match<'tcx>(
22     cx: &LateContext<'tcx>,
23     expr: &'tcx Expr<'_>,
24     scrutinee: &'tcx Expr<'_>,
25     arms: &'tcx [Arm<'_>],
26 ) {
27     if let [arm1, arm2] = arms
28         && arm1.guard.is_none()
29         && arm2.guard.is_none()
30     {
31         check(cx, expr, scrutinee, arm1.pat, arm1.body, Some(arm2.pat), arm2.body);
32     }
33 }
34
35 pub(super) fn check_if_let<'tcx>(
36     cx: &LateContext<'tcx>,
37     expr: &'tcx Expr<'_>,
38     let_pat: &'tcx Pat<'_>,
39     let_expr: &'tcx Expr<'_>,
40     then_expr: &'tcx Expr<'_>,
41     else_expr: &'tcx Expr<'_>,
42 ) {
43     check(cx, expr, let_expr, let_pat, then_expr, None, else_expr);
44 }
45
46 #[expect(clippy::too_many_lines)]
47 fn check<'tcx>(
48     cx: &LateContext<'tcx>,
49     expr: &'tcx Expr<'_>,
50     scrutinee: &'tcx Expr<'_>,
51     then_pat: &'tcx Pat<'_>,
52     then_body: &'tcx Expr<'_>,
53     else_pat: Option<&'tcx Pat<'_>>,
54     else_body: &'tcx Expr<'_>,
55 ) {
56     let (scrutinee_ty, ty_ref_count, ty_mutability) =
57         peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee));
58     if !(is_type_diagnostic_item(cx, scrutinee_ty, sym::Option)
59         && is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::Option))
60     {
61         return;
62     }
63
64     let expr_ctxt = expr.span.ctxt();
65     let (some_expr, some_pat, pat_ref_count, is_wild_none) = match (
66         try_parse_pattern(cx, then_pat, expr_ctxt),
67         else_pat.map_or(Some(OptionPat::Wild), |p| try_parse_pattern(cx, p, expr_ctxt)),
68     ) {
69         (Some(OptionPat::Wild), Some(OptionPat::Some { pattern, ref_count })) if is_none_expr(cx, then_body) => {
70             (else_body, pattern, ref_count, true)
71         },
72         (Some(OptionPat::None), Some(OptionPat::Some { pattern, ref_count })) if is_none_expr(cx, then_body) => {
73             (else_body, pattern, ref_count, false)
74         },
75         (Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::Wild)) if is_none_expr(cx, else_body) => {
76             (then_body, pattern, ref_count, true)
77         },
78         (Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::None)) if is_none_expr(cx, else_body) => {
79             (then_body, pattern, ref_count, false)
80         },
81         _ => return,
82     };
83
84     // Top level or patterns aren't allowed in closures.
85     if matches!(some_pat.kind, PatKind::Or(_)) {
86         return;
87     }
88
89     let some_expr = match get_some_expr(cx, some_expr, false, expr_ctxt) {
90         Some(expr) => expr,
91         None => return,
92     };
93
94     // These two lints will go back and forth with each other.
95     if cx.typeck_results().expr_ty(some_expr.expr) == cx.tcx.types.unit
96         && !is_lint_allowed(cx, OPTION_MAP_UNIT_FN, expr.hir_id)
97     {
98         return;
99     }
100
101     // `map` won't perform any adjustments.
102     if !cx.typeck_results().expr_adjustments(some_expr.expr).is_empty() {
103         return;
104     }
105
106     // Determine which binding mode to use.
107     let explicit_ref = some_pat.contains_explicit_ref_binding();
108     let binding_ref = explicit_ref.or_else(|| (ty_ref_count != pat_ref_count).then(|| ty_mutability));
109
110     let as_ref_str = match binding_ref {
111         Some(Mutability::Mut) => ".as_mut()",
112         Some(Mutability::Not) => ".as_ref()",
113         None => "",
114     };
115
116     match can_move_expr_to_closure(cx, some_expr.expr) {
117         Some(captures) => {
118             // Check if captures the closure will need conflict with borrows made in the scrutinee.
119             // TODO: check all the references made in the scrutinee expression. This will require interacting
120             // with the borrow checker. Currently only `<local>[.<field>]*` is checked for.
121             if let Some(binding_ref_mutability) = binding_ref {
122                 let e = peel_hir_expr_while(scrutinee, |e| match e.kind {
123                     ExprKind::Field(e, _) | ExprKind::AddrOf(_, _, e) => Some(e),
124                     _ => None,
125                 });
126                 if let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(l), .. })) = e.kind {
127                     match captures.get(l) {
128                         Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return,
129                         Some(CaptureKind::Ref(Mutability::Not)) if binding_ref_mutability == Mutability::Mut => {
130                             return;
131                         },
132                         Some(CaptureKind::Ref(Mutability::Not)) | None => (),
133                     }
134                 }
135             }
136         },
137         None => return,
138     };
139
140     let mut app = Applicability::MachineApplicable;
141
142     // Remove address-of expressions from the scrutinee. Either `as_ref` will be called, or
143     // it's being passed by value.
144     let scrutinee = peel_hir_expr_refs(scrutinee).0;
145     let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app);
146     let scrutinee_str = if scrutinee.span.ctxt() == expr.span.ctxt() && scrutinee.precedence().order() < PREC_POSTFIX {
147         format!("({})", scrutinee_str)
148     } else {
149         scrutinee_str.into()
150     };
151
152     let body_str = if let PatKind::Binding(annotation, id, some_binding, None) = some_pat.kind {
153         if_chain! {
154             if !some_expr.needs_unsafe_block;
155             if let Some(func) = can_pass_as_func(cx, id, some_expr.expr);
156             if func.span.ctxt() == some_expr.expr.span.ctxt();
157             then {
158                 snippet_with_applicability(cx, func.span, "..", &mut app).into_owned()
159             } else {
160                 if path_to_local_id(some_expr.expr, id)
161                     && !is_lint_allowed(cx, MATCH_AS_REF, expr.hir_id)
162                     && binding_ref.is_some()
163                 {
164                     return;
165                 }
166
167                 // `ref` and `ref mut` annotations were handled earlier.
168                 let annotation = if matches!(annotation, BindingAnnotation::Mutable) {
169                     "mut "
170                 } else {
171                     ""
172                 };
173                 let expr_snip = snippet_with_context(cx, some_expr.expr.span, expr_ctxt, "..", &mut app).0;
174                 if some_expr.needs_unsafe_block {
175                     format!("|{}{}| unsafe {{ {} }}", annotation, some_binding, expr_snip)
176                 } else {
177                     format!("|{}{}| {}", annotation, some_binding, expr_snip)
178                 }
179             }
180         }
181     } else if !is_wild_none && explicit_ref.is_none() {
182         // TODO: handle explicit reference annotations.
183         let pat_snip = snippet_with_context(cx, some_pat.span, expr_ctxt, "..", &mut app).0;
184         let expr_snip = snippet_with_context(cx, some_expr.expr.span, expr_ctxt, "..", &mut app).0;
185         if some_expr.needs_unsafe_block {
186             format!("|{}| unsafe {{ {} }}", pat_snip, expr_snip)
187         } else {
188             format!("|{}| {}", pat_snip, expr_snip)
189         }
190     } else {
191         // Refutable bindings and mixed reference annotations can't be handled by `map`.
192         return;
193     };
194
195     span_lint_and_sugg(
196         cx,
197         MANUAL_MAP,
198         expr.span,
199         "manual implementation of `Option::map`",
200         "try this",
201         if else_pat.is_none() && is_else_clause(cx.tcx, expr) {
202             format!("{{ {}{}.map({}) }}", scrutinee_str, as_ref_str, body_str)
203         } else {
204             format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str)
205         },
206         app,
207     );
208 }
209
210 // Checks whether the expression could be passed as a function, or whether a closure is needed.
211 // Returns the function to be passed to `map` if it exists.
212 fn can_pass_as_func<'tcx>(cx: &LateContext<'tcx>, binding: HirId, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
213     match expr.kind {
214         ExprKind::Call(func, [arg])
215             if path_to_local_id(arg, binding)
216                 && cx.typeck_results().expr_adjustments(arg).is_empty()
217                 && !type_is_unsafe_function(cx, cx.typeck_results().expr_ty(func).peel_refs()) =>
218         {
219             Some(func)
220         },
221         _ => None,
222     }
223 }
224
225 enum OptionPat<'a> {
226     Wild,
227     None,
228     Some {
229         // The pattern contained in the `Some` tuple.
230         pattern: &'a Pat<'a>,
231         // The number of references before the `Some` tuple.
232         // e.g. `&&Some(_)` has a ref count of 2.
233         ref_count: usize,
234     },
235 }
236
237 struct SomeExpr<'tcx> {
238     expr: &'tcx Expr<'tcx>,
239     needs_unsafe_block: bool,
240 }
241
242 // Try to parse into a recognized `Option` pattern.
243 // i.e. `_`, `None`, `Some(..)`, or a reference to any of those.
244 fn try_parse_pattern<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, ctxt: SyntaxContext) -> Option<OptionPat<'tcx>> {
245     fn f<'tcx>(
246         cx: &LateContext<'tcx>,
247         pat: &'tcx Pat<'_>,
248         ref_count: usize,
249         ctxt: SyntaxContext,
250     ) -> Option<OptionPat<'tcx>> {
251         match pat.kind {
252             PatKind::Wild => Some(OptionPat::Wild),
253             PatKind::Ref(pat, _) => f(cx, pat, ref_count + 1, ctxt),
254             PatKind::Path(ref qpath) if is_lang_ctor(cx, qpath, OptionNone) => Some(OptionPat::None),
255             PatKind::TupleStruct(ref qpath, [pattern], _)
256                 if is_lang_ctor(cx, qpath, OptionSome) && pat.span.ctxt() == ctxt =>
257             {
258                 Some(OptionPat::Some { pattern, ref_count })
259             },
260             _ => None,
261         }
262     }
263     f(cx, pat, 0, ctxt)
264 }
265
266 // Checks for an expression wrapped by the `Some` constructor. Returns the contained expression.
267 fn get_some_expr<'tcx>(
268     cx: &LateContext<'tcx>,
269     expr: &'tcx Expr<'_>,
270     needs_unsafe_block: bool,
271     ctxt: SyntaxContext,
272 ) -> Option<SomeExpr<'tcx>> {
273     // TODO: Allow more complex expressions.
274     match expr.kind {
275         ExprKind::Call(
276             Expr {
277                 kind: ExprKind::Path(ref qpath),
278                 ..
279             },
280             [arg],
281         ) if ctxt == expr.span.ctxt() && is_lang_ctor(cx, qpath, OptionSome) => Some(SomeExpr {
282             expr: arg,
283             needs_unsafe_block,
284         }),
285         ExprKind::Block(
286             Block {
287                 stmts: [],
288                 expr: Some(expr),
289                 rules,
290                 ..
291             },
292             _,
293         ) => get_some_expr(
294             cx,
295             expr,
296             needs_unsafe_block || *rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided),
297             ctxt,
298         ),
299         _ => None,
300     }
301 }
302
303 // Checks for the `None` value.
304 fn is_none_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
305     matches!(peel_blocks(expr).kind, ExprKind::Path(ref qpath) if is_lang_ctor(cx, qpath, OptionNone))
306 }