Migrate SuggestAccessingField
This commit is contained in:
parent
b36abea285
commit
d56b304bc8
3 changed files with 35 additions and 22 deletions
|
@ -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_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_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}`
|
||||||
|
|
|
@ -1289,3 +1289,31 @@ pub enum SuggestAsRefWhereAppropriate<'a> {
|
||||||
snippet: &'a str,
|
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>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@ use rustc_target::abi::FieldIdx;
|
||||||
|
|
||||||
use crate::errors::{
|
use crate::errors::{
|
||||||
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
|
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
|
||||||
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAsRefWhereAppropriate,
|
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField,
|
||||||
SuggestRemoveSemiOrReturnBinding,
|
SuggestAsRefWhereAppropriate, SuggestRemoveSemiOrReturnBinding,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::TypeErrCtxt;
|
use super::TypeErrCtxt;
|
||||||
|
@ -264,15 +264,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
pub(super) fn suggest_accessing_field_where_appropriate(
|
||||||
&self,
|
&self,
|
||||||
cause: &ObligationCause<'tcx>,
|
cause: &ObligationCause<'tcx>,
|
||||||
|
@ -299,21 +290,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
if let ObligationCauseCode::Pattern { span: Some(span), .. } = *cause.code() {
|
if let ObligationCauseCode::Pattern { span: Some(span), .. } = *cause.code() {
|
||||||
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
|
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
|
||||||
let suggestion = if expected_def.is_struct() {
|
let suggestion = if expected_def.is_struct() {
|
||||||
format!("{}.{}", snippet, name)
|
SuggestAccessingField::Safe { span, snippet, name, ty }
|
||||||
} else if expected_def.is_union() {
|
} else if expected_def.is_union() {
|
||||||
format!("unsafe {{ {}.{} }}", snippet, name)
|
SuggestAccessingField::Unsafe { span, snippet, name, ty }
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
diag.span_suggestion(
|
diag.subdiagnostic(suggestion);
|
||||||
span,
|
|
||||||
&format!(
|
|
||||||
"you might have meant to use field `{}` whose type is `{}`",
|
|
||||||
name, ty
|
|
||||||
),
|
|
||||||
suggestion,
|
|
||||||
Applicability::MaybeIncorrect,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue