From 6596e7cddfc00281362c3640781f6cd6bc0b5614 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 28 Mar 2020 11:08:19 +0100 Subject: Nice string formatting --- crates/ra_hir_ty/src/test_db.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/test_db.rs b/crates/ra_hir_ty/src/test_db.rs index 5bbeabf51..208096aab 100644 --- a/crates/ra_hir_ty/src/test_db.rs +++ b/crates/ra_hir_ty/src/test_db.rs @@ -10,6 +10,7 @@ use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink}; use ra_db::{ salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, Upcast, }; +use stdx::format_to; use crate::{db::HirDatabase, expr::ExprValidator}; @@ -131,7 +132,7 @@ impl TestDB { for f in fns { let infer = self.infer(f.into()); let mut sink = DiagnosticSink::new(|d| { - buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); + format_to!(buf, "{:?}: {}\n", d.syntax_node(self).text(), d.message()); }); infer.add_diagnostics(self, f, &mut sink); let mut validator = ExprValidator::new(f, infer, &mut sink); -- cgit v1.2.3 From 311cbbdad599d51c6f08f7dd72c299f7c0128bb2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 28 Mar 2020 11:20:34 +0100 Subject: Remove some unwraps --- crates/ra_hir_ty/src/diagnostics.rs | 8 ++++---- crates/ra_hir_ty/src/tests.rs | 26 ++++++++++++-------------- 2 files changed, 16 insertions(+), 18 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index 6eafdc8f6..0f8522021 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs @@ -4,6 +4,7 @@ use std::any::Any; use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr}; +use stdx::format_to; pub use hir_def::diagnostics::UnresolvedModule; pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; @@ -37,12 +38,11 @@ pub struct MissingFields { impl Diagnostic for MissingFields { fn message(&self) -> String { - use std::fmt::Write; - let mut message = String::from("Missing structure fields:\n"); + let mut buf = String::from("Missing structure fields:\n"); for field in &self.missed_fields { - writeln!(message, "- {}", field).unwrap(); + format_to!(buf, "- {}", field); } - message + buf } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.field_list.into() } diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 7e9547340..027e5a8f8 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -7,7 +7,6 @@ mod traits; mod method_resolution; mod macros; -use std::fmt::Write; use std::sync::Arc; use hir_def::{ @@ -26,6 +25,7 @@ use ra_syntax::{ algo, ast::{self, AstNode}, }; +use stdx::format_to; use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResult}; @@ -63,7 +63,7 @@ fn infer(ra_fixture: &str) -> String { fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { let (db, file_id) = TestDB::with_single_file(content); - let mut acc = String::new(); + let mut buf = String::new(); let mut infer_def = |inference_result: Arc, body_source_map: Arc| { @@ -106,15 +106,14 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { (src_ptr.value.range(), node.text().to_string().replace("\n", " ")) }; let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" }; - writeln!( - acc, - "{}{} '{}': {}", + format_to!( + buf, + "{}{} '{}': {}\n", macro_prefix, range, ellipsize(text, 15), ty.display(&db) - ) - .unwrap(); + ); } if include_mismatches { mismatches.sort_by_key(|(src_ptr, _)| { @@ -123,15 +122,14 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { for (src_ptr, mismatch) in &mismatches { let range = src_ptr.value.range(); let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" }; - writeln!( - acc, - "{}{}: expected {}, got {}", + format_to!( + buf, + "{}{}: expected {}, got {}\n", macro_prefix, range, mismatch.expected.display(&db), mismatch.actual.display(&db), - ) - .unwrap(); + ); } } }; @@ -158,8 +156,8 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { infer_def(infer, source_map); } - acc.truncate(acc.trim_end().len()); - acc + buf.truncate(buf.trim_end().len()); + buf } fn visit_module( -- cgit v1.2.3