simplify suggest_deref_ref_or_into
This commit is contained in:
parent
1599c5a821
commit
45b88aff10
3 changed files with 49 additions and 47 deletions
|
@ -1811,6 +1811,14 @@ impl Expr<'_> {
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn method_ident(&self) -> Option<Ident> {
|
||||||
|
match self.kind {
|
||||||
|
ExprKind::MethodCall(receiver_method, ..) => Some(receiver_method.ident),
|
||||||
|
ExprKind::Unary(_, expr) | ExprKind::AddrOf(.., expr) => expr.method_ident(),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the specified expression is a built-in range literal.
|
/// Checks if the specified expression is a built-in range literal.
|
||||||
|
|
|
@ -1434,6 +1434,8 @@ symbols! {
|
||||||
thumb2,
|
thumb2,
|
||||||
thumb_mode: "thumb-mode",
|
thumb_mode: "thumb-mode",
|
||||||
tmm_reg,
|
tmm_reg,
|
||||||
|
to_string,
|
||||||
|
to_vec,
|
||||||
todo_macro,
|
todo_macro,
|
||||||
tool_attributes,
|
tool_attributes,
|
||||||
tool_lints,
|
tool_lints,
|
||||||
|
|
|
@ -14,12 +14,10 @@ use rustc_infer::infer::{self, TyCtxtInferExt};
|
||||||
use rustc_infer::traits;
|
use rustc_infer::traits;
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_middle::ty::{self, Binder, IsSuggestable, Subst, ToPredicate, Ty};
|
use rustc_middle::ty::{self, Binder, IsSuggestable, Subst, ToPredicate, Ty};
|
||||||
use rustc_span::symbol::{sym, Ident};
|
use rustc_span::symbol::sym;
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
|
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
|
||||||
|
|
||||||
use std::iter;
|
|
||||||
|
|
||||||
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
pub(in super::super) fn suggest_semicolon_at_end(&self, span: Span, err: &mut Diagnostic) {
|
pub(in super::super) fn suggest_semicolon_at_end(&self, span: Span, err: &mut Diagnostic) {
|
||||||
err.span_suggestion_short(
|
err.span_suggestion_short(
|
||||||
|
@ -187,54 +185,48 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
err.span_label(self.tcx.def_span(def_id), &format!("{} defined here", found));
|
err.span_label(self.tcx.def_span(def_id), &format!("{} defined here", found));
|
||||||
}
|
}
|
||||||
} else if !self.check_for_cast(err, expr, found, expected, expected_ty_expr) {
|
} else if !self.check_for_cast(err, expr, found, expected, expected_ty_expr) {
|
||||||
let struct_pat_shorthand_field = self.maybe_get_struct_pattern_shorthand_field(expr);
|
|
||||||
let methods = self.get_conversion_methods(expr.span, expected, found, expr.hir_id);
|
let methods = self.get_conversion_methods(expr.span, expected, found, expr.hir_id);
|
||||||
if !methods.is_empty() {
|
if !methods.is_empty() {
|
||||||
let mut suggestions = iter::zip(iter::repeat(&expr), &methods)
|
let mut suggestions = methods.iter()
|
||||||
.filter_map(|(receiver_expr, method)| {
|
.filter_map(|conversion_method| {
|
||||||
let method_call = format!(".{}()", method.name);
|
let receiver_method_ident = expr.method_ident();
|
||||||
fn method_ident(expr: &hir::Expr<'_>) -> Option<Ident> {
|
if let Some(method_ident) = receiver_method_ident
|
||||||
match expr.kind {
|
&& method_ident.name == conversion_method.name
|
||||||
ExprKind::MethodCall(receiver_method, ..) => Some(receiver_method.ident),
|
|
||||||
ExprKind::Unary(_, expr) | ExprKind::AddrOf(.., expr) => method_ident(expr),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let method_ident = method_ident(&receiver_expr);
|
|
||||||
if let Some(method_ident) = method_ident
|
|
||||||
&& method_ident.name == method.name
|
|
||||||
{
|
{
|
||||||
None // do not suggest code that is already there (#53348)
|
return None // do not suggest code that is already there (#53348)
|
||||||
} else {
|
|
||||||
let method_call_list = [".to_vec()", ".to_string()"];
|
|
||||||
let mut sugg = if let ExprKind::MethodCall(receiver_method, ..) = receiver_expr.kind
|
|
||||||
&& receiver_method.ident.name == sym::clone
|
|
||||||
&& method_call_list.contains(&method_call.as_str())
|
|
||||||
{
|
|
||||||
vec![(
|
|
||||||
receiver_method.ident.span,
|
|
||||||
method.name.to_string()
|
|
||||||
)]
|
|
||||||
} else {
|
|
||||||
if expr.precedence().order()
|
|
||||||
< ExprPrecedence::MethodCall.order()
|
|
||||||
{
|
|
||||||
vec![
|
|
||||||
(expr.span.shrink_to_lo(), "(".to_string()),
|
|
||||||
(expr.span.shrink_to_hi(), format!("){}", method_call)),
|
|
||||||
]
|
|
||||||
} else {
|
|
||||||
vec![(expr.span.shrink_to_hi(), method_call)]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if let Some(name) = struct_pat_shorthand_field {
|
|
||||||
sugg.insert(
|
|
||||||
0,
|
|
||||||
(expr.span.shrink_to_lo(), format!("{}: ", name)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Some(sugg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let method_call_list = [sym::to_vec, sym::to_string];
|
||||||
|
let mut sugg = if let ExprKind::MethodCall(receiver_method, ..) = expr.kind
|
||||||
|
&& receiver_method.ident.name == sym::clone
|
||||||
|
&& method_call_list.contains(&conversion_method.name)
|
||||||
|
// If receiver is `.clone()` and found type has one of those methods,
|
||||||
|
// we guess that the user wants to convert from a slice type (`&[]` or `&str`)
|
||||||
|
// to an owned type (`Vec` or `String`). These conversions clone internally,
|
||||||
|
// so we remove the user's `clone` call.
|
||||||
|
{
|
||||||
|
vec![(
|
||||||
|
receiver_method.ident.span,
|
||||||
|
conversion_method.name.to_string()
|
||||||
|
)]
|
||||||
|
} else if expr.precedence().order()
|
||||||
|
< ExprPrecedence::MethodCall.order()
|
||||||
|
{
|
||||||
|
vec![
|
||||||
|
(expr.span.shrink_to_lo(), "(".to_string()),
|
||||||
|
(expr.span.shrink_to_hi(), format!(").{}()", conversion_method.name)),
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
vec![(expr.span.shrink_to_hi(), format!(".{}()", conversion_method.name))]
|
||||||
|
};
|
||||||
|
let struct_pat_shorthand_field = self.maybe_get_struct_pattern_shorthand_field(expr);
|
||||||
|
if let Some(name) = struct_pat_shorthand_field {
|
||||||
|
sugg.insert(
|
||||||
|
0,
|
||||||
|
(expr.span.shrink_to_lo(), format!("{}: ", name)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Some(sugg)
|
||||||
})
|
})
|
||||||
.peekable();
|
.peekable();
|
||||||
if suggestions.peek().is_some() {
|
if suggestions.peek().is_some() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue