impl WithSearchPat for Ty

This commit is contained in:
Centri3 2023-06-06 22:16:02 -05:00
parent e97f190a9d
commit a434a7715d
2 changed files with 8 additions and 7 deletions

View file

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg}; use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
use clippy_utils::source::{snippet, snippet_opt, snippet_with_applicability}; use clippy_utils::source::{snippet, snippet_opt, snippet_with_applicability};
use clippy_utils::{SpanlessEq, SpanlessHash}; use clippy_utils::{is_from_proc_macro, SpanlessEq, SpanlessHash};
use core::hash::{Hash, Hasher}; use core::hash::{Hash, Hasher};
use if_chain::if_chain; use if_chain::if_chain;
use itertools::Itertools; use itertools::Itertools;
@ -260,10 +260,7 @@ impl TraitBounds {
SpanlessTy { ty: p.bounded_ty, cx }, SpanlessTy { ty: p.bounded_ty, cx },
p.bounds.iter().collect::<Vec<_>>() p.bounds.iter().collect::<Vec<_>>()
); );
let bounded_ty = snippet(cx, p.bounded_ty.span, "_"); if !is_from_proc_macro(cx, p.bounded_ty);
if let TyKind::Path(qpath) = p.bounded_ty.kind;
if format!("{}:", rustc_hir_pretty::qpath_to_string(&qpath)) == format!("{bounded_ty}:");
then { then {
let trait_bounds = v let trait_bounds = v
.iter() .iter()
@ -272,7 +269,10 @@ impl TraitBounds {
.filter_map(get_trait_info_from_bound) .filter_map(get_trait_info_from_bound)
.map(|(_, _, span)| snippet_with_applicability(cx, span, "..", &mut applicability)) .map(|(_, _, span)| snippet_with_applicability(cx, span, "..", &mut applicability))
.join(" + "); .join(" + ");
let hint_string = format!("consider combining the bounds: `{bounded_ty}: {trait_bounds}`"); let hint_string = format!(
"consider combining the bounds: `{}: {trait_bounds}`",
snippet(cx, p.bounded_ty.span, "_"),
);
span_lint_and_help( span_lint_and_help(
cx, cx,
TYPE_REPETITION_IN_BOUNDS, TYPE_REPETITION_IN_BOUNDS,

View file

@ -351,7 +351,8 @@ fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
TyKind::Never => (Pat::Str("!"), Pat::Str("")), TyKind::Never => (Pat::Str("!"), Pat::Str("")),
TyKind::Tup(..) => (Pat::Str("("), Pat::Str(")")), TyKind::Tup(..) => (Pat::Str("("), Pat::Str(")")),
TyKind::OpaqueDef(..) => (Pat::Str("impl"), Pat::Str("")), TyKind::OpaqueDef(..) => (Pat::Str("impl"), Pat::Str("")),
// NOTE: This is missing `TraitObject` and `Path` here. It always return true then. TyKind::Path(qpath) => qpath_search_pat(&qpath),
// NOTE: This is missing `TraitObject`. It always return true then.
_ => (Pat::Str(""), Pat::Str("")), _ => (Pat::Str(""), Pat::Str("")),
} }
} }