mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-25 15:04:33 +00:00
Suggest better place to add call parentheses for method expressions wrapped in parentheses
This commit is contained in:
parent
207d9558d0
commit
68147ebd60
@ -1844,6 +1844,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
field,
|
||||
expr_t,
|
||||
expr,
|
||||
None,
|
||||
);
|
||||
}
|
||||
err.emit();
|
||||
@ -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![
|
||||
@ -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");
|
||||
|
@ -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(
|
||||
@ -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 },
|
||||
|
9
src/test/ui/typeck/issue-89044-wrapped-expr-method.fixed
Normal file
9
src/test/ui/typeck/issue-89044-wrapped-expr-method.fixed
Normal file
@ -0,0 +1,9 @@
|
||||
// run-rustfix
|
||||
|
||||
fn main() {
|
||||
let a = Some(42);
|
||||
println!(
|
||||
"The value is {}.",
|
||||
(a.unwrap()) //~ERROR [E0615]
|
||||
);
|
||||
}
|
9
src/test/ui/typeck/issue-89044-wrapped-expr-method.rs
Normal file
9
src/test/ui/typeck/issue-89044-wrapped-expr-method.rs
Normal file
@ -0,0 +1,9 @@
|
||||
// run-rustfix
|
||||
|
||||
fn main() {
|
||||
let a = Some(42);
|
||||
println!(
|
||||
"The value is {}.",
|
||||
(a.unwrap) //~ERROR [E0615]
|
||||
);
|
||||
}
|
14
src/test/ui/typeck/issue-89044-wrapped-expr-method.stderr
Normal file
14
src/test/ui/typeck/issue-89044-wrapped-expr-method.stderr
Normal file
@ -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`.
|
Loading…
Reference in New Issue
Block a user