From 1a37b17162ab04b468eb514788d6bea7912f7c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Mon, 21 Mar 2022 10:43:36 +0200 Subject: [PATCH] Replace write! with direct calls --- crates/base_db/src/input.rs | 4 +-- crates/cfg/src/cfg_expr.rs | 2 +- crates/cfg/src/dnf.rs | 18 +++++++------- crates/cfg/src/lib.rs | 8 +++--- crates/hir_def/src/type_ref.rs | 12 ++++----- crates/hir_expand/src/mod_path.rs | 2 +- crates/hir_ty/src/consteval.rs | 27 ++++++++++++--------- crates/hir_ty/src/diagnostics/decl_check.rs | 4 +-- crates/hir_ty/src/tls.rs | 8 +++--- crates/ide/src/runnables.rs | 4 +-- crates/ide/src/syntax_highlighting/tags.rs | 10 +++++--- crates/proc_macro_api/src/lib.rs | 5 ++-- crates/profile/src/memory_usage.rs | 4 +-- 13 files changed, 59 insertions(+), 49 deletions(-) diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs index 6a7f063586..db3f85604b 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs @@ -105,7 +105,7 @@ impl CrateName { impl fmt::Display for CrateName { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) + self.0.fmt(f) } } @@ -160,7 +160,7 @@ impl From for CrateDisplayName { impl fmt::Display for CrateDisplayName { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.crate_name) + self.crate_name.fmt(f) } } diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs index 2158fb9f2d..5db5e5d390 100644 --- a/crates/cfg/src/cfg_expr.rs +++ b/crates/cfg/src/cfg_expr.rs @@ -43,7 +43,7 @@ impl CfgAtom { impl fmt::Display for CfgAtom { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - CfgAtom::Flag(name) => write!(f, "{}", name), + CfgAtom::Flag(name) => name.fmt(f), CfgAtom::KeyValue { key, value } => write!(f, "{} = {:?}", key, value), } } diff --git a/crates/cfg/src/dnf.rs b/crates/cfg/src/dnf.rs index 0ae685a7d0..fd80e1ebe6 100644 --- a/crates/cfg/src/dnf.rs +++ b/crates/cfg/src/dnf.rs @@ -6,7 +6,7 @@ //! //! This is currently both messy and inefficient. Feel free to improve, there are unit tests. -use std::fmt; +use std::fmt::{self, Write}; use rustc_hash::FxHashSet; @@ -125,17 +125,17 @@ impl DnfExpr { impl fmt::Display for DnfExpr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if self.conjunctions.len() != 1 { - write!(f, "any(")?; + f.write_str("any(")?; } for (i, conj) in self.conjunctions.iter().enumerate() { if i != 0 { f.write_str(", ")?; } - write!(f, "{}", conj)?; + conj.fmt(f)?; } if self.conjunctions.len() != 1 { - write!(f, ")")?; + f.write_char(')')?; } Ok(()) @@ -165,17 +165,17 @@ impl Conjunction { impl fmt::Display for Conjunction { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if self.literals.len() != 1 { - write!(f, "all(")?; + f.write_str("all(")?; } for (i, lit) in self.literals.iter().enumerate() { if i != 0 { f.write_str(", ")?; } - write!(f, "{}", lit)?; + lit.fmt(f)?; } if self.literals.len() != 1 { - write!(f, ")")?; + f.write_str(")")?; } Ok(()) @@ -204,12 +204,12 @@ impl fmt::Display for Literal { } match &self.var { - Some(var) => write!(f, "{}", var)?, + Some(var) => var.fmt(f)?, None => f.write_str("")?, } if self.negate { - write!(f, ")")?; + f.write_char(')')?; } Ok(()) diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs index f867a903b0..837b8d4c92 100644 --- a/crates/cfg/src/lib.rs +++ b/crates/cfg/src/lib.rs @@ -128,7 +128,7 @@ impl fmt::Display for CfgDiff { }; f.write_str(sep)?; - write!(f, "{}", atom)?; + atom.fmt(f)?; } if !self.disable.is_empty() { @@ -146,7 +146,7 @@ impl fmt::Display for CfgDiff { }; f.write_str(sep)?; - write!(f, "{}", atom)?; + atom.fmt(f)?; } } @@ -170,7 +170,7 @@ impl fmt::Display for InactiveReason { }; f.write_str(sep)?; - write!(f, "{}", atom)?; + atom.fmt(f)?; } let is_are = if self.enabled.len() == 1 { "is" } else { "are" }; write!(f, " {} enabled", is_are)?; @@ -189,7 +189,7 @@ impl fmt::Display for InactiveReason { }; f.write_str(sep)?; - write!(f, "{}", atom)?; + atom.fmt(f)?; } let is_are = if self.disabled.len() == 1 { "is" } else { "are" }; write!(f, " {} disabled", is_are)?; diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index b9fadcfa8d..c6c521f733 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -5,7 +5,7 @@ use hir_expand::{ name::{AsName, Name}, AstId, InFile, }; -use std::convert::TryInto; +use std::{convert::TryInto, fmt::Write}; use syntax::ast::{self, HasName}; use crate::{body::LowerCtx, intern::Interned, path::Path}; @@ -364,8 +364,8 @@ pub enum ConstScalarOrPath { impl std::fmt::Display for ConstScalarOrPath { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - ConstScalarOrPath::Scalar(s) => write!(f, "{}", s), - ConstScalarOrPath::Path(n) => write!(f, "{}", n), + ConstScalarOrPath::Scalar(s) => s.fmt(f), + ConstScalarOrPath::Path(n) => n.fmt(f), } } } @@ -425,10 +425,10 @@ pub enum ConstScalar { } impl std::fmt::Display for ConstScalar { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { - ConstScalar::Usize(us) => write!(fmt, "{}", us), - ConstScalar::Unknown => write!(fmt, "_"), + ConstScalar::Usize(us) => us.fmt(f), + ConstScalar::Unknown => f.write_char('_'), } } } diff --git a/crates/hir_expand/src/mod_path.rs b/crates/hir_expand/src/mod_path.rs index 5e264d0398..bb2f6350c6 100644 --- a/crates/hir_expand/src/mod_path.rs +++ b/crates/hir_expand/src/mod_path.rs @@ -121,7 +121,7 @@ impl Display for ModPath { f.write_str("::")?; } first_segment = false; - write!(f, "{}", segment)?; + segment.fmt(f)?; } Ok(()) } diff --git a/crates/hir_ty/src/consteval.rs b/crates/hir_ty/src/consteval.rs index 24296c6b7b..1ba291528f 100644 --- a/crates/hir_ty/src/consteval.rs +++ b/crates/hir_ty/src/consteval.rs @@ -1,6 +1,10 @@ //! Constant evaluation details -use std::{collections::HashMap, convert::TryInto, fmt::Display}; +use std::{ + collections::HashMap, + convert::TryInto, + fmt::{Display, Write}, +}; use chalk_ir::{BoundVar, DebruijnIndex, GenericArgData, IntTy, Scalar}; use hir_def::{ @@ -79,28 +83,29 @@ impl Display for ComputedExpr { if *x >= 16 { write!(f, "{} ({:#X})", x, x) } else { - write!(f, "{}", x) + x.fmt(f) } } Literal::Uint(x, _) => { if *x >= 16 { write!(f, "{} ({:#X})", x, x) } else { - write!(f, "{}", x) + x.fmt(f) } } - Literal::Float(x, _) => write!(f, "{}", x), - Literal::Bool(x) => write!(f, "{}", x), - Literal::Char(x) => write!(f, "{:?}", x), - Literal::String(x) => write!(f, "{:?}", x), - Literal::ByteString(x) => write!(f, "{:?}", x), + Literal::Float(x, _) => x.fmt(f), + Literal::Bool(x) => x.fmt(f), + Literal::Char(x) => std::fmt::Debug::fmt(x, f), + Literal::String(x) => std::fmt::Debug::fmt(x, f), + Literal::ByteString(x) => std::fmt::Debug::fmt(x, f), }, ComputedExpr::Tuple(t) => { - write!(f, "(")?; + f.write_char('(')?; for x in &**t { - write!(f, "{}, ", x)?; + x.fmt(f)?; + f.write_str(", ")?; } - write!(f, ")") + f.write_char(')') } } } diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs index 3098198517..9e1f934034 100644 --- a/crates/hir_ty/src/diagnostics/decl_check.rs +++ b/crates/hir_ty/src/diagnostics/decl_check.rs @@ -72,7 +72,7 @@ impl fmt::Display for CaseType { CaseType::UpperCamelCase => "CamelCase", }; - write!(f, "{}", repr) + repr.fmt(f) } } @@ -103,7 +103,7 @@ impl fmt::Display for IdentType { IdentType::Variant => "Variant", }; - write!(f, "{}", repr) + repr.fmt(f) } } diff --git a/crates/hir_ty/src/tls.rs b/crates/hir_ty/src/tls.rs index ad6cd36ca6..0600fd7c6d 100644 --- a/crates/hir_ty/src/tls.rs +++ b/crates/hir_ty/src/tls.rs @@ -1,5 +1,5 @@ //! Implementation of Chalk debug helper functions using TLS. -use std::fmt; +use std::fmt::{self, Display}; use itertools::Itertools; @@ -24,17 +24,17 @@ impl DebugContext<'_> { AdtId::UnionId(it) => self.0.union_data(it).name.clone(), AdtId::EnumId(it) => self.0.enum_data(it).name.clone(), }; - write!(f, "{}", name) + name.fmt(f) } pub(crate) fn debug_trait_id( &self, id: chalk_db::TraitId, - fmt: &mut fmt::Formatter<'_>, + f: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { let trait_: hir_def::TraitId = from_chalk_trait_id(id); let trait_data = self.0.trait_data(trait_); - write!(fmt, "{}", trait_data.name) + trait_data.name.fmt(f) } pub(crate) fn debug_assoc_type_id( diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index 2c8e898d72..f4abee0931 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -38,8 +38,8 @@ pub enum TestId { impl fmt::Display for TestId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - TestId::Name(name) => write!(f, "{}", name), - TestId::Path(path) => write!(f, "{}", path), + TestId::Name(name) => name.fmt(f), + TestId::Path(path) => path.fmt(f), } } } diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index cb9d36f830..b900dadcfa 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -1,7 +1,10 @@ //! Defines token tags we use for syntax highlighting. //! A tag is not unlike a CSS class. -use std::{fmt, ops}; +use std::{ + fmt::{self, Write}, + ops, +}; use ide_db::SymbolKind; @@ -254,9 +257,10 @@ impl fmt::Display for HlMod { impl fmt::Display for Highlight { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.tag)?; + self.tag.fmt(f)?; for modifier in self.mods.iter() { - write!(f, ".{}", modifier)? + f.write_char('.')?; + modifier.fmt(f)?; } Ok(()) } diff --git a/crates/proc_macro_api/src/lib.rs b/crates/proc_macro_api/src/lib.rs index 0b8a2b6f85..eb7a28b972 100644 --- a/crates/proc_macro_api/src/lib.rs +++ b/crates/proc_macro_api/src/lib.rs @@ -95,9 +95,10 @@ pub struct ServerError { impl fmt::Display for ServerError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.message)?; + self.message.fmt(f)?; if let Some(io) = &self.io { - write!(f, ": {}", io)?; + f.write_str(": ")?; + io.fmt(f)?; } Ok(()) } diff --git a/crates/profile/src/memory_usage.rs b/crates/profile/src/memory_usage.rs index fbcb9e3c28..885fe1f1aa 100644 --- a/crates/profile/src/memory_usage.rs +++ b/crates/profile/src/memory_usage.rs @@ -11,8 +11,8 @@ pub struct MemoryUsage { } impl fmt::Display for MemoryUsage { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.allocated) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.allocated.fmt(f) } }