Skip to content

Commit 1660820

Browse files
committed
internal: use idiomatic form of assertions
Ideally, we should just return an InvalidParameter dialog here, but that shows error message to the end user, and we generally avoid that
1 parent fa6a4a0 commit 1660820

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

crates/rust-analyzer/src/handlers.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77
process::{self, Stdio},
88
};
99

10+
use always_assert::always;
1011
use ide::{
1112
AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange,
1213
HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SingleResolve,
@@ -265,10 +266,11 @@ pub(crate) fn handle_on_type_formatting(
265266
// `text.char_at(position) == typed_char`.
266267
position.offset -= TextSize::of('.');
267268
let char_typed = params.ch.chars().next().unwrap_or('\0');
268-
assert!({
269-
let text = snap.analysis.file_text(position.file_id)?;
270-
text[usize::from(position.offset)..].starts_with(char_typed)
271-
});
269+
270+
let text = snap.analysis.file_text(position.file_id)?;
271+
if !always!(text[usize::from(position.offset)..].starts_with(char_typed)) {
272+
return Ok(None);
273+
}
272274

273275
// We have an assist that inserts ` ` after typing `->` in `fn foo() ->{`,
274276
// but it requires precise cursor positioning to work, and one can't

docs/dev/style.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ if idx >= len {
260260
## Assertions
261261

262262
Assert liberally.
263-
Prefer `stdx::never!` to standard `assert!`.
263+
Prefer [`stdx::never!`](https://docs.rs/always-assert/0.1.2/always_assert/macro.never.html) to standard `assert!`.
264+
265+
**Rationale:** See [cross cutting concern: error handling](https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/architecture.md#error-handling).
264266

265267
## Getters & Setters
266268

0 commit comments

Comments
 (0)