From 1660820e516fef175c98ae7e7e8c30b866d6e67c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 24 Aug 2021 13:13:38 +0300 Subject: [PATCH] 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 --- crates/rust-analyzer/src/handlers.rs | 10 ++++++---- docs/dev/style.md | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 6425510acf..d196db6f59 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -7,6 +7,7 @@ use std::{ process::{self, Stdio}, }; +use always_assert::always; use ide::{ AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SingleResolve, @@ -265,10 +266,11 @@ pub(crate) fn handle_on_type_formatting( // `text.char_at(position) == typed_char`. position.offset -= TextSize::of('.'); let char_typed = params.ch.chars().next().unwrap_or('\0'); - assert!({ - let text = snap.analysis.file_text(position.file_id)?; - text[usize::from(position.offset)..].starts_with(char_typed) - }); + + let text = snap.analysis.file_text(position.file_id)?; + if !always!(text[usize::from(position.offset)..].starts_with(char_typed)) { + return Ok(None); + } // We have an assist that inserts ` ` after typing `->` in `fn foo() ->{`, // but it requires precise cursor positioning to work, and one can't diff --git a/docs/dev/style.md b/docs/dev/style.md index ac9ad8fc28..7954ae8ec6 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -260,7 +260,9 @@ if idx >= len { ## Assertions Assert liberally. -Prefer `stdx::never!` to standard `assert!`. +Prefer [`stdx::never!`](https://docs.rs/always-assert/0.1.2/always_assert/macro.never.html) to standard `assert!`. + +**Rationale:** See [cross cutting concern: error handling](https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/architecture.md#error-handling). ## Getters & Setters