Skip to content

Commit 311cbbd

Browse files
committed
Remove some unwraps
1 parent 6596e7c commit 311cbbd

File tree

5 files changed

+29
-29
lines changed

5 files changed

+29
-29
lines changed

crates/ra_hir_ty/src/diagnostics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::any::Any;
44

55
use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile};
66
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
7+
use stdx::format_to;
78

89
pub use hir_def::diagnostics::UnresolvedModule;
910
pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
@@ -37,12 +38,11 @@ pub struct MissingFields {
3738

3839
impl Diagnostic for MissingFields {
3940
fn message(&self) -> String {
40-
use std::fmt::Write;
41-
let mut message = String::from("Missing structure fields:\n");
41+
let mut buf = String::from("Missing structure fields:\n");
4242
for field in &self.missed_fields {
43-
writeln!(message, "- {}", field).unwrap();
43+
format_to!(buf, "- {}", field);
4444
}
45-
message
45+
buf
4646
}
4747
fn source(&self) -> InFile<SyntaxNodePtr> {
4848
InFile { file_id: self.file, value: self.field_list.into() }

crates/ra_hir_ty/src/tests.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ mod traits;
77
mod method_resolution;
88
mod macros;
99

10-
use std::fmt::Write;
1110
use std::sync::Arc;
1211

1312
use hir_def::{
@@ -26,6 +25,7 @@ use ra_syntax::{
2625
algo,
2726
ast::{self, AstNode},
2827
};
28+
use stdx::format_to;
2929

3030
use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResult};
3131

@@ -63,7 +63,7 @@ fn infer(ra_fixture: &str) -> String {
6363
fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
6464
let (db, file_id) = TestDB::with_single_file(content);
6565

66-
let mut acc = String::new();
66+
let mut buf = String::new();
6767

6868
let mut infer_def = |inference_result: Arc<InferenceResult>,
6969
body_source_map: Arc<BodySourceMap>| {
@@ -106,15 +106,14 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
106106
(src_ptr.value.range(), node.text().to_string().replace("\n", " "))
107107
};
108108
let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
109-
writeln!(
110-
acc,
111-
"{}{} '{}': {}",
109+
format_to!(
110+
buf,
111+
"{}{} '{}': {}\n",
112112
macro_prefix,
113113
range,
114114
ellipsize(text, 15),
115115
ty.display(&db)
116-
)
117-
.unwrap();
116+
);
118117
}
119118
if include_mismatches {
120119
mismatches.sort_by_key(|(src_ptr, _)| {
@@ -123,15 +122,14 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
123122
for (src_ptr, mismatch) in &mismatches {
124123
let range = src_ptr.value.range();
125124
let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
126-
writeln!(
127-
acc,
128-
"{}{}: expected {}, got {}",
125+
format_to!(
126+
buf,
127+
"{}{}: expected {}, got {}\n",
129128
macro_prefix,
130129
range,
131130
mismatch.expected.display(&db),
132131
mismatch.actual.display(&db),
133-
)
134-
.unwrap();
132+
);
135133
}
136134
}
137135
};
@@ -158,8 +156,8 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
158156
infer_def(infer, source_map);
159157
}
160158

161-
acc.truncate(acc.trim_end().len());
162-
acc
159+
buf.truncate(buf.trim_end().len());
160+
buf
163161
}
164162

165163
fn visit_module(

crates/ra_ide/src/display.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ mod navigation_target;
66
mod structure;
77
mod short_label;
88

9-
use std::fmt::{Display, Write};
9+
use std::fmt::Display;
1010

1111
use ra_syntax::{
1212
ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
1313
SyntaxKind::{ATTR, COMMENT},
1414
};
15+
use stdx::format_to;
1516

1617
pub use function_signature::FunctionSignature;
1718
pub use navigation_target::NavigationTarget;
@@ -78,18 +79,18 @@ pub(crate) fn rust_code_markup_with_doc(
7879
doc: Option<&str>,
7980
mod_path: Option<&str>,
8081
) -> String {
81-
let mut markup = "```rust\n".to_owned();
82+
let mut buf = "```rust\n".to_owned();
8283

8384
if let Some(mod_path) = mod_path {
8485
if !mod_path.is_empty() {
85-
write!(markup, "{}\n", mod_path).unwrap();
86+
format_to!(buf, "{}\n", mod_path);
8687
}
8788
}
88-
write!(markup, "{}\n```", code).unwrap();
89+
format_to!(buf, "{}\n```", code);
8990

9091
if let Some(doc) = doc {
91-
write!(markup, "\n\n{}", doc).unwrap();
92+
format_to!(buf, "\n\n{}", doc);
9293
}
9394

94-
markup
95+
buf
9596
}

crates/rust-analyzer/src/cli/analysis_stats.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Fully type-check project and print various stats, like the number of type
22
//! errors.
33
4-
use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
4+
use std::{collections::HashSet, path::Path, time::Instant};
55

66
use hir::{
77
db::{AstDatabase, DefDatabase, HirDatabase},
@@ -13,6 +13,7 @@ use itertools::Itertools;
1313
use ra_db::SourceDatabaseExt;
1414
use ra_syntax::AstNode;
1515
use rand::{seq::SliceRandom, thread_rng};
16+
use stdx::format_to;
1617

1718
use crate::cli::{load_cargo::load_cargo, progress_report::ProgressReport, Result, Verbosity};
1819

@@ -128,7 +129,7 @@ pub fn analysis_stats(
128129
let original_file = src.file_id.original_file(db);
129130
let path = db.file_relative_path(original_file);
130131
let syntax_range = src.value.syntax().text_range();
131-
write!(msg, " ({:?} {})", path, syntax_range).unwrap();
132+
format_to!(msg, " ({:?} {})", path, syntax_range);
132133
}
133134
if verbosity.is_spammy() {
134135
bar.println(msg.to_string());

crates/rust-analyzer/src/main_loop/handlers.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! `ra_ide` crate.
44
55
use std::{
6-
fmt::Write as _,
76
io::Write as _,
87
process::{self, Stdio},
98
};
@@ -28,6 +27,7 @@ use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit};
2827
use rustc_hash::FxHashMap;
2928
use serde::{Deserialize, Serialize};
3029
use serde_json::to_value;
30+
use stdx::format_to;
3131

3232
use crate::{
3333
cargo_target_spec::CargoTargetSpec,
@@ -46,11 +46,11 @@ use crate::{
4646
pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result<String> {
4747
let _p = profile("handle_analyzer_status");
4848
let mut buf = world.status();
49-
writeln!(buf, "\n\nrequests:").unwrap();
49+
format_to!(buf, "\n\nrequests:");
5050
let requests = world.latest_requests.read();
5151
for (is_last, r) in requests.iter() {
5252
let mark = if is_last { "*" } else { " " };
53-
writeln!(buf, "{}{:4} {:<36}{}ms", mark, r.id, r.method, r.duration.as_millis()).unwrap();
53+
format_to!(buf, "{}{:4} {:<36}{}ms", mark, r.id, r.method, r.duration.as_millis());
5454
}
5555
Ok(buf)
5656
}

0 commit comments

Comments
 (0)