Open
Description
While checking span_help
s that could be span_suggestion
I noticed that rustc contains lots of code similar to
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
Ok(s) => {
err.span_suggestion(self.cast_span,
"try casting to a reference instead",
format!("&{}{}", mtstr, s));
}
Err(_) => {
span_help!(err, self.cast_span, "did you mean `&{}{}`?", mtstr, tstr)
}
}
where we try to get a snippet and if that fails, since we can't produce a nice suggestion, we produce a help
message that contains a message.
We should probably provide a helper for that. First I thought that we could add a helper that does essentially the above without all the duplication, but with the new approximate suggestions (#47540) we can always produce the suggestion, but mark it as approximate if we need to use the fallback value.
I'd assume the above example would look something like this:
err.span_possibly_approximate_suggestion(
self.cast_span, // span to replace
"try casting to a reference instead", // message
fcx.tcx.sess.codemap().span_to_snippet(self.cast_span).ok(), // optional snippet
tstr, // default if snippet is none
|snip| format!("&{}{}", mtstr, snip), // closure taking snippet and producing the replacement code
);
cc @Manishearth @nrc