More robust as_ref/as_deref suggestions
This commit is contained in:
parent
522ae84e03
commit
af54d584b2
12 changed files with 208 additions and 153 deletions
|
@ -278,9 +278,6 @@ infer_ril_introduced_by = requirement introduced by this return type
|
|||
infer_ril_introduced_here = `'static` requirement introduced here
|
||||
infer_ril_static_introduced_by = "`'static` lifetime requirement introduced by the return type
|
||||
|
||||
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_sbfrit_box_return_expr = if you change the return type to expect trait objects, box the returned expressions
|
||||
|
||||
infer_sbfrit_change_return_type = you could change the return type to be a boxed trait object
|
||||
|
|
|
@ -1246,30 +1246,6 @@ pub struct FnConsiderCasting {
|
|||
pub casting: String,
|
||||
}
|
||||
|
||||
#[derive(Subdiagnostic)]
|
||||
pub enum SuggestAsRefWhereAppropriate<'a> {
|
||||
#[suggestion(
|
||||
infer_sarwa_option,
|
||||
code = "{snippet}.as_ref()",
|
||||
applicability = "machine-applicable"
|
||||
)]
|
||||
Option {
|
||||
#[primary_span]
|
||||
span: Span,
|
||||
snippet: &'a str,
|
||||
},
|
||||
#[suggestion(
|
||||
infer_sarwa_result,
|
||||
code = "{snippet}.as_ref()",
|
||||
applicability = "machine-applicable"
|
||||
)]
|
||||
Result {
|
||||
#[primary_span]
|
||||
span: Span,
|
||||
snippet: &'a str,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subdiagnostic)]
|
||||
pub enum SuggestAccessingField<'a> {
|
||||
#[suggestion(
|
||||
|
|
|
@ -1897,7 +1897,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
|
||||
if should_suggest_fixes {
|
||||
self.suggest_tuple_pattern(cause, &exp_found, diag);
|
||||
self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
|
||||
self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
|
||||
self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
|
||||
self.suggest_function_pointers(cause, span, &exp_found, diag);
|
||||
|
|
|
@ -13,9 +13,9 @@ use rustc_span::{sym, BytePos, Span};
|
|||
|
||||
use crate::errors::{
|
||||
ConsiderAddingAwait, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
|
||||
FunctionPointerSuggestion, SuggestAccessingField, SuggestAsRefWhereAppropriate,
|
||||
SuggestBoxingForReturnImplTrait, SuggestRemoveSemiOrReturnBinding, SuggestTuplePatternMany,
|
||||
SuggestTuplePatternOne, TypeErrorAdditionalDiags,
|
||||
FunctionPointerSuggestion, SuggestAccessingField, SuggestBoxingForReturnImplTrait,
|
||||
SuggestRemoveSemiOrReturnBinding, SuggestTuplePatternMany, SuggestTuplePatternOne,
|
||||
TypeErrorAdditionalDiags,
|
||||
};
|
||||
|
||||
use super::TypeErrCtxt;
|
||||
|
@ -289,27 +289,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
/// When encountering a case where `.as_ref()` on a `Result` or `Option` would be appropriate,
|
||||
/// suggests it.
|
||||
pub(super) fn suggest_as_ref_where_appropriate(
|
||||
&self,
|
||||
span: Span,
|
||||
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
|
||||
diag: &mut Diagnostic,
|
||||
) {
|
||||
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
|
||||
&& let Some(msg) = self.should_suggest_as_ref_kind(exp_found.expected, exp_found.found)
|
||||
{
|
||||
// HACK: fix issue# 100605, suggesting convert from &Option<T> to Option<&T>, remove the extra `&`
|
||||
let snippet = snippet.trim_start_matches('&');
|
||||
let subdiag = match msg {
|
||||
SuggestAsRefKind::Option => SuggestAsRefWhereAppropriate::Option { span, snippet },
|
||||
SuggestAsRefKind::Result => SuggestAsRefWhereAppropriate::Result { span, snippet },
|
||||
};
|
||||
diag.subdiagnostic(subdiag);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn suggest_function_pointers(
|
||||
&self,
|
||||
cause: &ObligationCause<'tcx>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue