From 2ee075dc47ec686b48746fd261212b044705fcdc Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Sat, 3 Sep 2022 14:35:41 +0800 Subject: strings: simplify code using unsafe.StringData Updates #54854 Change-Id: I93396dc92bd2decba895f2d059e1aeffcd22312c Reviewed-on: https://go-review.googlesource.com/c/go/+/428158 Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills Reviewed-by: Bryan Mills Reviewed-by: Robert Griesemer --- src/strings/clone_test.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/strings/clone_test.go') diff --git a/src/strings/clone_test.go b/src/strings/clone_test.go index a9ba8add23..77479cfacf 100644 --- a/src/strings/clone_test.go +++ b/src/strings/clone_test.go @@ -5,7 +5,6 @@ package strings_test import ( - "reflect" "strings" "testing" "unsafe" @@ -27,15 +26,12 @@ func TestClone(t *testing.T) { t.Errorf("Clone(%q) = %q; want %q", input, clone, input) } - inputHeader := (*reflect.StringHeader)(unsafe.Pointer(&input)) - cloneHeader := (*reflect.StringHeader)(unsafe.Pointer(&clone)) - if len(input) != 0 && cloneHeader.Data == inputHeader.Data { + if len(input) != 0 && unsafe.StringData(clone) == unsafe.StringData(input) { t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input) } - emptyHeader := (*reflect.StringHeader)(unsafe.Pointer(&emptyString)) - if len(input) == 0 && cloneHeader.Data != emptyHeader.Data { - t.Errorf("Clone(%#v) return value should be equal to empty string.", inputHeader) + if len(input) == 0 && unsafe.StringData(clone) != unsafe.StringData(emptyString) { + t.Errorf("Clone(%#v) return value should be equal to empty string.", unsafe.StringData(input)) } } } -- cgit v1.3