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

flag: compiler not picking up on redefined flags #67679

Closed
udf2457 opened this issue May 28, 2024 · 4 comments
Closed

flag: compiler not picking up on redefined flags #67679

udf2457 opened this issue May 28, 2024 · 4 comments

Comments

@udf2457
Copy link

udf2457 commented May 28, 2024

Go version

go version go1.22.3 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOOS='darwin'
GOPROXY='direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.3'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'

What did you do?

package main

import (
	"flag"
	"fmt"
)

func main() {
	a := flag.String("a", "", "")
	b := flag.String("a", "", "")
	fmt.Println(a)
	fmt.Println(b)
	flag.Parse()
}

What did you see happen?

go build returns no error

What did you expect to see?

Warning about flag redefined

@randall77
Copy link
Contributor

go build does not return an error in this case. Running the program will (a panic).
It is possible vet or some other analysis tool could detect this and report it. But it's probably pretty rare, and is easily detected at runtime, so it is not clear it would qualify for inclusion.

@udf2457
Copy link
Author

udf2457 commented May 28, 2024

is easily detected at runtime

With all due respect, that's a lazy argument.

It makes the developer look sloppy if you ship out code with errors that could and, frankly, should have been prevented by vet or other linting.

@randall77
Copy link
Contributor

It also makes the developer look sloppy if they don't test their binary at all before shipping it.

Assuming your flag.String calls are at the start of the main function, you literally can't run the binary at all, it will immediately fail.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2024
@udf2457
Copy link
Author

udf2457 commented May 29, 2024

Testing code is not for the purpose of finding stupid things a linter should have picked up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants