Skip to content

Commit e867567

Browse files
committed
Use write_str() instead of write!() when possible
Reduces code size
1 parent a167cbd commit e867567

File tree

58 files changed

+243
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+243
-243
lines changed

compiler/rustc_ast/src/util/literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl fmt::Display for LitKind {
210210
LitKind::Err => {
211211
// This only shows up in places like `-Zunpretty=hir` output, so we
212212
// don't bother to produce something useful.
213-
write!(f, "<bad-literal>")?;
213+
f.write_str("<bad-literal>")?;
214214
}
215215
}
216216

compiler/rustc_const_eval/src/const_eval/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ impl fmt::Display for ConstEvalErrKind {
3737
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3838
use self::ConstEvalErrKind::*;
3939
match self {
40-
ConstAccessesStatic => write!(f, "constant accesses static"),
40+
ConstAccessesStatic => f.write_str("constant accesses static"),
4141
ModifiedGlobal => {
42-
write!(f, "modifying a static's initial value from another static's initializer")
42+
f.write_str("modifying a static's initial value from another static's initializer")
4343
}
4444
AssertFailure(msg) => write!(f, "{:?}", msg),
4545
Panic { msg, line, col, file } => {

compiler/rustc_const_eval/src/const_eval/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub enum MemoryKind {
147147
impl fmt::Display for MemoryKind {
148148
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149149
match self {
150-
MemoryKind::Heap => write!(f, "heap allocation"),
150+
MemoryKind::Heap => f.write_str("heap allocation"),
151151
}
152152
}
153153
}

compiler/rustc_const_eval/src/interpret/eval_context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
265265
if tcx.def_key(self.instance.def_id()).disambiguated_data.data
266266
== DefPathData::ClosureExpr
267267
{
268-
write!(f, "inside closure")
268+
f.write_str("inside closure")
269269
} else {
270270
// Note: this triggers a `good_path_bug` state, which means that if we ever get here
271271
// we must emit a diagnostic. We should never display a `FrameInfo` unless we
@@ -987,12 +987,12 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> std::fmt::Debug
987987
if frame != self.ecx.frame_idx() {
988988
write!(fmt, " ({} frames up)", self.ecx.frame_idx() - frame)?;
989989
}
990-
write!(fmt, ":")?;
990+
fmt.write_str(":")?;
991991

992992
match self.ecx.stack()[frame].locals[local].value {
993-
LocalValue::Dead => write!(fmt, " is dead")?,
993+
LocalValue::Dead => fmt.write_str(" is dead")?,
994994
LocalValue::Live(Operand::Immediate(Immediate::Uninit)) => {
995-
write!(fmt, " is uninitialized")?
995+
fmt.write_str(" is uninitialized")?
996996
}
997997
LocalValue::Live(Operand::Indirect(mplace)) => {
998998
write!(

compiler/rustc_const_eval/src/interpret/memory.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl<T: MayLeak> MayLeak for MemoryKind<T> {
5050
impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
5151
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5252
match self {
53-
MemoryKind::Stack => write!(f, "stack variable"),
54-
MemoryKind::CallerLocation => write!(f, "caller location"),
53+
MemoryKind::Stack => f.write_str("stack variable"),
54+
MemoryKind::CallerLocation => f.write_str("caller location"),
5555
MemoryKind::Machine(m) => write!(f, "{}", m),
5656
}
5757
}
@@ -893,7 +893,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
893893
// global alloc
894894
match self.ecx.tcx.try_get_global_alloc(id) {
895895
Some(GlobalAlloc::Memory(alloc)) => {
896-
write!(fmt, " (unchanged global, ")?;
896+
fmt.write_str(" (unchanged global, ")?;
897897
write_allocation_track_relocs(
898898
&mut *fmt,
899899
*self.ecx.tcx,
@@ -914,7 +914,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
914914
write!(fmt, " (static: {})", self.ecx.tcx.def_path_str(did))?;
915915
}
916916
None => {
917-
write!(fmt, " (deallocated)")?;
917+
fmt.write_str(" (deallocated)")?;
918918
}
919919
}
920920
}

compiler/rustc_driver_impl/src/args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum Error {
4141
impl fmt::Display for Error {
4242
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
4343
match self {
44-
Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
44+
Error::Utf8Error(None) => fmt.write_str("Utf8 error"),
4545
Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {path}"),
4646
Error::IOError(path, err) => write!(fmt, "IO Error: {path}: {err}"),
4747
}

compiler/rustc_error_messages/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ impl fmt::Display for TranslationBundleError {
7474
write!(f, "could not parse ftl file: {}", e)
7575
}
7676
TranslationBundleError::AddResource(e) => write!(f, "failed to add resource: {}", e),
77-
TranslationBundleError::MissingLocale => write!(f, "missing locale directory"),
77+
TranslationBundleError::MissingLocale => f.write_str("missing locale directory"),
7878
TranslationBundleError::ReadLocalesDir(e) => {
7979
write!(f, "could not read locales dir: {}", e)
8080
}
8181
TranslationBundleError::ReadLocalesDirEntry(e) => {
8282
write!(f, "could not read locales dir entry: {}", e)
8383
}
8484
TranslationBundleError::LocaleIsNotDir => {
85-
write!(f, "`$sysroot/share/locales/$locale` is not a directory")
85+
f.write_str("`$sysroot/share/locales/$locale` is not a directory")
8686
}
8787
}
8888
}

compiler/rustc_expand/src/mbe/macro_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl Display for MatcherLoc {
157157
if let Some(kind) = kind {
158158
write!(f, ":{}", kind)?;
159159
}
160-
write!(f, "`")?;
160+
f.write_str("`")?;
161161
Ok(())
162162
}
163163
MatcherLoc::Eof => f.write_str("end of macro"),

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl std::fmt::Debug for AttributeGate {
7272
Self::Gated(ref stab, name, expl, _) => {
7373
write!(fmt, "Gated({stab:?}, {name}, {expl})")
7474
}
75-
Self::Ungated => write!(fmt, "Ungated"),
75+
Self::Ungated => fmt.write_str("Ungated"),
7676
}
7777
}
7878
}

compiler/rustc_feature/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ pub enum State {
3838
impl fmt::Debug for State {
3939
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4040
match self {
41-
State::Accepted { .. } => write!(f, "accepted"),
42-
State::Active { .. } => write!(f, "active"),
43-
State::Removed { .. } => write!(f, "removed"),
44-
State::Stabilized { .. } => write!(f, "stabilized"),
41+
State::Accepted { .. } => f.write_str("accepted"),
42+
State::Active { .. } => f.write_str("active"),
43+
State::Removed { .. } => f.write_str("removed"),
44+
State::Stabilized { .. } => f.write_str("stabilized"),
4545
}
4646
}
4747
}

compiler/rustc_hir/src/hir.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1620,9 +1620,9 @@ impl ConstContext {
16201620
impl fmt::Display for ConstContext {
16211621
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16221622
match *self {
1623-
Self::Const => write!(f, "constant"),
1624-
Self::Static(_) => write!(f, "static"),
1625-
Self::ConstFn => write!(f, "constant function"),
1623+
Self::Const => f.write_str("constant"),
1624+
Self::Static(_) => f.write_str("static"),
1625+
Self::ConstFn => f.write_str("constant function"),
16261626
}
16271627
}
16281628
}

compiler/rustc_infer/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ impl<'tcx> fmt::Display for FixupError<'tcx> {
531531
"cannot determine the type of this number; \
532532
add a suffix to specify the type explicitly"
533533
),
534-
UnresolvedTy(_) => write!(f, "unconstrained type"),
535-
UnresolvedConst(_) => write!(f, "unconstrained const value"),
534+
UnresolvedTy(_) => f.write_str("unconstrained type"),
535+
UnresolvedConst(_) => f.write_str("unconstrained const value"),
536536
}
537537
}
538538
}

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
702702

703703
impl fmt::Debug for RegionSnapshot {
704704
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
705-
write!(f, "RegionSnapshot")
705+
f.write_str("RegionSnapshot")
706706
}
707707
}
708708

compiler/rustc_infer/src/traits/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> fmt::Debug for traits::FulfillmentErrorCode<'tcx> {
4646
super::CodeConstEquateError(ref a, ref b) => {
4747
write!(f, "CodeConstEquateError({:?}, {:?})", a, b)
4848
}
49-
super::CodeAmbiguity => write!(f, "Ambiguity"),
49+
super::CodeAmbiguity => f.write_str("Ambiguity"),
5050
super::CodeCycle(ref cycle) => write!(f, "Cycle({:?})", cycle),
5151
}
5252
}

