Skip to content

Commit a5d9310

Browse files
author
Guanqun Lu
committed
remove 'as_str' when it's already a str type.
fix #62642
1 parent 42ec683 commit a5d9310

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/librustc_typeck/check/method/suggest.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
518518
}
519519
}
520520

521-
if let Some(lev_candidate) = lev_candidate {
521+
fn is_str_ref<'tcx>(ty: Ty<'tcx>) -> bool {
522+
match ty.sty {
523+
ty::Str => true,
524+
ty::Ref(_, ty, _) => is_str_ref(&ty),
525+
_ => false,
526+
}
527+
}
528+
if item_name.as_str() == "as_str" && is_str_ref(&actual) {
529+
err.span_suggestion(
530+
span,
531+
"try to remove `as_str`",
532+
String::new(),
533+
Applicability::MaybeIncorrect,
534+
);
535+
} else if let Some(lev_candidate) = lev_candidate {
522536
let def_kind = lev_candidate.def_kind();
523537
err.span_suggestion(
524538
span,

0 commit comments

Comments
 (0)