Skip to content

Commit 1bf94a1

Browse files
authored
Rollup merge of rust-lang#88841 - notriddle:notriddle/method-parens, r=estebank
feat(rustc_typeck): suggest removing bad parens in `(recv.method)()` Fixes rust-lang#88803
2 parents fb2d7df + 8be729c commit 1bf94a1

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

compiler/rustc_typeck/src/check/expr.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,28 @@ 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 =
1846+
if let hir::Node::Expr(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) =
1847+
self.tcx.hir().get(self.tcx.hir().get_parent_node(expr.hir_id))
1848+
{
1849+
expr.hir_id == callee.hir_id
1850+
} else {
1851+
false
1852+
};
1853+
let expr_snippet =
1854+
self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or(String::new());
1855+
if expr_is_call && expr_snippet.starts_with("(") && expr_snippet.ends_with(")") {
1856+
let after_open = expr.span.lo() + rustc_span::BytePos(1);
1857+
let before_close = expr.span.hi() - rustc_span::BytePos(1);
1858+
err.multipart_suggestion(
1859+
"remove wrapping parentheses to call the method",
1860+
vec![
1861+
(expr.span.with_hi(after_open), String::new()),
1862+
(expr.span.with_lo(before_close), String::new()),
1863+
],
1864+
Applicability::MachineApplicable,
1865+
);
1866+
} else if !self.expr_in_place(expr.hir_id) {
18461867
self.suggest_method_call(
18471868
&mut err,
18481869
"use parentheses to call the method",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
let a = Some(42);
5+
println!(
6+
"The value is {}.",
7+
a.unwrap() //~ERROR [E0615]
8+
);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
let a = Some(42);
5+
println!(
6+
"The value is {}.",
7+
(a.unwrap)() //~ERROR [E0615]
8+
);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0615]: attempted to take value of method `unwrap` on type `Option<{integer}>`
2+
--> $DIR/issue-88803-call-expr-method.rs:7:12
3+
|
4+
LL | (a.unwrap)()
5+
| ^^^^^^ method, not a field
6+
|
7+
help: remove wrapping parentheses to call the method
8+
|
9+
LL - (a.unwrap)()
10+
LL + a.unwrap()
11+
|
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0615`.

0 commit comments

Comments
 (0)