compiler/rustc_interface/src/callbacks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) ->
5656
}
5757
Ok(())
5858
})?;
59-
write!(f, ")")
59+
f.write_str(")")
6060
}
6161

6262
/// Sets up the callbacks in prior crates which we want to refer to the

compiler/rustc_macros/src/diagnostics/utils.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,11 @@ impl FromStr for SuggestionKind {
516516
impl fmt::Display for SuggestionKind {
517517
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
518518
match self {
519-
SuggestionKind::Normal => write!(f, "normal"),
520-
SuggestionKind::Short => write!(f, "short"),
521-
SuggestionKind::Hidden => write!(f, "hidden"),
522-
SuggestionKind::Verbose => write!(f, "verbose"),
523-
SuggestionKind::ToolOnly => write!(f, "tool-only"),
519+
SuggestionKind::Normal => f.write_str("normal"),
520+
SuggestionKind::Short => f.write_str("short"),
521+
SuggestionKind::Hidden => f.write_str("hidden"),
522+
SuggestionKind::Verbose => f.write_str("verbose"),
523+
SuggestionKind::ToolOnly => f.write_str("tool-only"),
524524
}
525525
}
526526
}
@@ -822,13 +822,13 @@ impl SubdiagnosticKind {
822822
impl quote::IdentFragment for SubdiagnosticKind {
823823
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
824824
match self {
825-
SubdiagnosticKind::Label => write!(f, "label"),
826-
SubdiagnosticKind::Note => write!(f, "note"),
827-
SubdiagnosticKind::Help => write!(f, "help"),
828-
SubdiagnosticKind::Warn => write!(f, "warn"),
829-
SubdiagnosticKind::Suggestion { .. } => write!(f, "suggestions_with_style"),
825+
SubdiagnosticKind::Label => f.write_str("label"),
826+
SubdiagnosticKind::Note => f.write_str("note"),
827+
SubdiagnosticKind::Help => f.write_str("help"),
828+
SubdiagnosticKind::Warn => f.write_str("warn"),
829+
SubdiagnosticKind::Suggestion { .. } => f.write_str("suggestions_with_style"),
830830
SubdiagnosticKind::MultipartSuggestion { .. } => {
831-
write!(f, "multipart_suggestion_with_style")
831+
f.write_str("multipart_suggestion_with_style")
832832
}
833833
}
834834
}

compiler/rustc_middle/src/dep_graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
4545
Ok(())
4646
})?;
4747

