1
Fork 0

Rejig some top-level rustc_hir_pretty functions.

There are several that are unused and can be removed.

And there are some calls to `to_string`, which can be expressed more
nicely as a `foo_to_string` call, and then `to_string` need not be
`pub`. (This requires adding `pat_to_string`).
This commit is contained in:
Nicholas Nethercote 2023-10-10 14:02:42 +11:00
parent bf9a1c8a19
commit b6b11c72c9
4 changed files with 8 additions and 46 deletions

View file

@ -187,7 +187,7 @@ impl<'a> State<'a> {
} }
} }
pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
where where
F: FnOnce(&mut State<'_>), F: FnOnce(&mut State<'_>),
{ {
@ -196,48 +196,16 @@ where
printer.s.eof() printer.s.eof()
} }
pub fn generic_params_to_string(generic_params: &[GenericParam<'_>]) -> String {
to_string(NO_ANN, |s| s.print_generic_params(generic_params))
}
pub fn bounds_to_string<'b>(bounds: impl IntoIterator<Item = &'b hir::GenericBound<'b>>) -> String {
to_string(NO_ANN, |s| s.print_bounds("", bounds))
}
pub fn ty_to_string(ty: &hir::Ty<'_>) -> String { pub fn ty_to_string(ty: &hir::Ty<'_>) -> String {
to_string(NO_ANN, |s| s.print_type(ty)) to_string(NO_ANN, |s| s.print_type(ty))
} }
pub fn path_segment_to_string(segment: &hir::PathSegment<'_>) -> String {
to_string(NO_ANN, |s| s.print_path_segment(segment))
}
pub fn path_to_string(segment: &hir::Path<'_>) -> String {
to_string(NO_ANN, |s| s.print_path(segment, false))
}
pub fn qpath_to_string(segment: &hir::QPath<'_>) -> String { pub fn qpath_to_string(segment: &hir::QPath<'_>) -> String {
to_string(NO_ANN, |s| s.print_qpath(segment, false)) to_string(NO_ANN, |s| s.print_qpath(segment, false))
} }
pub fn fn_to_string( pub fn pat_to_string(pat: &hir::Pat<'_>) -> String {
decl: &hir::FnDecl<'_>, to_string(NO_ANN, |s| s.print_pat(pat))
header: hir::FnHeader,
name: Option<Symbol>,
generics: &hir::Generics<'_>,
arg_names: &[Ident],
body_id: Option<hir::BodyId>,
) -> String {
to_string(NO_ANN, |s| s.print_fn(decl, header, name, generics, arg_names, body_id))
}
pub fn enum_def_to_string(
enum_definition: &hir::EnumDef<'_>,
generics: &hir::Generics<'_>,
name: Symbol,
span: rustc_span::Span,
) -> String {
to_string(NO_ANN, |s| s.print_enum_def(enum_definition, generics, name, span))
} }
impl<'a> State<'a> { impl<'a> State<'a> {

View file

@ -1504,9 +1504,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{ {
let has_shorthand_field_name = field_patterns.iter().any(|field| field.is_shorthand); let has_shorthand_field_name = field_patterns.iter().any(|field| field.is_shorthand);
if has_shorthand_field_name { if has_shorthand_field_name {
let path = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| { let path = rustc_hir_pretty::qpath_to_string(qpath);
s.print_qpath(qpath, false)
});
let mut err = struct_span_err!( let mut err = struct_span_err!(
self.tcx.sess, self.tcx.sess,
pat.span, pat.span,
@ -1688,9 +1686,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return None; return None;
} }
let path = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| { let path = rustc_hir_pretty::qpath_to_string(qpath);
s.print_qpath(qpath, false)
});
let mut err = struct_span_err!( let mut err = struct_span_err!(
self.tcx.sess, self.tcx.sess,
pat.span, pat.span,
@ -1740,9 +1736,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
f f
} }
} }
Err(_) => rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| { Err(_) => rustc_hir_pretty::pat_to_string(field.pat),
s.print_pat(field.pat)
}),
} }
}) })
.collect::<Vec<String>>() .collect::<Vec<String>>()

View file

@ -19,7 +19,7 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'
if is_type_diagnostic_item(cx, ex_ty, sym::Result) { if is_type_diagnostic_item(cx, ex_ty, sym::Result) {
for arm in arms { for arm in arms {
if let PatKind::TupleStruct(ref path, inner, _) = arm.pat.kind { if let PatKind::TupleStruct(ref path, inner, _) = arm.pat.kind {
let path_str = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)); let path_str = rustc_hir_pretty::qpath_to_string(path);
if path_str == "Err" { if path_str == "Err" {
let mut matching_wild = inner.iter().any(is_wild); let mut matching_wild = inner.iter().any(is_wild);
let mut ident_bind_name = kw::Underscore; let mut ident_bind_name = kw::Underscore;

View file

@ -49,7 +49,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed {
cx, cx,
arguments.iter().collect(), arguments.iter().collect(),
cx.typeck_results().expr_ty(fn_expr), cx.typeck_results().expr_ty(fn_expr),
&rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)), &rustc_hir_pretty::qpath_to_string(path),
"function", "function",
); );
} }