diff options
Diffstat (limited to 'crates/hir_ty')
-rw-r--r-- | crates/hir_ty/src/diagnostics.rs | 48 | ||||
-rw-r--r-- | crates/hir_ty/src/diagnostics/expr.rs | 70 |
2 files changed, 105 insertions, 13 deletions
diff --git a/crates/hir_ty/src/diagnostics.rs b/crates/hir_ty/src/diagnostics.rs index 247da43f2..b4cf81c10 100644 --- a/crates/hir_ty/src/diagnostics.rs +++ b/crates/hir_ty/src/diagnostics.rs | |||
@@ -247,7 +247,7 @@ impl Diagnostic for RemoveThisSemicolon { | |||
247 | 247 | ||
248 | // Diagnostic: break-outside-of-loop | 248 | // Diagnostic: break-outside-of-loop |
249 | // | 249 | // |
250 | // This diagnostic is triggered if `break` keyword is used outside of a loop. | 250 | // This diagnostic is triggered if the `break` keyword is used outside of a loop. |
251 | #[derive(Debug)] | 251 | #[derive(Debug)] |
252 | pub struct BreakOutsideOfLoop { | 252 | pub struct BreakOutsideOfLoop { |
253 | pub file: HirFileId, | 253 | pub file: HirFileId, |
@@ -271,7 +271,7 @@ impl Diagnostic for BreakOutsideOfLoop { | |||
271 | 271 | ||
272 | // Diagnostic: missing-unsafe | 272 | // Diagnostic: missing-unsafe |
273 | // | 273 | // |
274 | // This diagnostic is triggered if operation marked as `unsafe` is used outside of `unsafe` function or block. | 274 | // This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block. |
275 | #[derive(Debug)] | 275 | #[derive(Debug)] |
276 | pub struct MissingUnsafe { | 276 | pub struct MissingUnsafe { |
277 | pub file: HirFileId, | 277 | pub file: HirFileId, |
@@ -295,7 +295,7 @@ impl Diagnostic for MissingUnsafe { | |||
295 | 295 | ||
296 | // Diagnostic: mismatched-arg-count | 296 | // Diagnostic: mismatched-arg-count |
297 | // | 297 | // |
298 | // This diagnostic is triggered if function is invoked with an incorrect amount of arguments. | 298 | // This diagnostic is triggered if a function is invoked with an incorrect amount of arguments. |
299 | #[derive(Debug)] | 299 | #[derive(Debug)] |
300 | pub struct MismatchedArgCount { | 300 | pub struct MismatchedArgCount { |
301 | pub file: HirFileId, | 301 | pub file: HirFileId, |
@@ -347,7 +347,7 @@ impl fmt::Display for CaseType { | |||
347 | 347 | ||
348 | // Diagnostic: incorrect-ident-case | 348 | // Diagnostic: incorrect-ident-case |
349 | // | 349 | // |
350 | // This diagnostic is triggered if item name doesn't follow https://doc.rust-lang.org/1.0.0/style/style/naming/README.html[Rust naming convention]. | 350 | // This diagnostic is triggered if an item name doesn't follow https://doc.rust-lang.org/1.0.0/style/style/naming/README.html[Rust naming convention]. |
351 | #[derive(Debug)] | 351 | #[derive(Debug)] |
352 | pub struct IncorrectCase { | 352 | pub struct IncorrectCase { |
353 | pub file: HirFileId, | 353 | pub file: HirFileId, |
@@ -386,6 +386,31 @@ impl Diagnostic for IncorrectCase { | |||
386 | } | 386 | } |
387 | } | 387 | } |
388 | 388 | ||
389 | // Diagnostic: replace-filter-map-next-with-find-map | ||
390 | // | ||
391 | // This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`. | ||
392 | #[derive(Debug)] | ||
393 | pub struct ReplaceFilterMapNextWithFindMap { | ||
394 | pub file: HirFileId, | ||
395 | pub filter_map_expr: AstPtr<ast::Expr>, | ||
396 | pub next_expr: AstPtr<ast::Expr>, | ||
397 | } | ||
398 | |||
399 | impl Diagnostic for ReplaceFilterMapNextWithFindMap { | ||
400 | fn code(&self) -> DiagnosticCode { | ||
401 | DiagnosticCode("replace-filter-map-next-with-find-map") | ||
402 | } | ||
403 | fn message(&self) -> String { | ||
404 | "replace filter_map(..).next() with find_map(..)".to_string() | ||
405 | } | ||
406 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
407 | InFile { file_id: self.file, value: self.filter_map_expr.clone().into() } | ||
408 | } | ||
409 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
410 | self | ||
411 | } | ||
412 | } | ||
413 | |||
389 | #[cfg(test)] | 414 | #[cfg(test)] |
390 | mod tests { | 415 | mod tests { |
391 | use base_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; | 416 | use base_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; |
@@ -644,4 +669,19 @@ fn foo() { break; } | |||
644 | "#, | 669 | "#, |
645 | ); | 670 | ); |
646 | } | 671 | } |
672 | |||
673 | #[test] | ||
674 | fn replace_missing_filter_next_with_find_map() { | ||
675 | check_diagnostics( | ||
676 | r#" | ||
677 | fn foo() { | ||
678 | let m = [1, 2, 3] | ||
679 | .iter() | ||
680 | .filter_map(|x| if *x == 2 { Some (4) } else { None }) | ||
681 | .next(); | ||
682 | //^^^ Replace .filter_map(..).next() with .find_map(..) | ||
683 | } | ||
684 | "#, | ||
685 | ); | ||
686 | } | ||
647 | } | 687 | } |
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( |