Skip to content

Commit 441046a

Browse files
authored
Rollup merge of #89055 - Kobzol:wrapped-method-expr-call-parens, r=wesleywiser
Suggest better place to add call parentheses for method expressions wrapped in parentheses I wanted to improve the suggestion a bit to both remove the wrapping parentheses **and** add call parentheses by both calling `suggest_method_call` and using `multipart_suggestion`. But I very quickly ran into a problem where multiple overlapping machine applicable suggestions cannot be properly applied together. So I applied the suggestion from the issue and only added the call parentheses directly after the expression. Fixes: #89044
2 parents e4dbe27 + 68147eb commit 441046a

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
@@ -1860,6 +1860,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18601860
field,
18611861
expr_t,
18621862
expr,
1863+
None,
18631864
);
18641865
}
18651866
err.emit();
@@ -1886,9 +1887,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18861887
};
18871888
let expr_snippet =
18881889
self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or(String::new());
1889-
if expr_is_call && expr_snippet.starts_with("(") && expr_snippet.ends_with(")") {
1890-
let after_open = expr.span.lo() + rustc_span::BytePos(1);
1891-
let before_close = expr.span.hi() - rustc_span::BytePos(1);
1890+
let is_wrapped = expr_snippet.starts_with("(") && expr_snippet.ends_with(")");
1891+
let after_open = expr.span.lo() + rustc_span::BytePos(1);
1892+
let before_close = expr.span.hi() - rustc_span::BytePos(1);
1893+
1894+
if expr_is_call && is_wrapped {
18921895
err.multipart_suggestion(
18931896
"remove wrapping parentheses to call the method",
18941897
vec![
@@ -1898,12 +1901,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18981901
Applicability::MachineApplicable,
18991902
);
19001903
} else if !self.expr_in_place(expr.hir_id) {
1904+
// Suggest call parentheses inside the wrapping parentheses
1905+
let span = if is_wrapped {
1906+
expr.span.with_lo(after_open).with_hi(before_close)
1907+
} else {
1908+
expr.span
1909+
};
19011910
self.suggest_method_call(
19021911
&mut err,
19031912
"use parentheses to call the method",
19041913
field,
19051914
expr_t,
19061915
expr,
1916+
Some(span),
19071917
);
19081918
} else {
19091919
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)