Migrate SuggestAccessingField

This commit is contained in:
IQuant 2023-03-03 16:17:53 +03:00
parent b36abea285
commit d56b304bc8
3 changed files with 35 additions and 22 deletions

View File

@ -360,3 +360,5 @@ infer_fn_consider_casting = consider casting the fn item to a fn pointer: `{$cas
infer_sarwa_option = you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`
infer_sarwa_result = you can convert from `&Result<T, E>` to `Result<&T, &E>` using `.as_ref()`
infer_suggest_accessing_field = you might have meant to use field `{$name}` whose type is `{$ty}`

View File

@ -1289,3 +1289,31 @@ pub enum SuggestAsRefWhereAppropriate<'a> {
snippet: &'a str,
},
}
#[derive(Subdiagnostic)]
pub enum SuggestAccessingField<'a> {
#[suggestion(
infer_suggest_accessing_field,
code = "{snippet}.{name}",
applicability = "maybe-incorrect"
)]
Safe {
#[primary_span]
span: Span,
snippet: String,
name: Symbol,
ty: Ty<'a>,
},
#[suggestion(
infer_suggest_accessing_field,
code = "unsafe {{ {snippet}.{name} }}",
applicability = "maybe-incorrect"
)]
Unsafe {
#[primary_span]
span: Span,
snippet: String,
name: Symbol,
ty: Ty<'a>,
},
}

View File

@ -14,8 +14,8 @@
use crate::errors::{
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAsRefWhereAppropriate,
SuggestRemoveSemiOrReturnBinding,
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField,
SuggestAsRefWhereAppropriate, SuggestRemoveSemiOrReturnBinding,
};
use super::TypeErrCtxt;
@ -264,15 +264,6 @@ pub(super) fn suggest_await_on_expect_found(
}
}
pub fn suggest_await_on_future(&self, diag: &mut Diagnostic, sp: Span) {
diag.span_suggestion_verbose(
sp.shrink_to_hi(),
"consider `await`ing on the `Future`",
".await",
Applicability::MaybeIncorrect,
);
}
pub(super) fn suggest_accessing_field_where_appropriate(
&self,
cause: &ObligationCause<'tcx>,
@ -299,21 +290,13 @@ pub(super) fn suggest_accessing_field_where_appropriate(
if let ObligationCauseCode::Pattern { span: Some(span), .. } = *cause.code() {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
let suggestion = if expected_def.is_struct() {
format!("{}.{}", snippet, name)
SuggestAccessingField::Safe { span, snippet, name, ty }
} else if expected_def.is_union() {
format!("unsafe {{ {}.{} }}", snippet, name)
SuggestAccessingField::Unsafe { span, snippet, name, ty }
} else {
return;
};
diag.span_suggestion(
span,
&format!(
"you might have meant to use field `{}` whose type is `{}`",
name, ty
),
suggestion,
Applicability::MaybeIncorrect,
);
diag.subdiagnostic(suggestion);
}
}
}