diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2019-03-08 19:08:28 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2019-03-08 19:08:28 -0800 |
| commit | 9aa89b25232b79b93dc122e86b8071fb14fc4e04 (patch) | |
| tree | 255685f120065a92b642d6585bd5cff5cca7da42 /src | |
| parent | 2a65cbeea78c2c79b1030a0012cdea475104a44f (diff) | |
| download | rust-9aa89b25232b79b93dc122e86b8071fb14fc4e04.tar.gz rust-9aa89b25232b79b93dc122e86b8071fb14fc4e04.zip | |
When encountetring `||{}()`, suggest the likely intended `(||{})()`
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_typeck/check/callee.rs | 35 | ||||
| -rw-r--r-- | src/test/ui/suggestions/suggest-on-bare-closure-call.rs | 4 | ||||
| -rw-r--r-- | src/test/ui/suggestions/suggest-on-bare-closure-call.stderr | 15 |
3 files changed, 53 insertions, 1 deletions
diff --git a/src/librustc_typeck/check/callee.rs b/src/librustc_typeck/check/callee.rs index 0a4c0eb3aff..15ae39600f6 100644 --- a/src/librustc_typeck/check/callee.rs +++ b/src/librustc_typeck/check/callee.rs @@ -2,7 +2,7 @@ use super::autoderef::Autoderef; use super::method::MethodCallee; use super::{Expectation, FnCtxt, Needs, TupleArgumentsFlag}; -use errors::Applicability; +use errors::{Applicability, DiagnosticBuilder}; use hir::def::Def; use hir::def_id::{DefId, LOCAL_CRATE}; use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability}; @@ -232,6 +232,32 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { None } + /// Give appropriate suggestion when encountering `||{/* not callable */}()`, where the + /// likely intention is to call the closure, suggest `(||{})()`. (#55851) + fn identify_bad_closure_def_and_call( + &self, + err: &mut DiagnosticBuilder<'a>, + hir_id: hir::HirId, + callee_node: &hir::ExprKind, + callee_span: Span, + ) { + let hir_id = self.tcx.hir().get_parent_node_by_hir_id(hir_id); + let parent_node = self.tcx.hir().get_by_hir_id(hir_id); + if let ( + hir::Node::Expr(hir::Expr { node: hir::ExprKind::Closure(_, _, _, sp, ..), .. }), + hir::ExprKind::Block(..), + ) = (parent_node, callee_node) { + let start = sp.shrink_to_lo(); + let end = self.tcx.sess.source_map().next_point(callee_span); + err.multipart_suggestion( + "if you meant to create this closure and immediately call it, surround the \ + closure with parenthesis", + vec![(start, "(".to_string()), (end, ")".to_string())], + Applicability::MaybeIncorrect, + ); + } + } + fn confirm_builtin_call( &self, call_expr: &hir::Expr, @@ -268,6 +294,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } ); + self.identify_bad_closure_def_and_call( + &mut err, + call_expr.hir_id, + &callee.node, + callee.span, + ); + if let Some(ref path) = unit_variant { err.span_suggestion( call_expr.span, diff --git a/src/test/ui/suggestions/suggest-on-bare-closure-call.rs b/src/test/ui/suggestions/suggest-on-bare-closure-call.rs new file mode 100644 index 00000000000..355708c08ec --- /dev/null +++ b/src/test/ui/suggestions/suggest-on-bare-closure-call.rs @@ -0,0 +1,4 @@ +fn main() { + let _ = ||{}(); + //~^ ERROR expected function, found `()` +} diff --git a/src/test/ui/suggestions/suggest-on-bare-closure-call.stderr b/src/test/ui/suggestions/suggest-on-bare-closure-call.stderr new file mode 100644 index 00000000000..17001e3974c --- /dev/null +++ b/src/test/ui/suggestions/suggest-on-bare-closure-call.stderr @@ -0,0 +1,15 @@ +error[E0618]: expected function, found `()` + --> $DIR/suggest-on-bare-closure-call.rs:2:15 + | +LL | let _ = ||{}(); + | ^^-- + | | + | call expression requires function +help: if you meant to create this closure and immediately call it, surround the closure with parenthesis + | +LL | let _ = (||{})(); + | ^ ^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0618`. |
