Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parentheses properly for method calls #109472

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,34 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
Applicability::MaybeIncorrect,
);
} else {
let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_mut;
let sugg_prefix = format!("&{}", if is_mut { "mut " } else { "" });
let sugg_msg = &format!(
"consider{} borrowing here",
if is_mut { " mutably" } else { "" }
);

// Issue #109436, we need to add parentheses properly for method calls
// for example, `foo.into()` should be `(&foo).into()`
if let Ok(snippet) = self
.tcx
.sess
.source_map()
.span_to_snippet(self.tcx.sess.source_map().next_point(span))
mu001999 marked this conversation as resolved.
Show resolved Hide resolved
{
if snippet == "." {
err.multipart_suggestion_verbose(
sugg_msg,
vec![
(span.shrink_to_lo(), format!("({}", sugg_prefix)),
(span.shrink_to_hi(), ")".to_string()),
],
Applicability::MaybeIncorrect,
);
return true;
}
}

// Issue #104961, we need to add parentheses properly for compond expressions
// for example, `x.starts_with("hi".to_string() + "you")`
// should be `x.starts_with(&("hi".to_string() + "you"))`
Expand All @@ -1374,14 +1402,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
_ => false,
};

let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_mut;
let span = if needs_parens { span } else { span.shrink_to_lo() };
let sugg_prefix = format!("&{}", if is_mut { "mut " } else { "" });
let sugg_msg = &format!(
"consider{} borrowing here",
if is_mut { " mutably" } else { "" }
);

let suggestions = if !needs_parens {
vec![(span.shrink_to_lo(), format!("{}", sugg_prefix))]
} else {
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/suggestions/issue-109436.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
struct Foo;
struct Bar;

impl From<&Foo> for Bar {
fn from(foo: &Foo) -> Bar {
Bar
}
}

fn main() {
let foo = Foo;
let b: Bar = foo.into(); //~ ERROR E0277
}
15 changes: 15 additions & 0 deletions tests/ui/suggestions/issue-109436.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0277]: the trait bound `Foo: Into<_>` is not satisfied
--> $DIR/issue-109436.rs:12:22
|
LL | let b: Bar = foo.into();
| ^^^^ the trait `~const Into<_>` is not implemented for `Foo`
|
= note: required for `Foo` to implement `Into<Bar>`
help: consider borrowing here
|
LL | let b: Bar = (&foo).into();
| ++ +

error: aborting due to previous error

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