]> git.lizzy.rs Git - rust.git/blob - clippy_utils/src/eager_or_lazy.rs
Auto merge of #6866 - anall:ice6840, r=flip1995
[rust.git] / clippy_utils / src / eager_or_lazy.rs
1 //! Utilities for evaluating whether eagerly evaluated expressions can be made lazy and vice versa.
2 //!
3 //! Things to consider:
4 //!  - has the expression side-effects?
5 //!  - is the expression computationally expensive?
6 //!
7 //! See lints:
8 //!  - unnecessary-lazy-evaluations
9 //!  - or-fun-call
10 //!  - option-if-let-else
11
12 use crate::{is_ctor_or_promotable_const_function, is_type_diagnostic_item, match_type, paths};
13 use rustc_hir::def::{DefKind, Res};
14
15 use rustc_hir::intravisit;
16 use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
17
18 use rustc_hir::{Block, Expr, ExprKind, Path, QPath};
19 use rustc_lint::LateContext;
20 use rustc_middle::hir::map::Map;
21
22 /// Is the expr pure (is it free from side-effects)?
23 /// This function is named so to stress that it isn't exhaustive and returns FNs.
24 fn identify_some_pure_patterns(expr: &Expr<'_>) -> bool {
25     match expr.kind {
26         ExprKind::Lit(..) | ExprKind::ConstBlock(..) | ExprKind::Path(..) | ExprKind::Field(..) => true,
27         ExprKind::AddrOf(_, _, addr_of_expr) => identify_some_pure_patterns(addr_of_expr),
28         ExprKind::Tup(tup_exprs) => tup_exprs.iter().all(|expr| identify_some_pure_patterns(expr)),
29         ExprKind::Struct(_, fields, expr) => {
30             fields.iter().all(|f| identify_some_pure_patterns(f.expr))
31                 && expr.map_or(true, |e| identify_some_pure_patterns(e))
32         },
33         ExprKind::Call(
34             &Expr {
35                 kind:
36                     ExprKind::Path(QPath::Resolved(
37                         _,
38                         Path {
39                             res: Res::Def(DefKind::Ctor(..) | DefKind::Variant, ..),
40                             ..
41                         },
42                     )),
43                 ..
44             },
45             args,
46         ) => args.iter().all(|expr| identify_some_pure_patterns(expr)),
47         ExprKind::Block(
48             &Block {
49                 stmts,
50                 expr: Some(expr),
51                 ..
52             },
53             _,
54         ) => stmts.is_empty() && identify_some_pure_patterns(expr),
55         ExprKind::Box(..)
56         | ExprKind::Array(..)
57         | ExprKind::Call(..)
58         | ExprKind::MethodCall(..)
59         | ExprKind::Binary(..)
60         | ExprKind::Unary(..)
61         | ExprKind::Cast(..)
62         | ExprKind::Type(..)
63         | ExprKind::DropTemps(..)
64         | ExprKind::Loop(..)
65         | ExprKind::If(..)
66         | ExprKind::Match(..)
67         | ExprKind::Closure(..)
68         | ExprKind::Block(..)
69         | ExprKind::Assign(..)
70         | ExprKind::AssignOp(..)
71         | ExprKind::Index(..)
72         | ExprKind::Break(..)
73         | ExprKind::Continue(..)
74         | ExprKind::Ret(..)
75         | ExprKind::InlineAsm(..)
76         | ExprKind::LlvmInlineAsm(..)
77         | ExprKind::Repeat(..)
78         | ExprKind::Yield(..)
79         | ExprKind::Err => false,
80     }
81 }
82
83 /// Identify some potentially computationally expensive patterns.
84 /// This function is named so to stress that its implementation is non-exhaustive.
85 /// It returns FNs and FPs.
86 fn identify_some_potentially_expensive_patterns<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
87     // Searches an expression for method calls or function calls that aren't ctors
88     struct FunCallFinder<'a, 'tcx> {
89         cx: &'a LateContext<'tcx>,
90         found: bool,
91     }
92
93     impl<'a, 'tcx> intravisit::Visitor<'tcx> for FunCallFinder<'a, 'tcx> {
94         type Map = Map<'tcx>;
95
96         fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
97             let call_found = match &expr.kind {
98                 // ignore enum and struct constructors
99                 ExprKind::Call(..) => !is_ctor_or_promotable_const_function(self.cx, expr),
100                 ExprKind::Index(obj, _) => {
101                     let ty = self.cx.typeck_results().expr_ty(obj);
102                     is_type_diagnostic_item(self.cx, ty, sym!(hashmap_type))
103                         || match_type(self.cx, ty, &paths::BTREEMAP)
104                 },
105                 ExprKind::MethodCall(..) => true,
106                 _ => false,
107             };
108
109             if call_found {
110                 self.found |= true;
111             }
112
113             if !self.found {
114                 intravisit::walk_expr(self, expr);
115             }
116         }
117
118         fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
119             NestedVisitorMap::None
120         }
121     }
122
123     let mut finder = FunCallFinder { cx, found: false };
124     finder.visit_expr(expr);
125     finder.found
126 }
127
128 pub fn is_eagerness_candidate<'a, 'tcx>(cx: &'a LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
129     !identify_some_potentially_expensive_patterns(cx, expr) && identify_some_pure_patterns(expr)
130 }
131
132 pub fn is_lazyness_candidate<'a, 'tcx>(cx: &'a LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
133     identify_some_potentially_expensive_patterns(cx, expr)
134 }