}
}
+ /// Give appropriate suggestion when encountering `[("a", 0) ("b", 1)]`, where the
+ /// likely intention is to create an array containing tuples.
+ fn maybe_suggest_bad_array_definition(
+ &self,
+ err: &mut DiagnosticBuilder<'a>,
+ call_expr: &'tcx hir::Expr<'tcx>,
+ callee_expr: &'tcx hir::Expr<'tcx>,
+ ) -> bool {
+ let hir_id = self.tcx.hir().get_parent_node(call_expr.hir_id);
+ let parent_node = self.tcx.hir().get(hir_id);
+ if let (
+ hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Array(_), .. }),
+ hir::ExprKind::Tup(exp),
+ hir::ExprKind::Call(_, args),
+ ) = (parent_node, &callee_expr.kind, &call_expr.kind)
+ {
+ if args.len() == exp.len() {
+ let start = callee_expr.span.shrink_to_hi();
+ err.span_suggestion(
+ start,
+ "consider separating array elements with a comma",
+ ",".to_string(),
+ Applicability::MaybeIncorrect,
+ );
+ return true;
+ }
+ }
+ false
+ }
+
fn confirm_builtin_call(
&self,
call_expr: &'tcx hir::Expr<'tcx>,
_ => Res::Err,
};
- err.span_label(call_expr.span, "call expression requires function");
+ if !self.maybe_suggest_bad_array_definition(&mut err, call_expr, callee_expr) {
+ err.span_label(call_expr.span, "call expression requires function");
+ }
if let Some(span) = self.tcx.hir().res_span(def) {
let callee_ty = callee_ty.to_string();
error[E0618]: expected function, found `(char, char)`
--> $DIR/issue-5100.rs:48:14
|
-LL | let v = [('a', 'b')
- | ______________-^^^^^^^^^
-LL | | ('c', 'd'),
- | |_______________________- call expression requires function
+LL | let v = [('a', 'b')
+ | ^^^^^^^^^^- help: consider separating array elements with a comma: `,`
error[E0308]: mismatched types
--> $DIR/issue-5100.rs:55:19
--- /dev/null
+error[E0618]: expected function, found `(&'static str, {integer})`
+ --> $DIR/array-diagnostics.rs:4:9
+ |
+LL | ("C200B40A83", 4)
+ | ^^^^^^^^^^^^^^^^^- help: consider separating array elements with a comma: `,`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0618`.