Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rustc_typeck): suggest removing bad parens in (recv.method)() #88841

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 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,28 @@ 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(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) =
self.tcx.hir().get(self.tcx.hir().get_parent_node(expr.hir_id))
{
expr.hir_id == callee.hir_id
} 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(")") {
let after_open = expr.span.lo() + rustc_span::BytePos(1);
let before_close = expr.span.hi() - rustc_span::BytePos(1);
err.multipart_suggestion(
"remove wrapping parentheses to call the method",
vec![
(expr.span.with_hi(after_open), String::new()),
(expr.span.with_lo(before_close), String::new()),
],
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
9 changes: 9 additions & 0 deletions src/test/ui/typeck/issue-88803-call-expr-method.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-rustfix

fn main() {
let a = Some(42);
println!(
"The value is {}.",
a.unwrap() //~ERROR [E0615]
);
}
9 changes: 9 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,9 @@
// run-rustfix

fn main() {
let a = Some(42);
println!(
"The value is {}.",
(a.unwrap)() //~ERROR [E0615]
);
}
15 changes: 15 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,15 @@
error[E0615]: attempted to take value of method `unwrap` on type `Option<{integer}>`
--> $DIR/issue-88803-call-expr-method.rs:7:12
|
LL | (a.unwrap)()
| ^^^^^^ method, not a field
|
help: remove wrapping parentheses to call the method
|
LL - (a.unwrap)()
LL + a.unwrap()
|

error: aborting due to previous error

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