Skip to content

Commit 4578154

Browse files
Merge #4409
4409: Hot fix panic for function_signature r=edwin0cheng a=edwin0cheng I am totally agree this comment: https://github.com/rust-analyzer/rust-analyzer/blob/f1cb5b8a29ce509bf1f8d6df97d4b6586b9a2dac/crates/ra_ide/src/display/function_signature.rs#L3-L4 But let hot fix all panic for right now, it is so disturbing when browsing code... Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2 parents f1cb5b8 + a3375c1 commit 4578154

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

crates/ra_ide/src/display/function_signature.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ impl FunctionSignature {
8484
let ty = field.signature_ty(db);
8585
let raw_param = format!("{}", ty.display(db));
8686

87-
if let Some(param_type) = raw_param.split(':').nth(1) {
88-
parameter_types.push(param_type[1..].to_string());
87+
if let Some(param_type) = raw_param.split(':').nth(1).and_then(|it| it.get(1..)) {
88+
parameter_types.push(param_type.to_string());
8989
} else {
9090
// useful when you have tuple struct
9191
parameter_types.push(raw_param.clone());
@@ -129,8 +129,8 @@ impl FunctionSignature {
129129
for field in variant.fields(db).into_iter() {
130130
let ty = field.signature_ty(db);
131131
let raw_param = format!("{}", ty.display(db));
132-
if let Some(param_type) = raw_param.split(':').nth(1) {
133-
parameter_types.push(param_type[1..].to_string());
132+
if let Some(param_type) = raw_param.split(':').nth(1).and_then(|it| it.get(1..)) {
133+
parameter_types.push(param_type.to_string());
134134
} else {
135135
// The unwrap_or_else is useful when you have tuple
136136
parameter_types.push(raw_param);
@@ -197,16 +197,21 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
197197
let raw_param = self_param.syntax().text().to_string();
198198

199199
res_types.push(
200-
raw_param.split(':').nth(1).unwrap_or_else(|| " Self")[1..].to_string(),
200+
raw_param
201+
.split(':')
202+
.nth(1)
203+
.and_then(|it| it.get(1..))
204+
.unwrap_or_else(|| "Self")
205+
.to_string(),
201206
);
202207
res.push(raw_param);
203208
}
204209

205210
res.extend(param_list.params().map(|param| param.syntax().text().to_string()));
206211
res_types.extend(param_list.params().map(|param| {
207212
let param_text = param.syntax().text().to_string();
208-
match param_text.split(':').nth(1) {
209-
Some(it) => it[1..].to_string(),
213+
match param_text.split(':').nth(1).and_then(|it| it.get(1..)) {
214+
Some(it) => it.to_string(),
210215
None => param_text,
211216
}
212217
}));

0 commit comments

Comments
 (0)