Skip to content

Commit

Permalink
feat(rustc_typeck): suggest removing bad parens in (recv.method)()
Browse files Browse the repository at this point in the history
Fixes #88803
  • Loading branch information
notriddle committed Sep 11, 2021
1 parent b69fe57 commit 5dab3c5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
18 changes: 17 additions & 1 deletion compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr_t
);
err.span_label(field.span, "method, not a field");
if !self.expr_in_place(expr.hir_id) {
let expr_is_call = if let hir::Node::Expr(parent_expr) =
self.tcx.hir().get(self.tcx.hir().get_parent_node(expr.hir_id))
{
matches!(parent_expr.kind, ExprKind::Call(..))
} else {
false
};
let expr_snippet =
self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or(String::new());
if expr_is_call && expr_snippet.starts_with("(") && expr_snippet.ends_with(")") {
err.span_suggestion_short(
expr.span,
"remove wrapping parentheses to call the method",
expr_snippet[1..expr_snippet.len() - 1].to_owned(),
Applicability::MachineApplicable,
);
} else if !self.expr_in_place(expr.hir_id) {
self.suggest_method_call(
&mut err,
"use parentheses to call the method",
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/typeck/issue-88803-call-expr-method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let a = Some(42);
println!(
"The value is {}.",
(a.unwrap)() //~ERROR [E0615]
);
}
12 changes: 12 additions & 0 deletions src/test/ui/typeck/issue-88803-call-expr-method.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0615]: attempted to take value of method `unwrap` on type `Option<{integer}>`
--> $DIR/issue-88803-call-expr-method.rs:5:12
|
LL | (a.unwrap)()
| ---^^^^^^-
| | |
| | method, not a field
| help: remove wrapping parentheses to call the method

error: aborting due to previous error

For more information about this error, try `rustc --explain E0615`.

0 comments on commit 5dab3c5

Please sign in to comment.