Skip to content

Commit 7bf25cb

Browse files
committed
perf(language_server): transform MessageWithPosition to Diagnostic with less allocations (#11561)
1 parent 6d68568 commit 7bf25cb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

crates/oxc_language_server/src/linter/error_with_position.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ fn message_with_position_to_lsp_diagnostic(
6464
},
6565
},
6666
},
67-
message: span.message().unwrap_or(&Cow::Borrowed("")).to_string(),
67+
message: span
68+
.message()
69+
.map_or_else(String::new, |message| message.clone().into_owned()),
6870
})
6971
.collect()
7072
});
@@ -90,10 +92,19 @@ fn message_with_position_to_lsp_diagnostic(
9092
let code = message.code.to_string();
9193
let code_description =
9294
message.url.as_ref().map(|url| CodeDescription { href: Uri::from_str(url).ok().unwrap() });
93-
let message = message.help.as_ref().map_or_else(
94-
|| message.message.to_string(),
95-
|help| format!("{}\nhelp: {}", message.message, help),
96-
);
95+
let message = match &message.help {
96+
Some(help) => {
97+
let mut msg = String::with_capacity(message.message.len() + help.len() + 7);
98+
msg.push_str(message.message.as_ref());
99+
msg.push_str("\nhelp: ");
100+
msg.push_str(help.as_ref());
101+
msg
102+
}
103+
None => match message.message {
104+
Cow::Borrowed(s) => s.to_string(),
105+
Cow::Owned(ref s) => s.clone(),
106+
},
107+
};
97108

98109
lsp_types::Diagnostic {
99110
range,

0 commit comments

Comments
 (0)