diff options
author | Phil Ellison <[email protected]> | 2020-12-28 13:41:15 +0000 |
---|---|---|
committer | Phil Ellison <[email protected]> | 2021-01-23 07:40:24 +0000 |
commit | 1316422a7c2ef26e9da78fa23f170407b1cb39bb (patch) | |
tree | 1eea156207bcc920f0573ef51d7f6ad4fe03299d /crates/hir_ty/src/diagnostics | |
parent | eab5db20edd9604ba5d489fa8c6430eb7bac6610 (diff) |
Add diagnostic for filter_map followed by next
Diffstat (limited to 'crates/hir_ty/src/diagnostics')
-rw-r--r-- | crates/hir_ty/src/diagnostics/expr.rs | 70 |
1 files changed, 61 insertions, 9 deletions
diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 107417c27..170d23178 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs | |||
@@ -24,6 +24,8 @@ pub(crate) use hir_def::{ | |||
24 | LocalFieldId, VariantId, | 24 | LocalFieldId, VariantId, |
25 | }; | 25 | }; |
26 | 26 | ||
27 | use super::ReplaceFilterMapNextWithFindMap; | ||
28 | |||
27 | pub(super) struct ExprValidator<'a, 'b: 'a> { | 29 | pub(super) struct ExprValidator<'a, 'b: 'a> { |
28 | owner: DefWithBodyId, | 30 | owner: DefWithBodyId, |
29 | infer: Arc<InferenceResult>, | 31 | infer: Arc<InferenceResult>, |
@@ -39,7 +41,18 @@ impl<'a, 'b> ExprValidator<'a, 'b> { | |||
39 | ExprValidator { owner, infer, sink } | 41 | ExprValidator { owner, infer, sink } |
40 | } | 42 | } |
41 | 43 | ||
44 | fn bar() { | ||
45 | // LOOK FOR THIS | ||
46 | let m = [1, 2, 3] | ||
47 | .iter() | ||
48 | .filter_map(|x| if *x == 2 { Some(4) } else { None }) | ||
49 | .next(); | ||
50 | } | ||
51 | |||
42 | pub(super) fn validate_body(&mut self, db: &dyn HirDatabase) { | 52 | pub(super) fn validate_body(&mut self, db: &dyn HirDatabase) { |
53 | // DO NOT MERGE: just getting something working for now | ||
54 | self.check_for_filter_map_next(db); | ||
55 | |||
43 | let body = db.body(self.owner.into()); | 56 | let body = db.body(self.owner.into()); |
44 | 57 | ||
45 | for (id, expr) in body.exprs.iter() { | 58 | for (id, expr) in body.exprs.iter() { |
@@ -150,20 +163,58 @@ impl<'a, 'b> ExprValidator<'a, 'b> { | |||
150 | } | 163 | } |
151 | } | 164 | } |
152 | 165 | ||
153 | fn validate_call(&mut self, db: &dyn HirDatabase, call_id: ExprId, expr: &Expr) -> Option<()> { | 166 | fn check_for_filter_map_next(&mut self, db: &dyn HirDatabase) { |
167 | let body = db.body(self.owner.into()); | ||
168 | let mut prev = None; | ||
169 | |||
170 | for (id, expr) in body.exprs.iter() { | ||
171 | if let Expr::MethodCall { receiver, method_name, args, .. } = expr { | ||
172 | let method_name_hack_do_not_merge = format!("{}", method_name); | ||
173 | |||
174 | if method_name_hack_do_not_merge == "filter_map" && args.len() == 1 { | ||
175 | prev = Some((id, args[0])); | ||
176 | continue; | ||
177 | } | ||
178 | |||
179 | if method_name_hack_do_not_merge == "next" { | ||
180 | if let Some((filter_map_id, filter_map_args)) = prev { | ||
181 | if *receiver == filter_map_id { | ||
182 | let (_, source_map) = db.body_with_source_map(self.owner.into()); | ||
183 | if let (Ok(filter_map_source_ptr), Ok(next_source_ptr)) = ( | ||
184 | source_map.expr_syntax(filter_map_id), | ||
185 | source_map.expr_syntax(id), | ||
186 | ) { | ||
187 | self.sink.push(ReplaceFilterMapNextWithFindMap { | ||
188 | file: filter_map_source_ptr.file_id, | ||
189 | filter_map_expr: filter_map_source_ptr.value, | ||
190 | next_expr: next_source_ptr.value, | ||
191 | }); | ||
192 | } | ||
193 | } | ||
194 | } | ||
195 | } | ||
196 | } | ||
197 | prev = None; | ||
198 | } | ||
199 | } | ||
200 | |||
201 | fn validate_call(&mut self, db: &dyn HirDatabase, call_id: ExprId, expr: &Expr) { | ||
154 | // Check that the number of arguments matches the number of parameters. | 202 | // Check that the number of arguments matches the number of parameters. |
155 | 203 | ||
156 | // FIXME: Due to shortcomings in the current type system implementation, only emit this | 204 | // FIXME: Due to shortcomings in the current type system implementation, only emit this |
157 | // diagnostic if there are no type mismatches in the containing function. | 205 | // diagnostic if there are no type mismatches in the containing function. |
158 | if self.infer.type_mismatches.iter().next().is_some() { | 206 | if self.infer.type_mismatches.iter().next().is_some() { |
159 | return None; | 207 | return; |
160 | } | 208 | } |
161 | 209 | ||
162 | let is_method_call = matches!(expr, Expr::MethodCall { .. }); | 210 | let is_method_call = matches!(expr, Expr::MethodCall { .. }); |
163 | let (sig, args) = match expr { | 211 | let (sig, args) = match expr { |
164 | Expr::Call { callee, args } => { | 212 | Expr::Call { callee, args } => { |
165 | let callee = &self.infer.type_of_expr[*callee]; | 213 | let callee = &self.infer.type_of_expr[*callee]; |
166 | let sig = callee.callable_sig(db)?; | 214 | let sig = match callee.callable_sig(db) { |
215 | Some(sig) => sig, | ||
216 | None => return, | ||
217 | }; | ||
167 | (sig, args.clone()) | 218 | (sig, args.clone()) |
168 | } | 219 | } |
169 | Expr::MethodCall { receiver, args, .. } => { | 220 | Expr::MethodCall { receiver, args, .. } => { |
@@ -175,22 +226,25 @@ impl<'a, 'b> ExprValidator<'a, 'b> { | |||
175 | // if the receiver is of unknown type, it's very likely we | 226 | // if the receiver is of unknown type, it's very likely we |
176 | // don't know enough to correctly resolve the method call. | 227 | // don't know enough to correctly resolve the method call. |
177 | // This is kind of a band-aid for #6975. | 228 | // This is kind of a band-aid for #6975. |
178 | return None; | 229 | return; |
179 | } | 230 | } |
180 | 231 | ||
181 | // FIXME: note that we erase information about substs here. This | 232 | // FIXME: note that we erase information about substs here. This |
182 | // is not right, but, luckily, doesn't matter as we care only | 233 | // is not right, but, luckily, doesn't matter as we care only |
183 | // about the number of params | 234 | // about the number of params |
184 | let callee = self.infer.method_resolution(call_id)?; | 235 | let callee = match self.infer.method_resolution(call_id) { |
236 | Some(callee) => callee, | ||
237 | None => return, | ||
238 | }; | ||
185 | let sig = db.callable_item_signature(callee.into()).value; | 239 | let sig = db.callable_item_signature(callee.into()).value; |
186 | 240 | ||
187 | (sig, args) | 241 | (sig, args) |
188 | } | 242 | } |
189 | _ => return None, | 243 | _ => return, |
190 | }; | 244 | }; |
191 | 245 | ||
192 | if sig.is_varargs { | 246 | if sig.is_varargs { |
193 | return None; | 247 | return; |
194 | } | 248 | } |
195 | 249 | ||
196 | let params = sig.params(); | 250 | let params = sig.params(); |
@@ -213,8 +267,6 @@ impl<'a, 'b> ExprValidator<'a, 'b> { | |||
213 | }); | 267 | }); |
214 | } | 268 | } |
215 | } | 269 | } |
216 | |||
217 | None | ||
218 | } | 270 | } |
219 | 271 | ||
220 | fn validate_match( | 272 | fn validate_match( |