]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/higher.rs
Change Hash{Map, Set} to FxHash{Map, Set}
[rust.git] / clippy_lints / src / utils / higher.rs
1 //! This module contains functions for retrieve the original AST from lowered
2 //! `hir`.
3
4 #![deny(clippy::missing_docs_in_private_items)]
5
6 use if_chain::if_chain;
7 use rustc::{hir, ty};
8 use rustc::lint::LateContext;
9 use syntax::ast;
10 use crate::utils::{is_expn_of, match_def_path, match_qpath, opt_def_id, paths, resolve_node};
11
12 /// Convert a hir binary operator to the corresponding `ast` type.
13 pub fn binop(op: hir::BinOpKind) -> ast::BinOpKind {
14     match op {
15         hir::BinOpKind::Eq => ast::BinOpKind::Eq,
16         hir::BinOpKind::Ge => ast::BinOpKind::Ge,
17         hir::BinOpKind::Gt => ast::BinOpKind::Gt,
18         hir::BinOpKind::Le => ast::BinOpKind::Le,
19         hir::BinOpKind::Lt => ast::BinOpKind::Lt,
20         hir::BinOpKind::Ne => ast::BinOpKind::Ne,
21         hir::BinOpKind::Or => ast::BinOpKind::Or,
22         hir::BinOpKind::Add => ast::BinOpKind::Add,
23         hir::BinOpKind::And => ast::BinOpKind::And,
24         hir::BinOpKind::BitAnd => ast::BinOpKind::BitAnd,
25         hir::BinOpKind::BitOr => ast::BinOpKind::BitOr,
26         hir::BinOpKind::BitXor => ast::BinOpKind::BitXor,
27         hir::BinOpKind::Div => ast::BinOpKind::Div,
28         hir::BinOpKind::Mul => ast::BinOpKind::Mul,
29         hir::BinOpKind::Rem => ast::BinOpKind::Rem,
30         hir::BinOpKind::Shl => ast::BinOpKind::Shl,
31         hir::BinOpKind::Shr => ast::BinOpKind::Shr,
32         hir::BinOpKind::Sub => ast::BinOpKind::Sub,
33     }
34 }
35
36 /// Represent a range akin to `ast::ExprKind::Range`.
37 #[derive(Debug, Copy, Clone)]
38 pub struct Range<'a> {
39     /// The lower bound of the range, or `None` for ranges such as `..X`.
40     pub start: Option<&'a hir::Expr>,
41     /// The upper bound of the range, or `None` for ranges such as `X..`.
42     pub end: Option<&'a hir::Expr>,
43     /// Whether the interval is open or closed.
44     pub limits: ast::RangeLimits,
45 }
46
47 /// Higher a `hir` range to something similar to `ast::ExprKind::Range`.
48 pub fn range<'a, 'b, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'b hir::Expr) -> Option<Range<'b>> {
49
50     let def_path = match cx.tables.expr_ty(expr).sty {
51         ty::Adt(def, _) => cx.tcx.def_path(def.did),
52         _ => return None,
53     };
54
55     // sanity checks for std::ops::RangeXXXX
56     if def_path.data.len() != 3 {
57         return None;
58     }
59     if def_path.data.get(0)?.data.as_interned_str() != "ops" {
60         return None;
61     }
62     if def_path.data.get(1)?.data.as_interned_str() != "range" {
63         return None;
64     }
65     let type_name = def_path.data.get(2)?.data.as_interned_str();
66     let range_types = [
67         "RangeFrom",
68         "RangeFull",
69         "RangeInclusive",
70         "Range",
71         "RangeTo",
72         "RangeToInclusive",
73     ];
74     if !range_types.contains(&&*type_name.as_str()) {
75         return None;
76     }
77
78     /// Find the field named `name` in the field. Always return `Some` for
79     /// convenience.
80     fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr> {
81         let expr = &fields.iter().find(|field| field.ident.name == name)?.expr;
82
83         Some(expr)
84     }
85
86     // The range syntax is expanded to literal paths starting with `core` or `std`
87     // depending on
88     // `#[no_std]`. Testing both instead of resolving the paths.
89
90     match expr.node {
91         hir::ExprKind::Path(ref path) => {
92             if match_qpath(path, &paths::RANGE_FULL_STD) || match_qpath(path, &paths::RANGE_FULL) {
93                 Some(Range {
94                     start: None,
95                     end: None,
96                     limits: ast::RangeLimits::HalfOpen,
97                 })
98             } else {
99                 None
100             }
101         },
102         hir::ExprKind::Call(ref path, ref args) => if let hir::ExprKind::Path(ref path) = path.node {
103             if match_qpath(path, &paths::RANGE_INCLUSIVE_STD_NEW) || match_qpath(path, &paths::RANGE_INCLUSIVE_NEW) {
104                 Some(Range {
105                     start: Some(&args[0]),
106                     end: Some(&args[1]),
107                     limits: ast::RangeLimits::Closed,
108                 })
109             } else {
110                 None
111             }
112         } else {
113             None
114         },
115         hir::ExprKind::Struct(ref path, ref fields, None) => if match_qpath(path, &paths::RANGE_FROM_STD)
116             || match_qpath(path, &paths::RANGE_FROM)
117         {
118             Some(Range {
119                 start: Some(get_field("start", fields)?),
120                 end: None,
121                 limits: ast::RangeLimits::HalfOpen,
122             })
123         } else if match_qpath(path, &paths::RANGE_STD) || match_qpath(path, &paths::RANGE) {
124             Some(Range {
125                 start: Some(get_field("start", fields)?),
126                 end: Some(get_field("end", fields)?),
127                 limits: ast::RangeLimits::HalfOpen,
128             })
129         } else if match_qpath(path, &paths::RANGE_TO_INCLUSIVE_STD) || match_qpath(path, &paths::RANGE_TO_INCLUSIVE) {
130             Some(Range {
131                 start: None,
132                 end: Some(get_field("end", fields)?),
133                 limits: ast::RangeLimits::Closed,
134             })
135         } else if match_qpath(path, &paths::RANGE_TO_STD) || match_qpath(path, &paths::RANGE_TO) {
136             Some(Range {
137                 start: None,
138                 end: Some(get_field("end", fields)?),
139                 limits: ast::RangeLimits::HalfOpen,
140             })
141         } else {
142             None
143         },
144         _ => None,
145     }
146 }
147
148 /// Checks if a `let` decl is from a `for` loop desugaring.
149 pub fn is_from_for_desugar(decl: &hir::Decl) -> bool {
150     // This will detect plain for-loops without an actual variable binding:
151     //
152     // ```
153     // for x in some_vec {
154     //   // do stuff
155     // }
156     // ```
157     if_chain! {
158         if let hir::DeclKind::Local(ref loc) = decl.node;
159         if let Some(ref expr) = loc.init;
160         if let hir::ExprKind::Match(_, _, hir::MatchSource::ForLoopDesugar) = expr.node;
161         then {
162             return true;
163         }
164     }
165
166     // This detects a variable binding in for loop to avoid `let_unit_value`
167     // lint (see issue #1964).
168     //
169     // ```
170     // for _ in vec![()] {
171     //   // anything
172     // }
173     // ```
174     if_chain! {
175         if let hir::DeclKind::Local(ref loc) = decl.node;
176         if let hir::LocalSource::ForLoopDesugar = loc.source;
177         then {
178             return true;
179         }
180     }
181
182     false
183 }
184
185 /// Recover the essential nodes of a desugared for loop:
186 /// `for pat in arg { body }` becomes `(pat, arg, body)`.
187 pub fn for_loop(expr: &hir::Expr) -> Option<(&hir::Pat, &hir::Expr, &hir::Expr)> {
188     if_chain! {
189         if let hir::ExprKind::Match(ref iterexpr, ref arms, hir::MatchSource::ForLoopDesugar) = expr.node;
190         if let hir::ExprKind::Call(_, ref iterargs) = iterexpr.node;
191         if iterargs.len() == 1 && arms.len() == 1 && arms[0].guard.is_none();
192         if let hir::ExprKind::Loop(ref block, _, _) = arms[0].body.node;
193         if block.expr.is_none();
194         if let [ _, _, ref let_stmt, ref body ] = *block.stmts;
195         if let hir::StmtKind::Decl(ref decl, _) = let_stmt.node;
196         if let hir::DeclKind::Local(ref decl) = decl.node;
197         if let hir::StmtKind::Expr(ref expr, _) = body.node;
198         then {
199             return Some((&*decl.pat, &iterargs[0], expr));
200         }
201     }
202     None
203 }
204
205 /// Represent the pre-expansion arguments of a `vec!` invocation.
206 pub enum VecArgs<'a> {
207     /// `vec![elem; len]`
208     Repeat(&'a hir::Expr, &'a hir::Expr),
209     /// `vec![a, b, c]`
210     Vec(&'a [hir::Expr]),
211 }
212
213 /// Returns the arguments of the `vec!` macro if this expression was expanded
214 /// from `vec!`.
215 pub fn vec_macro<'e>(cx: &LateContext<'_, '_>, expr: &'e hir::Expr) -> Option<VecArgs<'e>> {
216     if_chain! {
217         if let hir::ExprKind::Call(ref fun, ref args) = expr.node;
218         if let hir::ExprKind::Path(ref path) = fun.node;
219         if is_expn_of(fun.span, "vec").is_some();
220         if let Some(fun_def_id) = opt_def_id(resolve_node(cx, path, fun.hir_id));
221         then {
222             return if match_def_path(cx.tcx, fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 {
223                 // `vec![elem; size]` case
224                 Some(VecArgs::Repeat(&args[0], &args[1]))
225             }
226             else if match_def_path(cx.tcx, fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 {
227                 // `vec![a, b, c]` case
228                 if_chain! {
229                     if let hir::ExprKind::Box(ref boxed) = args[0].node;
230                     if let hir::ExprKind::Array(ref args) = boxed.node;
231                     then {
232                         return Some(VecArgs::Vec(&*args));
233                     }
234                 }
235
236                 None
237             }
238             else {
239                 None
240             };
241         }
242     }
243
244     None
245 }