Skip to content

Commit 9bf7ca5

Browse files
bors[bot]matklad
andauthored
Merge #8337
8337: internal: explain "extract if condition" refactoring r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 58924cf + a01fd1a commit 9bf7ca5

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

crates/rust-analyzer/src/handlers.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -927,22 +927,22 @@ pub(crate) fn handle_formatting(
927927
let captured_stderr = String::from_utf8(output.stderr).unwrap_or_default();
928928

929929
if !output.status.success() {
930-
match output.status.code() {
931-
Some(1)
932-
if !captured_stderr.contains("not installed")
933-
&& !captured_stderr.contains("not available") =>
934-
{
930+
let rustfmt_not_installed =
931+
captured_stderr.contains("not installed") || captured_stderr.contains("not available");
932+
933+
return match output.status.code() {
934+
Some(1) if !rustfmt_not_installed => {
935935
// While `rustfmt` doesn't have a specific exit code for parse errors this is the
936936
// likely cause exiting with 1. Most Language Servers swallow parse errors on
937937
// formatting because otherwise an error is surfaced to the user on top of the
938938
// syntax error diagnostics they're already receiving. This is especially jarring
939939
// if they have format on save enabled.
940940
log::info!("rustfmt exited with status 1, assuming parse error and ignoring");
941-
return Ok(None);
941+
Ok(None)
942942
}
943943
_ => {
944944
// Something else happened - e.g. `rustfmt` is missing or caught a signal
945-
return Err(LspError::new(
945+
Err(LspError::new(
946946
-32900,
947947
format!(
948948
r#"rustfmt exited with:
@@ -952,9 +952,9 @@ pub(crate) fn handle_formatting(
952952
output.status, captured_stdout, captured_stderr,
953953
),
954954
)
955-
.into());
955+
.into())
956956
}
957-
}
957+
};
958958
}
959959

960960
let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);

docs/dev/style.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,26 @@ Re-using originally single-purpose function often leads to bad coupling.
842842

843843
## Helper Variables
844844

845-
Introduce helper variables freely, especially for multiline conditions.
845+
Introduce helper variables freely, especially for multiline conditions:
846+
847+
```rust
848+
// GOOD
849+
let rustfmt_not_installed =
850+
captured_stderr.contains("not installed") || captured_stderr.contains("not available");
851+
852+
match output.status.code() {
853+
Some(1) if !rustfmt_not_installed => Ok(None),
854+
_ => Err(format_err!("rustfmt failed:\n{}", captured_stderr)),
855+
};
856+
857+
// BAD
858+
match output.status.code() {
859+
Some(1)
860+
if !captured_stderr.contains("not installed")
861+
&& !captured_stderr.contains("not available") => Ok(None),
862+
_ => Err(format_err!("rustfmt failed:\n{}", captured_stderr)),
863+
};
864+
```
846865

847866
**Rationale:** like blocks, single-use variables are a cognitively cheap abstraction, as they have access to all the context.
848867
Extra variables help during debugging, they make it easy to print/view important intermediate results.

0 commit comments

Comments
 (0)