48-
write!(f, ")")
48+
f.write_str(")")
4949
}
5050

5151
fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R

compiler/rustc_middle/src/mir/coverage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl Debug for CoverageKind {
141141
},
142142
rhs.index(),
143143
),
144-
Unreachable => write!(fmt, "Unreachable"),
144+
Unreachable => fmt.write_str("Unreachable"),
145145
}
146146
}
147147
}

compiler/rustc_middle/src/mir/interpret/allocation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'tcx> fmt::Debug for ConstAllocation<'tcx> {
150150
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151151
// The debug representation of this is very verbose and basically useless,
152152
// so don't print it.
153-
write!(f, "ConstAllocation {{ .. }}")
153+
f.write_str("ConstAllocation { .. }")
154154
}
155155
}
156156

compiler/rustc_middle/src/mir/interpret/error.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl fmt::Display for InvalidProgramInfo<'_> {
140140
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141141
use InvalidProgramInfo::*;
142142
match self {
143-
TooGeneric => write!(f, "encountered overly generic constant"),
143+
TooGeneric => f.write_str("encountered overly generic constant"),
144144
AlreadyReported(ErrorGuaranteed { .. }) => {
145145
write!(
146146
f,
@@ -288,15 +288,15 @@ impl fmt::Display for UndefinedBehaviorInfo {
288288
use UndefinedBehaviorInfo::*;
289289
match self {
290290
Ub(msg) => write!(f, "{msg}"),
291-
Unreachable => write!(f, "entering unreachable code"),
291+
Unreachable => f.write_str("entering unreachable code"),
292292
BoundsCheckFailed { ref len, ref index } => {
293293
write!(f, "indexing out of bounds: the len is {len} but the index is {index}")
294294
}
295-
DivisionByZero => write!(f, "dividing by zero"),
296-
RemainderByZero => write!(f, "calculating the remainder with a divisor of zero"),
297-
DivisionOverflow => write!(f, "overflow in signed division (dividing MIN by -1)"),
298-
RemainderOverflow => write!(f, "overflow in signed remainder (dividing MIN by -1)"),
299-
PointerArithOverflow => write!(f, "overflowing in-bounds pointer arithmetic"),
295+
DivisionByZero => f.write_str("dividing by zero"),
296+
RemainderByZero => f.write_str("calculating the remainder with a divisor of zero"),
297+
DivisionOverflow => f.write_str("overflow in signed division (dividing MIN by -1)"),
298+
RemainderOverflow => f.write_str("overflow in signed remainder (dividing MIN by -1)"),
299+
PointerArithOverflow => f.write_str("overflowing in-bounds pointer arithmetic"),
300300
InvalidMeta(msg) => write!(f, "invalid metadata in wide pointer: {msg}"),
301301
UnterminatedCString(p) => write!(
302302
f,
@@ -367,13 +367,13 @@ impl fmt::Display for UndefinedBehaviorInfo {
367367
f,
368368
"using uninitialized data, but this operation requires initialized memory"
369369
),
370-
DeadLocal => write!(f, "accessing a dead local variable"),
370+
DeadLocal => f.write_str("accessing a dead local variable"),
371371
ScalarSizeMismatch(self::ScalarSizeMismatch { target_size, data_size }) => write!(
372372
f,
373373
"scalar size mismatch: expected {target_size} bytes but got {data_size} bytes instead",
374374
),
375375
UninhabitedEnumVariantWritten => {
376-
write!(f, "writing discriminant of an uninhabited enum")
376+
f.write_str("writing discriminant of an uninhabited enum")
377377
}
378378
}
379379
}
@@ -414,7 +414,7 @@ impl fmt::Display for UnsupportedOpInfo {
414414
PartialPointerCopy(ptr) => {
415415
write!(f, "unable to copy parts of a pointer from memory at {ptr:?}")
416416
}
417-
ReadPointerAsBytes => write!(f, "unable to turn pointer into raw bytes"),
417+
ReadPointerAsBytes => f.write_str("unable to turn pointer into raw bytes"),
418418
ThreadLocalStatic(did) => write!(f, "cannot access thread local static ({did:?})"),
419419
ReadExternStatic(did) => write!(f, "cannot read from extern static ({did:?})"),
420420
}
@@ -441,16 +441,16 @@ impl fmt::Display for ResourceExhaustionInfo {
441441
use ResourceExhaustionInfo::*;
442442
match self {
443443
StackFrameLimitReached => {
444-
write!(f, "reached the configured maximum number of stack frames")
444+
f.write_str("reached the configured maximum number of stack frames")
445445
}
446446
StepLimitReached => {
447-
write!(f, "exceeded interpreter step limit (see `#[const_eval_limit]`)")
447+
f.write_str("exceeded interpreter step limit (see `#[const_eval_limit]`)")
448448
}
449449
MemoryExhausted => {
450-
write!(f, "tried to allocate more memory than available to compiler")
450+
f.write_str("tried to allocate more memory than available to compiler")
451451
}
452452
AddressSpaceFull => {
453-
write!(f, "there are no more free addresses in the address space")
453+
f.write_str("there are no more free addresses in the address space")
454454
}
455455
}
456456
}

compiler/rustc_middle/src/mir/interpret/pointer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<Prov: Provenance> fmt::Debug for Pointer<Option<Prov>> {
204204
impl<Prov: Provenance> fmt::Display for Pointer<Option<Prov>> {
205205
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206206
if self.provenance.is_none() && self.offset.bytes() == 0 {
207-
write!(f, "null pointer")
207+
f.write_str("null pointer")
208208
} else {
209209
fmt::Debug::fmt(self, f)
210210
}

0 commit comments

Comments
 (0)