Skip to content

Commit

Permalink
Suggest better place to add call parentheses for method expressions w…
Browse files Browse the repository at this point in the history
…rapped in parentheses
  • Loading branch information
Kobzol committed Sep 18, 2021
1 parent 207d955 commit 68147eb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
16 changes: 13 additions & 3 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
field,
expr_t,
expr,
None,
);
}
err.emit();
Expand All @@ -1870,9 +1871,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
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);
let is_wrapped = 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);

if expr_is_call && is_wrapped {
err.multipart_suggestion(
"remove wrapping parentheses to call the method",
vec![
Expand All @@ -1882,12 +1885,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Applicability::MachineApplicable,
);
} else if !self.expr_in_place(expr.hir_id) {
// Suggest call parentheses inside the wrapping parentheses
let span = if is_wrapped {
expr.span.with_lo(after_open).with_hi(before_close)
} else {
expr.span
};
self.suggest_method_call(
&mut err,
"use parentheses to call the method",
field,
expr_t,
expr,
Some(span),
);
} else {
err.help("methods are immutable and cannot be assigned to");
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_typeck/src/check/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
method_name: Ident,
self_ty: Ty<'tcx>,
call_expr: &hir::Expr<'_>,
span: Option<Span>,
) {
let params = self
.probe_for_name(
Expand All @@ -159,7 +160,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.unwrap_or(0);

// Account for `foo.bar<T>`;
let sugg_span = call_expr.span.shrink_to_hi();
let sugg_span = span.unwrap_or_else(|| call_expr.span).shrink_to_hi();
let (suggestion, applicability) = (
format!("({})", (0..params).map(|_| "_").collect::<Vec<_>>().join(", ")),
if params > 0 { Applicability::HasPlaceholders } else { Applicability::MaybeIncorrect },
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/typeck/issue-89044-wrapped-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-89044-wrapped-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]
);
}
14 changes: 14 additions & 0 deletions src/test/ui/typeck/issue-89044-wrapped-expr-method.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0615]: attempted to take value of method `unwrap` on type `Option<{integer}>`
--> $DIR/issue-89044-wrapped-expr-method.rs:7:12
|
LL | (a.unwrap)
| ^^^^^^ method, not a field
|
help: use parentheses to call the method
|
LL | (a.unwrap())
| ++

error: aborting due to previous error

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

0 comments on commit 68147eb

Please sign in to comment.