Skip to content

Commit 7f5d298

Browse files
committed
small refactor
1 parent b020ec7 commit 7f5d298

File tree

1 file changed

+36
-39
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+36
-39
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -913,45 +913,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
913913
.0
914914
}
915915
GenericArgs::Parenthesized(ref data) => {
916-
let mut err = self.sess.struct_span_err(
917-
gen_args.span(),
918-
"parenthesized generic arguments cannot be used in associated type constraints"
919-
);
920-
if let Ok(snippet) = self.sess.source_map().span_to_snippet(data.span) {
921-
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
922-
if !data.inputs.is_empty() {
923-
// Suggest replacing `(` and `)` with `<` and `>`
924-
// The snippet may be missing the closing `)`, skip that case
925-
if snippet.ends_with(')') {
926-
if let Some(split) = snippet.find('(') {
927-
let trait_name = &snippet[0..split];
928-
let args = &snippet[split + 1..snippet.len() - 1];
929-
err.span_suggestion(
930-
data.span,
931-
"use angle brackets instead",
932-
format!("{}<{}>", trait_name, args),
933-
Applicability::MaybeIncorrect,
934-
);
935-
}
936-
}
937-
}
938-
// Suggest removing empty parentheses: "Trait()" -> "Trait"
939-
else {
940-
// The snippet may be missing the closing `)`, skip that case
941-
if snippet.ends_with(')') {
942-
if let Some(split) = snippet.find('(') {
943-
let trait_name = &snippet[0..split];
944-
err.span_suggestion(
945-
data.span,
946-
"remove parentheses",
947-
format!("{}", trait_name),
948-
Applicability::MaybeIncorrect,
949-
);
950-
}
951-
}
952-
}
953-
};
954-
err.emit();
916+
self.assoc_ty_contraint_param_error_emit(gen_args, data);
955917
self.lower_angle_bracketed_parameter_data(
956918
&data.as_angle_bracketed_args(),
957919
ParamMode::Explicit,
@@ -1065,6 +1027,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10651027
}
10661028
}
10671029

1030+
fn assoc_ty_contraint_param_error_emit(&self, gen_args: &GenericArgs, data: &ParenthesizedArgs) -> () {
1031+
let mut err = self.sess.struct_span_err(
1032+
gen_args.span(),
1033+
"parenthesized generic arguments cannot be used in associated type constraints"
1034+
);
1035+
if let Ok(snippet) = self.sess.source_map().span_to_snippet(data.span)
1036+
// Skip snippet with missing `)`
1037+
&& snippet.ends_with(')')
1038+
&& let Some(split) = snippet.find("(") {
1039+
1040+
let trait_name = &snippet[0..split];
1041+
1042+
// Suggest removing empty parentheses: "Trait()" -> "Trait"
1043+
if data.inputs.is_empty() {
1044+
err.span_suggestion(
1045+
data.span,
1046+
"remove parentheses",
1047+
format!("{}", trait_name),
1048+
Applicability::MaybeIncorrect,
1049+
);
1050+
}
1051+
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
1052+
else {
1053+
let args = &snippet[split + 1..snippet.len() - 1];
1054+
err.span_suggestion(
1055+
data.span,
1056+
"use angle brackets instead",
1057+
format!("{}<{}>", trait_name, args),
1058+
Applicability::MaybeIncorrect,
1059+
);
1060+
}
1061+
};
1062+
err.emit();
1063+
}
1064+
10681065
fn lower_generic_arg(
10691066
&mut self,
10701067
arg: &ast::GenericArg,

0 commit comments

Comments
 (0)