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 ++++++++++++-------------- crates/ra_ide/src/display.rs | 13 +++++++------ crates/rust-analyzer/src/cli/analysis_stats.rs | 5 +++-- crates/rust-analyzer/src/main_loop/handlers.rs | 6 +++--- 5 files changed, 29 insertions(+), 29 deletions(-) (limited to 'crates') 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( diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index c395057a7..722092de9 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/src/display.rs @@ -6,12 +6,13 @@ mod navigation_target; mod structure; mod short_label; -use std::fmt::{Display, Write}; +use std::fmt::Display; use ra_syntax::{ ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}, }; +use stdx::format_to; pub use function_signature::FunctionSignature; pub use navigation_target::NavigationTarget; @@ -78,18 +79,18 @@ pub(crate) fn rust_code_markup_with_doc( doc: Option<&str>, mod_path: Option<&str>, ) -> String { - let mut markup = "```rust\n".to_owned(); + let mut buf = "```rust\n".to_owned(); if let Some(mod_path) = mod_path { if !mod_path.is_empty() { - write!(markup, "{}\n", mod_path).unwrap(); + format_to!(buf, "{}\n", mod_path); } } - write!(markup, "{}\n```", code).unwrap(); + format_to!(buf, "{}\n```", code); if let Some(doc) = doc { - write!(markup, "\n\n{}", doc).unwrap(); + format_to!(buf, "\n\n{}", doc); } - markup + buf } diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 27459be8c..75cf2dae5 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -1,7 +1,7 @@ //! Fully type-check project and print various stats, like the number of type //! errors. -use std::{collections::HashSet, fmt::Write, path::Path, time::Instant}; +use std::{collections::HashSet, path::Path, time::Instant}; use hir::{ db::{AstDatabase, DefDatabase, HirDatabase}, @@ -13,6 +13,7 @@ use itertools::Itertools; use ra_db::SourceDatabaseExt; use ra_syntax::AstNode; use rand::{seq::SliceRandom, thread_rng}; +use stdx::format_to; use crate::cli::{load_cargo::load_cargo, progress_report::ProgressReport, Result, Verbosity}; @@ -128,7 +129,7 @@ pub fn analysis_stats( let original_file = src.file_id.original_file(db); let path = db.file_relative_path(original_file); let syntax_range = src.value.syntax().text_range(); - write!(msg, " ({:?} {})", path, syntax_range).unwrap(); + format_to!(msg, " ({:?} {})", path, syntax_range); } if verbosity.is_spammy() { bar.println(msg.to_string()); diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 1033d6de9..12f8ca297 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -3,7 +3,6 @@ //! `ra_ide` crate. use std::{ - fmt::Write as _, io::Write as _, process::{self, Stdio}, }; @@ -28,6 +27,7 @@ use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit}; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; use serde_json::to_value; +use stdx::format_to; use crate::{ cargo_target_spec::CargoTargetSpec, @@ -46,11 +46,11 @@ use crate::{ pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result { let _p = profile("handle_analyzer_status"); let mut buf = world.status(); - writeln!(buf, "\n\nrequests:").unwrap(); + format_to!(buf, "\n\nrequests:"); let requests = world.latest_requests.read(); for (is_last, r) in requests.iter() { let mark = if is_last { "*" } else { " " }; - writeln!(buf, "{}{:4} {:<36}{}ms", mark, r.id, r.method, r.duration.as_millis()).unwrap(); + format_to!(buf, "{}{:4} {:<36}{}ms", mark, r.id, r.method, r.duration.as_millis()); } Ok(buf) } -- cgit v1.2.3