From ed8003bfbcae8efd42e54895db0554c139b9d3a7 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Wed, 7 Jun 2023 07:11:48 +0800 Subject: [PATCH] errors: optimize *joinError's Error Remove TODOs, stale comment. --- src/errors/join.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/errors/join.go b/src/errors/join.go index f66534c4c38..349fc06ed9f 100644 --- a/src/errors/join.go +++ b/src/errors/join.go @@ -44,24 +44,17 @@ type joinError struct { func (e *joinError) Error() string { // Since Join returns nil if every value in errs is nil, // e.errs cannot be empty. - // TODO: get rid of case 0 - switch len(e.errs) { - case 0: // Impossible but handle. - return "" - case 1: + if len(e.errs) == 1 { return e.errs[0].Error() } - // To avoid doubling the number of Error calls, skip preallocating - // the slice on overflow and let the runtime handle the panic. b := []byte(e.errs[0].Error()) for _, err := range e.errs[1:] { b = append(b, '\n') b = append(b, err.Error()...) } // At this point, b has at least one byte '\n'. - // TODO: replace with unsafe.String(&b[0], len(b)) - return unsafe.String(unsafe.SliceData(b), len(b)) + return unsafe.String(&b[0], len(b)) } func (e *joinError) Unwrap() []error {