Skip to content
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

strconv: redundant type conversion to int #38682

Closed
semyon-dev opened this issue Apr 26, 2020 · 2 comments
Closed

strconv: redundant type conversion to int #38682

semyon-dev opened this issue Apr 26, 2020 · 2 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@semyon-dev
Copy link

semyon-dev commented Apr 26, 2020

What version of Go are you using (go version)?

go v1.14.2

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

Linux, amd64

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/semyon/.cache/go-build"
GOENV="/home/semyon/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/semyon/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build588385711=/tmp/go-build -gno-record-gcc-switches"

What did you see?

From strconv.ParseInt() line 206:

if bitSize == 0 {
	bitSize = int(IntSize)
}

Redundant type conversion to int. We can just do this:

if bitSize == 0 {
	bitSize = IntSize
}

IntSize is already int (from line 52):

const intSize = 32 << (^uint(0) >> 63)

// IntSize is the size in bits of an int or uint value.
const IntSize = intSize

My suggestion works for both 32bit and 64bit platforms.

@semyon-dev semyon-dev changed the title Unnecessary type conversion Redundant type conversion Apr 26, 2020
@smasher164 smasher164 changed the title Redundant type conversion strconv: redundant type conversion to int Apr 27, 2020
@smasher164 smasher164 added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Apr 27, 2020
@smasher164 smasher164 added this to the Backlog milestone Apr 27, 2020
@gopherbot
Copy link

Change https://golang.org/cl/230306 mentions this issue: strconv: remove redundant conversions to int

@smasher164
Copy link
Member

I annotated IntSize as of type int, ran unconvert on the strconv package at tip, and found the following

/Users/akhil/Projects/go/src/strconv/atoi.go:99:16: unnecessary conversion
		bitSize = int(IntSize)
		             ^
/Users/akhil/Projects/go/src/strconv/atoi.go:206:16: unnecessary conversion
		bitSize = int(IntSize)
		             ^

As a sidenote, more evidence for mdempsky/unconvert#20?

xujianhai666 pushed a commit to xujianhai666/go-1 that referenced this issue May 21, 2020
IntSize is an untyped constant that does not need explicit conversion.
Annotating IntSize as an int and running github.com/mdempsky/unconvert
reveals these two cases.

Fixes golang#38682.

Change-Id: I014646b7457ddcde32474810153229dcf0c269c6
Reviewed-on: https://go-review.googlesource.com/c/go/+/230306
Run-TryBot: Akhil Indurti <aindurti@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
@golang golang locked and limited conversation to collaborators Apr 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants