Skip to content

unused: false positive when only use is constant expression #812

Closed

Description

@peterbourgon
$ staticcheck -version
staticcheck 2020.1.5

$ staticcheck -debug.version
staticcheck 2020.1.5

Compiled with Go version: go1.14.4
Main module:
        honnef.co/go/tools@v0.0.1-2020.1.5 (sum: h1:nI5egYTGJakVyOryqLs1cQO5dO0ksin5XXs2pspk75k=)
Dependencies:
        github.com/BurntSushi/toml@v0.3.1 (sum: h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=)
        golang.org/x/tools@v0.0.0-20191130070609-6e064ea0cf2d (sum: h1:/iIZNFGxc/a7C3yWjGcnboV+Tkc7mxr+p6fDztwoxuM=)

$ go version
go version go1.15 darwin/amd64

go env provided upon request.

package main

import (
	"io"
	"unsafe"
)

var (
	w  io.Writer
	sz = unsafe.Sizeof(w)
)

func main() {
	println(sz)
}
$ go run main.go
16

$ staticcheck main.go
main.go:9:2: var w is unused (U1000)

Activity

  1. added
    needs-triage
    on Aug 20, 2020
  2. dgryski commented on Aug 20, 2020

    @dgryski

    A workaround: sz = unsafe.Sizeof((io.Writer)(nil))

  3. removed
    needs-triage
    on Aug 20, 2020
  4. changed the title [-]unused: false positive for zero value interface at package level[/-] [+]unused: false positive when only use is constant expression[/+] on May 30, 2021
  5. added
    started
    on Jun 4, 2022
  6. kortschak commented on Jun 29, 2022

    @kortschak
    Contributor

    I see a similar case that I can't see a similar work around for.

    package main
    
    import "unsafe"
    
    type t struct {
    	int
    }
    
    const s = unsafe.Sizeof(t{})
    
    func main() {
    	println(s)
    }
    
    $  staticcheck              
    main.go:5:6: type t is unused (U1000)
    
  7. added a commit that references this issue on Feb 27, 2024
    f1a9093
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    false-positivestarted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions