Skip to content

Commit 5dab3c5

Browse files
committed
feat(rustc_typeck): suggest removing bad parens in (recv.method)()
Fixes #88803
1 parent b69fe57 commit 5dab3c5

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

compiler/rustc_typeck/src/check/expr.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18421842
expr_t
18431843
);
18441844
err.span_label(field.span, "method, not a field");
1845-
if !self.expr_in_place(expr.hir_id) {
1845+
let expr_is_call = if let hir::Node::Expr(parent_expr) =
1846+
self.tcx.hir().get(self.tcx.hir().get_parent_node(expr.hir_id))
1847+
{
1848+
matches!(parent_expr.kind, ExprKind::Call(..))
1849+
} else {
1850+
false
1851+
};
1852+
let expr_snippet =
1853+
self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or(String::new());
1854+
if expr_is_call && expr_snippet.starts_with("(") && expr_snippet.ends_with(")") {
1855+
err.span_suggestion_short(
1856+
expr.span,
1857+
"remove wrapping parentheses to call the method",
1858+
expr_snippet[1..expr_snippet.len() - 1].to_owned(),
1859+
Applicability::MachineApplicable,
1860+
);
1861+
} else if !self.expr_in_place(expr.hir_id) {
18461862
self.suggest_method_call(
18471863
&mut err,
18481864
"use parentheses to call the method",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let a = Some(42);
3+
println!(
4+
"The value is {}.",
5+
(a.unwrap)() //~ERROR [E0615]
6+
);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0615]: attempted to take value of method `unwrap` on type `Option<{integer}>`
2+
--> $DIR/issue-88803-call-expr-method.rs:5:12
3+
|
4+
LL | (a.unwrap)()
5+
| ---^^^^^^-
6+
| | |
7+
| | method, not a field
8+
| help: remove wrapping parentheses to call the method
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0615`.

0 commit comments

Comments
 (0)