Skip to content

Commit 68147eb

Browse files
committed
Suggest better place to add call parentheses for method expressions wrapped in parentheses
1 parent 207d955 commit 68147eb

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

compiler/rustc_typeck/src/check/expr.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18441844
field,
18451845
expr_t,
18461846
expr,
1847+
None,
18471848
);
18481849
}
18491850
err.emit();
@@ -1870,9 +1871,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18701871
};
18711872
let expr_snippet =
18721873
self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or(String::new());
1873-
if expr_is_call && expr_snippet.starts_with("(") && expr_snippet.ends_with(")") {
1874-
let after_open = expr.span.lo() + rustc_span::BytePos(1);
1875-
let before_close = expr.span.hi() - rustc_span::BytePos(1);
1874+
let is_wrapped = expr_snippet.starts_with("(") && expr_snippet.ends_with(")");
1875+
let after_open = expr.span.lo() + rustc_span::BytePos(1);
1876+
let before_close = expr.span.hi() - rustc_span::BytePos(1);
1877+
1878+
if expr_is_call && is_wrapped {
18761879
err.multipart_suggestion(
18771880
"remove wrapping parentheses to call the method",
18781881
vec![
@@ -1882,12 +1885,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18821885
Applicability::MachineApplicable,
18831886
);
18841887
} else if !self.expr_in_place(expr.hir_id) {
1888+
// Suggest call parentheses inside the wrapping parentheses
1889+
let span = if is_wrapped {
1890+
expr.span.with_lo(after_open).with_hi(before_close)
1891+
} else {
1892+
expr.span
1893+
};
18851894
self.suggest_method_call(
18861895
&mut err,
18871896
"use parentheses to call the method",
18881897
field,
18891898
expr_t,
18901899
expr,
1900+
Some(span),
18911901
);
18921902
} else {
18931903
err.help("methods are immutable and cannot be assigned to");

compiler/rustc_typeck/src/check/method/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
141141
method_name: Ident,
142142
self_ty: Ty<'tcx>,
143143
call_expr: &hir::Expr<'_>,
144+
span: Option<Span>,
144145
) {
145146
let params = self
146147
.probe_for_name(
@@ -159,7 +160,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
159160
.unwrap_or(0);
160161

161162
// Account for `foo.bar<T>`;
162-
let sugg_span = call_expr.span.shrink_to_hi();
163+
let sugg_span = span.unwrap_or_else(|| call_expr.span).shrink_to_hi();
163164
let (suggestion, applicability) = (
164165
format!("({})", (0..params).map(|_| "_").collect::<Vec<_>>().join(", ")),
165166
if params > 0 { Applicability::HasPlaceholders } else { Applicability::MaybeIncorrect },
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,14 @@
1+
error[E0615]: attempted to take value of method `unwrap` on type `Option<{integer}>`
2+
--> $DIR/issue-89044-wrapped-expr-method.rs:7:12
3+
|
4+
LL | (a.unwrap)
5+
| ^^^^^^ method, not a field
6+
|
7+
help: use parentheses to call the method
8+
|
9+
LL | (a.unwrap())
10+
| ++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0615`.

0 commit comments

Comments
 (0)