1
Fork 0

Auto merge of #117934 - Young-Flash:dev, r=petrochenkov

feat: make `let_binding_suggestion` more reasonable

This is my first PR for rustc, which trying to fix https://github.com/rust-lang/rust/issues/117894, I am not familiar with some internal api so maybe some modification here isn't the way to go, appreciated for any review suggestion.
This commit is contained in:
bors 2023-11-24 15:26:04 +00:00
commit 4fd68eb47b
4 changed files with 62 additions and 7 deletions

View file

@ -2162,13 +2162,22 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
fn let_binding_suggestion(&mut self, err: &mut Diagnostic, ident_span: Span) -> bool {
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) =
self.diagnostic_metadata.in_assignment
&& let ast::ExprKind::Path(None, _) = lhs.kind
&& let ast::ExprKind::Path(None, ref path) = lhs.kind
{
if !ident_span.from_expansion() {
let (span, text) = match path.segments.first() {
Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
// a special case for #117894
let name = name.strip_prefix("_").unwrap_or(name);
(ident_span, format!("let {name}"))
}
_ => (ident_span.shrink_to_lo(), "let ".to_string()),
};
err.span_suggestion_verbose(
ident_span.shrink_to_lo(),
span,
"you might have meant to introduce a new binding",
"let ".to_string(),
text,
Applicability::MaybeIncorrect,
);
return true;