Skip to content

all: omit unnecessary 0 in slice expression #69170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bufio/bufio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func TestWriter(t *testing.T) {
for l := 0; l < len(written); l++ {
if written[l] != data[l] {
t.Errorf("wrong bytes written")
t.Errorf("want=%q", data[0:len(written)])
t.Errorf("want=%q", data[:len(written)])
t.Errorf("have=%q", written)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/bytes/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestLargeByteWrites(t *testing.T) {
func TestLargeStringReads(t *testing.T) {
var buf Buffer
for i := 3; i < 30; i += 3 {
s := fillString(t, "TestLargeReads (1)", &buf, "", 5, testString[0:len(testString)/i])
s := fillString(t, "TestLargeReads (1)", &buf, "", 5, testString[:len(testString)/i])
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString)))
}
check(t, "TestLargeStringReads (3)", &buf, "")
Expand All @@ -222,7 +222,7 @@ func TestLargeStringReads(t *testing.T) {
func TestLargeByteReads(t *testing.T) {
var buf Buffer
for i := 3; i < 30; i += 3 {
s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[:len(testBytes)/i])
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString)))
}
check(t, "TestLargeByteReads (3)", &buf, "")
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestNil(t *testing.T) {
func TestReadFrom(t *testing.T) {
var buf Buffer
for i := 3; i < 30; i += 3 {
s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[:len(testBytes)/i])
var b Buffer
b.ReadFrom(&buf)
empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(testString)))
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestReadFromNegativeReader(t *testing.T) {
func TestWriteTo(t *testing.T) {
var buf Buffer
for i := 3; i < 30; i += 3 {
s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[:len(testBytes)/i])
var b Buffer
buf.WriteTo(&b)
empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(testString)))
Expand Down
2 changes: 1 addition & 1 deletion src/bytes/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func Join(s [][]byte, sep []byte) []byte {

// HasPrefix reports whether the byte slice s begins with prefix.
func HasPrefix(s, prefix []byte) bool {
return len(s) >= len(prefix) && Equal(s[0:len(prefix)], prefix)
return len(s) >= len(prefix) && Equal(s[:len(prefix)], prefix)
}

// HasSuffix reports whether the byte slice s ends with suffix.
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/internal/notsha256/sha256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestGolden(t *testing.T) {
if j < 2 {
io.WriteString(c, g.in)
} else {
io.WriteString(c, g.in[0:len(g.in)/2])
io.WriteString(c, g.in[:len(g.in)/2])
c.Sum(nil)
io.WriteString(c, g.in[len(g.in)/2:])
}
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/cipher/ctr_aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestCTR_AES(t *testing.T) {
ctr := cipher.NewCTR(c, tt.iv)
encrypted := make([]byte, len(in))
ctr.XORKeyStream(encrypted, in)
if out := tt.out[0:len(in)]; !bytes.Equal(out, encrypted) {
if out := tt.out[:len(in)]; !bytes.Equal(out, encrypted) {
t.Errorf("%s/%d: CTR\ninpt %x\nhave %x\nwant %x", test, len(in), in, encrypted, out)
}
}
Expand All @@ -90,7 +90,7 @@ func TestCTR_AES(t *testing.T) {
ctr := cipher.NewCTR(c, tt.iv)
plain := make([]byte, len(in))
ctr.XORKeyStream(plain, in)
if out := tt.in[0:len(in)]; !bytes.Equal(out, plain) {
if out := tt.in[:len(in)]; !bytes.Equal(out, plain) {
t.Errorf("%s/%d: CTRReader\nhave %x\nwant %x", test, len(out), plain, out)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/internal/cryptotest/aead.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ func TestAEAD(t *testing.T, mAEAD MakeAEAD) {
out := sealMsg(t, aead, prefix, nonce, plaintext, addData)

// Check that Seal didn't alter the prefix
if !bytes.Equal(out[0:len(prefix)], prefix) {
t.Errorf("Seal alters dst instead of appending; got %s, want %s", truncateHex(out[0:len(prefix)]), truncateHex(prefix))
if !bytes.Equal(out[:len(prefix)], prefix) {
t.Errorf("Seal alters dst instead of appending; got %s, want %s", truncateHex(out[:len(prefix)]), truncateHex(prefix))
}

ciphertext := out[len(prefix):]
Expand Down Expand Up @@ -237,8 +237,8 @@ func TestAEAD(t *testing.T, mAEAD MakeAEAD) {
out := openWithoutError(t, aead, prefix, nonce, ciphertext, addData)

// Check that Open didn't alter the prefix
if !bytes.Equal(out[0:len(prefix)], prefix) {
t.Errorf("Open alters dst instead of appending; got %s, want %s", truncateHex(out[0:len(prefix)]), truncateHex(prefix))
if !bytes.Equal(out[:len(prefix)], prefix) {
t.Errorf("Open alters dst instead of appending; got %s, want %s", truncateHex(out[:len(prefix)]), truncateHex(prefix))
}

after := out[len(prefix):]
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/internal/cryptotest/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestHash(t *testing.T, mh MakeHash) {
sum := getSum(t, h, prefix) // Append new digest to prefix

// Check that Sum didn't alter the prefix
if !bytes.Equal(sum[0:len(prefix)], prefix) {
t.Errorf("Sum alters passed buffer instead of appending; got %x, want %x", sum[0:len(prefix)], prefix)
if !bytes.Equal(sum[:len(prefix)], prefix) {
t.Errorf("Sum alters passed buffer instead of appending; got %x, want %x", sum[:len(prefix)], prefix)
}

// Check that the appended sum wasn't affected by the prefix
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/md5/md5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestGolden(t *testing.T) {
if j < 2 {
io.WriteString(c, g.in)
} else if j == 2 {
io.WriteString(c, g.in[0:len(g.in)/2])
io.WriteString(c, g.in[:len(g.in)/2])
c.Sum(nil)
io.WriteString(c, g.in[len(g.in)/2:])
} else if j > 2 {
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/sha1/sha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ func TestGolden(t *testing.T) {
io.WriteString(c, g.in)
sum = c.Sum(nil)
case 2:
io.WriteString(c, g.in[0:len(g.in)/2])
io.WriteString(c, g.in[:len(g.in)/2])
c.Sum(nil)
io.WriteString(c, g.in[len(g.in)/2:])
sum = c.Sum(nil)
case 3:
if boring.Enabled {
continue
}
io.WriteString(c, g.in[0:len(g.in)/2])
io.WriteString(c, g.in[:len(g.in)/2])
c.(*digest).ConstantTimeSum(nil)
io.WriteString(c, g.in[len(g.in)/2:])
sum = c.(*digest).ConstantTimeSum(nil)
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/sha256/sha256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestGolden(t *testing.T) {
if j < 2 {
io.WriteString(c, g.in)
} else {
io.WriteString(c, g.in[0:len(g.in)/2])
io.WriteString(c, g.in[:len(g.in)/2])
c.Sum(nil)
io.WriteString(c, g.in[len(g.in)/2:])
}
Expand All @@ -126,7 +126,7 @@ func TestGolden(t *testing.T) {
if j < 2 {
io.WriteString(c, g.in)
} else {
io.WriteString(c, g.in[0:len(g.in)/2])
io.WriteString(c, g.in[:len(g.in)/2])
c.Sum(nil)
io.WriteString(c, g.in[len(g.in)/2:])
}
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/subtle/constant_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func TestConstantTimeEq(t *testing.T) {

func makeCopy(v int, x, y []byte) []byte {
if len(x) > len(y) {
x = x[0:len(y)]
x = x[:len(y)]
} else {
y = y[0:len(x)]
y = y[:len(x)]
}
if v == 1 {
copy(x, y)
Expand All @@ -90,9 +90,9 @@ func makeCopy(v int, x, y []byte) []byte {

func constantTimeCopyWrapper(v int, x, y []byte) []byte {
if len(x) > len(y) {
x = x[0:len(y)]
x = x[:len(y)]
} else {
y = y[0:len(x)]
y = y[:len(x)]
}
v &= 1
ConstantTimeCopy(v, x, y)
Expand Down
2 changes: 1 addition & 1 deletion src/go/internal/gccgoimporter/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (p *parser) parseQualifiedNameStr(unquotedName string) (pkgpath, name strin
name = parts[0]
default:
// qualified name, which may contain periods
pkgpath = strings.Join(parts[0:len(parts)-1], ".")
pkgpath = strings.Join(parts[:len(parts)-1], ".")
name = parts[len(parts)-1]
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/stringslite/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func HasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}

func HasSuffix(s, suffix string) bool {
Expand Down
4 changes: 2 additions & 2 deletions src/math/big/natdiv.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func (z nat) divLarge(u, uIn, vIn nat) (q, r nat) {
v := *vp
shlVU(v, vIn, shift)
u = u.make(len(uIn) + 1)
u[len(uIn)] = shlVU(u[0:len(uIn)], uIn, shift)
u[len(uIn)] = shlVU(u[:len(uIn)], uIn, shift)

// The caller should not pass aliased z and u, since those are
// the two different outputs, but correct just in case.
Expand Down Expand Up @@ -884,7 +884,7 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) {
if qhatv.cmp(u.norm()) > 0 {
panic("impossible")
}
c := subVV(u[0:len(qhatv)], u[0:len(qhatv)], qhatv)
c := subVV(u[:len(qhatv)], u[:len(qhatv)], qhatv)
if c > 0 {
c = subVW(u[len(qhatv):], u[len(qhatv):], c)
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ func testStreamingGet(t *testing.T, mode testMode) {
var buf [10]byte
for _, str := range []string{"i", "am", "also", "known", "as", "comet"} {
say <- str
n, err := io.ReadFull(res.Body, buf[0:len(str)])
n, err := io.ReadFull(res.Body, buf[:len(str)])
if err != nil {
t.Fatalf("ReadFull on %q: %v", str, err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/regexp/syntax/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ func (p *parser) factor(sub []*Regexp) []*Regexp {
}

// Found end of a run with common leading literal string:
// sub[start:i] all begin with str[0:len(str)], but sub[i]
// sub[start:i] all begin with str[:len(str)], but sub[i]
// does not even begin with str[0].
//
// Factor out common string and append factored expression to out.
Expand Down
2 changes: 1 addition & 1 deletion src/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func rotateRight[E any](s []E, r int) {
rotateLeft(s, len(s)-r)
}

// overlaps reports whether the memory ranges a[0:len(a)] and b[0:len(b)] overlap.
// overlaps reports whether the memory ranges a[:len(a)] and b[:len(b)] overlap.
func overlaps[E any](a, b []E) bool {
if len(a) == 0 || len(b) == 0 {
return false
Expand Down
2 changes: 1 addition & 1 deletion src/time/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func match(s1, s2 string) bool {

func lookup(tab []string, val string) (int, string, error) {
for i, v := range tab {
if len(val) >= len(v) && match(val[0:len(v)], v) {
if len(val) >= len(v) && match(val[:len(v)], v) {
return i, val[len(v):], nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/unicode/utf8/utf8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestDecodeRune(t *testing.T) {
}
r, size = DecodeRune(b[0 : len(b)-1])
if r != RuneError || size != wantsize {
t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b[0:len(b)-1], r, size, RuneError, wantsize)
t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b[:len(b)-1], r, size, RuneError, wantsize)
}
s = m.str[0 : len(m.str)-1]
r, size = DecodeRuneInString(s)
Expand Down