-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fix suggestion for removing &mut from &mut macro!(). #85939
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ use rustc_span::Span; | |
use super::method::probe; | ||
|
||
use std::fmt; | ||
use std::iter; | ||
|
||
impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | ||
pub fn emit_coerce_suggestions( | ||
|
@@ -577,12 +578,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
// We have `&T`, check if what was expected was `T`. If so, | ||
// we may want to suggest removing a `&`. | ||
if sm.is_imported(expr.span) { | ||
if let Ok(src) = sm.span_to_snippet(sp) { | ||
if let Some(src) = src.strip_prefix('&') { | ||
// Go through the spans from which this span was expanded, | ||
// and find the one that's pointing inside `sp`. | ||
// | ||
// E.g. for `&format!("")`, where we want the span to the | ||
// `format!()` invocation instead of its expansion. | ||
if let Some(call_span) = | ||
iter::successors(Some(expr.span), |s| s.parent()).find(|&s| sp.contains(s)) | ||
{ | ||
if let Ok(code) = sm.span_to_snippet(call_span) { | ||
return Some(( | ||
sp, | ||
"consider removing the borrow", | ||
src.to_string(), | ||
code, | ||
Comment on lines
-585
to
+593
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In another diagnostic, I emitted (potentially multiple) suggestions for removing the parts that need to be removed, instead of a single suggestion that contains a copy of (part of) the original source. Is there a guideline for which to prefer? Basically: Most diagnostics seem to use A, which surprises me a bit. The suggestion for A can get quite large for large expressions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The later didn't exist until "recently" (2019?), so there's a historical component. There's also a bug that needs to be fixed on rustc's side to allow rustfix apply multipart suggestions, which it can't today. Even with that caveat, I push for multipart suggestions in all new diagnostics when possible/necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks!
That should be fixed by rust-lang/rustfix#195 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! That should also allow us to add a |
||
Applicability::MachineApplicable, | ||
)); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@estebank What do you think about adding this as a function on
Span
(e.g.span.find_parent_within(span)
or something)?I have a feeling more diagnostics can/should use this, but I haven't written enough diagnostics yet to know if that's true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your assessment and we should have something like that (bikeshed notwithstanding).