Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Type inferrence flagged incorrectly #7

Closed
cznic opened this issue Jun 3, 2013 · 5 comments
Closed

Type inferrence flagged incorrectly #7

cznic opened this issue Jun 3, 2013 · 5 comments
Assignees

Comments

@cznic
Copy link

cznic commented Jun 3, 2013

(13:48) jnml@fsc-r550:~/src/tmp/golint$ cat main.go 
package main

type b []byte

func foo() []byte { return nil }

func main() {
        var x b = foo()
}
(13:49) jnml@fsc-r550:~/src/tmp/golint$ golint main.go 
main.go:8:8: should omit type b from declaration of var x; it will be inferred from the right-hand side
(13:49) jnml@fsc-r550:~/src/tmp/golint$ 
@dsymonds
Copy link
Contributor

dsymonds commented Jun 3, 2013

Yes, there's a large class of things that golint gets wrong for that (the more common is when the LHS is an interface). Still, it's relatively unusual to write code like that, and you can work around it easily enough (write "var x = b(foo())"). It'll need the full typechecking wired up to do it completely correctly, and that won't happen for a while.

@peterbourgon
Copy link

Same root cause for this symptom?

package main

import (
    "math"
)

func main() {
    var i uint64 = math.MaxUint64
    println(i)
}
$ golint a.go
a.go:8:8: should omit type uint64 from declaration of var i; it will be inferred from the right-hand side

package main

import (
    "math"
)

func main() {
    i := math.MaxUint64
    println(i)
}
$ golint b.go
$ go run b.go
# command-line-arguments
./b.go:8: constant 18446744073709551615 overflows int

@dsymonds
Copy link
Contributor

Yes, that's the same cause.

@robert-zaremba
Copy link

bumping up.

Other example

var maxTime int64 = 5 * 24 * 3600

and I want to use maxTime with other int64 values. It is ugly to write

var maxTime = int64(5 * 24 * 3600)

@mewmew
Copy link

mewmew commented Aug 23, 2014

Another example for the record:

Link to p.go: http://play.golang.org/p/9DvFBwDYlc

$ go install play/p
$ golint play/p2
play/p/p.go:8:12: should omit type T from declaration of var mask; it will be inferred from the right-hand side

Link to update version of p.go: http://play.golang.org/p/sGuwidVxjN

$ go install play/p
# play/p
play/p/p.go:9: invalid operation: t & mask (mismatched types T and int)

dsymonds added a commit that referenced this issue Sep 1, 2014
This does nothing with the extra information except warn if there's an error
returned by the typechecker (e.g. var not used).

This is the first step towards addressing #7.
@dsymonds dsymonds self-assigned this Sep 1, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants