From 48788436b8c9a87d056e52cf90d493940b610b4d Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Fri, 23 Jan 2026 16:12:27 +0100 Subject: [PATCH] go/token: simplify fixOffset Each time I go to definition of this I am deeply confused at what I am looking, so let's clean this a bit with modern Go. Change-Id: I8f44e78f0cdde9b970388f9b98a2720e6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/738341 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Dmitri Shuralyov Reviewed-by: Alan Donovan --- src/go/token/position.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/go/token/position.go b/src/go/token/position.go index 37017d4374..37a468012d 100644 --- a/src/go/token/position.go +++ b/src/go/token/position.go @@ -276,26 +276,12 @@ func (f *File) AddLineColumnInfo(offset int, filename string, line, column int) // fixOffset fixes an out-of-bounds offset such that 0 <= offset <= f.size. func (f *File) fixOffset(offset int) int { - switch { - case offset < 0: - if !debug { - return 0 - } - case offset > f.size: - if !debug { - return f.size - } - default: - return offset - } - - // only generate this code if needed - if debug { + if debug && !(0 <= offset && offset <= f.size) { panic(fmt.Sprintf("offset %d out of bounds [%d, %d] (position %d out of bounds [%d, %d])", 0 /* for symmetry */, offset, f.size, f.base+offset, f.base, f.base+f.size)) } - return 0 + return max(min(f.size, offset), 0) } // Pos returns the Pos value for the given file offset. -- 2.52.0