Skip to content

unused: false positive involving unnamed struct types, named struct types and implicit conversions #810

Closed

Description

@kylrth

I discovered this while using the unused linter in golangci-lint. I hope I found the right place to report this issue. 🤞

Here's some code that reproduces it:

package unused_test

import "fmt"

type Thing struct {
	stuff int
	has   struct {
		a bool
		b bool
	}
}

func ExampleThing() {
	type temp struct {
		a bool
		b bool
	}

	stuff := map[string]Thing{
		"hello": {has: temp{true, false}},
	}

	fmt.Println(stuff["hello"].stuff, stuff["hello"].has)
	// Output: 0 {true false}
}

Running ./golangci-lint run unused_test.go results in unused_test.go:14:7: type `temp` is unused (unused).

Activity

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

    @dgryski

    Does this reproduce with the latest staticcheck?

  3. kylrth commented on Aug 12, 2020

    @kylrth
    Author

    Yes, I get the following output with staticcheck built from master on this repo:

    unused_test.go:14:7: type temp is unused (U1000)
    
    Run './staticcheck -explain <check>' or visit https://staticcheck.io/docs/checks for documentation on checks.
    
  4. dominikh commented on Aug 12, 2020

    @dominikh
    Owner

    Simpler reproducer:

    package foo
    
    type Thing struct {
    	has struct {
    		a bool
    	}
    }
    
    func Fn() {
    	type temp struct {
    		a bool
    	}
    
    	x := Thing{
    		has: temp{true},
    	}
    	_ = x
    }
    

    the issue seems to be caused by the implicit conversion to the unnamed struct type in Thing.

  5. removed
    needs-triage
    on Aug 12, 2020
  6. changed the title [-]unused: type declared inside function and used in struct literal[/-] [+]unused: false positive involving unnamed struct types, named struct types and implicit conversions[/+] on Aug 12, 2020
  7. qdm12 commented on Mar 18, 2022

    @qdm12

    I can confirm (found using golangci-lint v1.44.2 built using Go 1.18):

    package main
    
    import (
    	"fmt"
    )
    
    type someStruct struct {
    	parentField struct {
    		n int
    	}
    }
    
    type someStruct2 struct {
    	n int
    }
    
    func main() {
    	x := someStruct{
    		parentField: someStruct2{n: 1},
    	}
    	fmt.Println(x)
    }

    Error:

    type `someStruct2` is unused (unused)
    
  8. added
    started
    on Jun 4, 2022
  9. 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