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

staticcheck: consider de facto constant expressions in SA9006 and other checks #833

Open
ainar-g opened this issue Sep 23, 2020 · 0 comments

Comments

@ainar-g
Copy link
Contributor

ainar-g commented Sep 23, 2020

(Inspired by a comment from @go101.)

Consider:

package main

const s = "Go101.org"
// len(s) == 9
// 1 << 9 == 512
// 512 / 128 == 4

var a byte = 1 << len(s) / 128
var b byte = 1 << len(s[:]) / 128

func main() {
	println(a, b)
}
$ go run ./tmp.go
4 0
$ go vet ./tmp.go
$
$ staticcheck --checks=all ./tmp.go
$

staticcheck does flag something like:

var c byte = 1
c <<= len(s)

But len(s[:]) is not constant, even though it can never change. Perhaps staticcheck could be taught to reason about these non-constant-but-immutable expressions.

@ainar-g ainar-g added the needs-triage Newly filed issue that needs triage label Sep 23, 2020
@dominikh dominikh added enhancement and removed needs-triage Newly filed issue that needs triage labels Oct 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants