Skip to content

Commit 6a0b199

Browse files
bors[bot]lnicola
andauthored
Merge #11776
11776: Replace write! with direct `Formatter` calls r=Veykril a=lnicola The final executable is somehow larger (36 239 296 vs 36 238 336 bytes), but this saves us a bit of `text` and `data`: ``` text data bss dec hex filename 23719199 1126625 4377 24850201 17b2f19 rust-analyzer-baseline 23716027 1126377 4377 24846781 17b21bd rust-analyzer-pr ``` Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
2 parents b594f9c + 1a37b17 commit 6a0b199

File tree

13 files changed

+59
-49
lines changed

13 files changed

+59
-49
lines changed

crates/base_db/src/input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl CrateName {
105105

106106
impl fmt::Display for CrateName {
107107
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108-
write!(f, "{}", self.0)
108+
self.0.fmt(f)
109109
}
110110
}
111111

@@ -160,7 +160,7 @@ impl From<CrateName> for CrateDisplayName {
160160

161161
impl fmt::Display for CrateDisplayName {
162162
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
163-
write!(f, "{}", self.crate_name)
163+
self.crate_name.fmt(f)
164164
}
165165
}
166166

crates/cfg/src/cfg_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl CfgAtom {
4343
impl fmt::Display for CfgAtom {
4444
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4545
match self {
46-
CfgAtom::Flag(name) => write!(f, "{}", name),
46+
CfgAtom::Flag(name) => name.fmt(f),
4747
CfgAtom::KeyValue { key, value } => write!(f, "{} = {:?}", key, value),
4848
}
4949
}

crates/cfg/src/dnf.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! This is currently both messy and inefficient. Feel free to improve, there are unit tests.
88
9-
use std::fmt;
9+
use std::fmt::{self, Write};
1010

1111
use rustc_hash::FxHashSet;
1212

@@ -125,17 +125,17 @@ impl DnfExpr {
125125
impl fmt::Display for DnfExpr {
126126
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127127
if self.conjunctions.len() != 1 {
128-
write!(f, "any(")?;
128+
f.write_str("any(")?;
129129
}
130130
for (i, conj) in self.conjunctions.iter().enumerate() {
131131
if i != 0 {
132132
f.write_str(", ")?;
133133
}
134134

135-
write!(f, "{}", conj)?;
135+
conj.fmt(f)?;
136136
}
137137
if self.conjunctions.len() != 1 {
138-
write!(f, ")")?;
138+
f.write_char(')')?;
139139
}
140140

141141
Ok(())
@@ -165,17 +165,17 @@ impl Conjunction {
165165
impl fmt::Display for Conjunction {
166166
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167167
if self.literals.len() != 1 {
168-
write!(f, "all(")?;
168+
f.write_str("all(")?;
169169
}
170170
for (i, lit) in self.literals.iter().enumerate() {
171171
if i != 0 {
172172
f.write_str(", ")?;
173173
}
174174

175-
write!(f, "{}", lit)?;
175+
lit.fmt(f)?;
176176
}
177177
if self.literals.len() != 1 {
178-
write!(f, ")")?;
178+
f.write_str(")")?;
179179
}
180180

181181
Ok(())
@@ -204,12 +204,12 @@ impl fmt::Display for Literal {
204204
}
205205

206206
match &self.var {
207-
Some(var) => write!(f, "{}", var)?,
207+
Some(var) => var.fmt(f)?,
208208
None => f.write_str("<invalid>")?,
209209
}
210210

211211
if self.negate {
212-
write!(f, ")")?;
212+
f.write_char(')')?;
213213
}
214214

215215
Ok(())

crates/cfg/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl fmt::Display for CfgDiff {
128128
};
129129
f.write_str(sep)?;
130130

131-
write!(f, "{}", atom)?;
131+
atom.fmt(f)?;
132132
}
133133

134134
if !self.disable.is_empty() {
@@ -146,7 +146,7 @@ impl fmt::Display for CfgDiff {
146146
};
147147
f.write_str(sep)?;
148148

149-
write!(f, "{}", atom)?;
149+
atom.fmt(f)?;
150150
}
151151
}
152152

@@ -170,7 +170,7 @@ impl fmt::Display for InactiveReason {
170170
};
171171
f.write_str(sep)?;
172172

173-
write!(f, "{}", atom)?;
173+
atom.fmt(f)?;
174174
}
175175
let is_are = if self.enabled.len() == 1 { "is" } else { "are" };
176176
write!(f, " {} enabled", is_are)?;
@@ -189,7 +189,7 @@ impl fmt::Display for InactiveReason {
189189
};
190190
f.write_str(sep)?;
191191

192-
write!(f, "{}", atom)?;
192+
atom.fmt(f)?;
193193
}
194194
let is_are = if self.disabled.len() == 1 { "is" } else { "are" };
195195
write!(f, " {} disabled", is_are)?;

crates/hir_def/src/type_ref.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir_expand::{
55
name::{AsName, Name},
66
AstId, InFile,
77
};
8-
use std::convert::TryInto;
8+
use std::{convert::TryInto, fmt::Write};
99
use syntax::ast::{self, HasName};
1010

1111
use crate::{body::LowerCtx, intern::Interned, path::Path};
@@ -364,8 +364,8 @@ pub enum ConstScalarOrPath {
364364
impl std::fmt::Display for ConstScalarOrPath {
365365
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
366366
match self {
367-
ConstScalarOrPath::Scalar(s) => write!(f, "{}", s),
368-
ConstScalarOrPath::Path(n) => write!(f, "{}", n),
367+
ConstScalarOrPath::Scalar(s) => s.fmt(f),
368+
ConstScalarOrPath::Path(n) => n.fmt(f),
369369
}
370370
}
371371
}
@@ -425,10 +425,10 @@ pub enum ConstScalar {
425425
}
426426

427427
impl std::fmt::Display for ConstScalar {
428-
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
428+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
429429
match self {
430-
ConstScalar::Usize(us) => write!(fmt, "{}", us),
431-
ConstScalar::Unknown => write!(fmt, "_"),
430+
ConstScalar::Usize(us) => us.fmt(f),
431+
ConstScalar::Unknown => f.write_char('_'),
432432
}
433433
}
434434
}

crates/hir_expand/src/mod_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Display for ModPath {
121121
f.write_str("::")?;
122122
}
123123
first_segment = false;
124-
write!(f, "{}", segment)?;
124+
segment.fmt(f)?;
125125
}
126126
Ok(())
127127
}

crates/hir_ty/src/consteval.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Constant evaluation details
22
3-
use std::{collections::HashMap, convert::TryInto, fmt::Display};
3+
use std::{
4+
collections::HashMap,
5+
convert::TryInto,
6+
fmt::{Display, Write},
7+
};
48

59
use chalk_ir::{BoundVar, DebruijnIndex, GenericArgData, IntTy, Scalar};
610
use hir_def::{
@@ -79,28 +83,29 @@ impl Display for ComputedExpr {
7983
if *x >= 16 {
8084
write!(f, "{} ({:#X})", x, x)
8185
} else {
82-
write!(f, "{}", x)
86+
x.fmt(f)
8387
}
8488
}
8589
Literal::Uint(x, _) => {
8690
if *x >= 16 {
8791
write!(f, "{} ({:#X})", x, x)
8892
} else {
89-
write!(f, "{}", x)
93+
x.fmt(f)
9094
}
9195
}
92-
Literal::Float(x, _) => write!(f, "{}", x),
93-
Literal::Bool(x) => write!(f, "{}", x),
94-
Literal::Char(x) => write!(f, "{:?}", x),
95-
Literal::String(x) => write!(f, "{:?}", x),
96-
Literal::ByteString(x) => write!(f, "{:?}", x),
96+
Literal::Float(x, _) => x.fmt(f),
97+
Literal::Bool(x) => x.fmt(f),
98+
Literal::Char(x) => std::fmt::Debug::fmt(x, f),
99+
Literal::String(x) => std::fmt::Debug::fmt(x, f),
100+
Literal::ByteString(x) => std::fmt::Debug::fmt(x, f),
97101
},
98102
ComputedExpr::Tuple(t) => {
99-
write!(f, "(")?;
103+
f.write_char('(')?;
100104
for x in &**t {
101-
write!(f, "{}, ", x)?;
105+
x.fmt(f)?;
106+
f.write_str(", ")?;
102107
}
103-
write!(f, ")")
108+
f.write_char(')')
104109
}
105110
}
106111
}

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl fmt::Display for CaseType {
7272
CaseType::UpperCamelCase => "CamelCase",
7373
};
7474

75-
write!(f, "{}", repr)
75+
repr.fmt(f)
7676
}
7777
}
7878

@@ -103,7 +103,7 @@ impl fmt::Display for IdentType {
103103
IdentType::Variant => "Variant",
104104
};
105105

106-
write!(f, "{}", repr)
106+
repr.fmt(f)
107107
}
108108
}
109109

crates/hir_ty/src/tls.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Implementation of Chalk debug helper functions using TLS.
2-
use std::fmt;
2+
use std::fmt::{self, Display};
33

44
use itertools::Itertools;
55

@@ -24,17 +24,17 @@ impl DebugContext<'_> {
2424
AdtId::UnionId(it) => self.0.union_data(it).name.clone(),
2525
AdtId::EnumId(it) => self.0.enum_data(it).name.clone(),
2626
};
27-
write!(f, "{}", name)
27+
name.fmt(f)
2828
}
2929

3030
pub(crate) fn debug_trait_id(
3131
&self,
3232
id: chalk_db::TraitId,
33-
fmt: &mut fmt::Formatter<'_>,
33+
f: &mut fmt::Formatter<'_>,
3434
) -> Result<(), fmt::Error> {
3535
let trait_: hir_def::TraitId = from_chalk_trait_id(id);
3636
let trait_data = self.0.trait_data(trait_);
37-
write!(fmt, "{}", trait_data.name)
37+
trait_data.name.fmt(f)
3838
}
3939

4040
pub(crate) fn debug_assoc_type_id(

crates/ide/src/runnables.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ pub enum TestId {
3838
impl fmt::Display for TestId {
3939
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4040
match self {
41-
TestId::Name(name) => write!(f, "{}", name),
42-
TestId::Path(path) => write!(f, "{}", path),
41+
TestId::Name(name) => name.fmt(f),
42+
TestId::Path(path) => path.fmt(f),
4343
}
4444
}
4545
}

0 commit comments

Comments
 (0)