When slicing, ignore expressions which could be elided, as in slicing
starting at 0 or ending at len(v).
Fixes #75278
Change-Id: I9c18e29c3d4da9bef89bd25bb261d3cb60e66392
Reviewed-on: https://go-review.googlesource.com/c/go/+/701216
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Mark Freeman <markfreeman@google.com>
if n.X.Op() == ir.OINDEX && isIndexingCoverageCounter(n.X) {
return false
}
+
+ case ir.OSLICE, ir.OSLICEARR, ir.OSLICESTR, ir.OSLICE3, ir.OSLICE3ARR:
+ n := n.(*ir.SliceExpr)
+
+ // Ignore superfluous slicing.
+ if n.Low != nil && n.Low.Op() == ir.OLITERAL && ir.Int64Val(n.Low) == 0 {
+ v.budget++
+ }
+ if n.High != nil && n.High.Op() == ir.OLEN && n.High.(*ir.UnaryExpr).X == n.X {
+ v.budget += 2
+ }
}
v.budget--
"(*B).Loop",
},
"path": {
+ "Base",
"scanChunk",
},
"path/filepath": {
--- /dev/null
+// errorcheck -0 -m=2
+
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+var a, b []int
+
+func NoIndices() { // ERROR "can inline NoIndices with cost 4 as:.*"
+ b = a[:]
+}
+
+func LowIndex() { // ERROR "can inline LowIndex with cost 4 as:.*"
+ b = a[0:]
+}
+
+func HighIndex() { // ERROR "can inline HighIndex with cost 4 as:.*"
+ b = a[:len(a)]
+}
+
+func BothIndices() { // ERROR "can inline BothIndices with cost 4 as:.*"
+ b = a[0:len(a)]
+}