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

Check for reassignment before use. #7

Open
kisielk opened this issue Mar 2, 2013 · 14 comments
Open

Check for reassignment before use. #7

kisielk opened this issue Mar 2, 2013 · 14 comments

Comments

@kisielk
Copy link
Owner

kisielk commented Mar 2, 2013

It would be nice to catch code such as:

var (
    x, y int
    err error
)
x, err = foo()
y, err = a(x)
return x, y

This is not an error according to the compiler: http://play.golang.org/p/3DJj2e3-8L
The upcoming ssa package would likely be useful here.

@robfig
Copy link
Contributor

robfig commented Aug 25, 2014

Thanks for the great package! We run errcheck as part of our continuous build at work.

I'm interested in getting this case covered, but it seems like it may be tough. It does seem like the best way would be to use SSA (btw, check out this ssa viewer: http://golang-ssaview.herokuapp.com/), but that seems like it would require basically starting from scratch.

Did you have any subsequent thoughts on the matter (I see this was opened long ago)? Do you think it would be preferable to implement all of errcheck using SSA anyway?

(As an aside, I wonder if it would be faster that way -- it already takes 40+ seconds to check ~20k lines of code.)

@kisielk
Copy link
Owner Author

kisielk commented Aug 25, 2014

Glad to hear it's working well for you :)

I have definitely thought about this some more, but rewriting the whole program using SSA just for this is a lot of work for what is likely very little gain. I'm not sure how much of a performance gain it would yield either.

40+ seconds for 20k lines seems like a long time, the Go standard library takes under 4 seconds to check on my machine.

@robfig
Copy link
Contributor

robfig commented Aug 25, 2014

The 40+ seconds happens on both Darwin & Linux, laptop and workstation, so I don't think it's a quirk of my setup.

FWIW, the Go std library checks quickly for me too. Maybe errcheck has to scan all dependencies as well, which would include standard library, plus the ~50 third-party packages that we use.

@kisielk
Copy link
Owner Author

kisielk commented Aug 25, 2014

Yes, I'm pretty sure the type analysis done by go.types needs to include all dependencies as well. I don't think using SSA would eliminate this since we would still need type analysis to determine which return values are errors.

@robfig
Copy link
Contributor

robfig commented Aug 25, 2014

That's too bad. In theory, it should be sufficient to look only at the signatures of functions that are directly called (the first degree dependencies). That should be much faster than constructing the AST for my entire GOPATH

@kisielk
Copy link
Owner Author

kisielk commented Aug 25, 2014

hm yes, that's true. I'll have to look in to it some more when I have time, I'm pretty sure when I originally wrote errcheck that wasn't possible to do.

@kisielk
Copy link
Owner Author

kisielk commented Aug 25, 2014

but the APIs of all the type-related packages have evolved a lot since then.

@dmage
Copy link

dmage commented Jul 12, 2015

Also it would be nice to catch unhandled error in this example:

package main

import "net/http"

func main() {
    resp1, err := http.Get("1")
    resp1 = resp1

    resp2, err := http.Get("2")
    if err != nil {
        panic(err)
    }
    resp2 = resp2
}

@kisielk
Copy link
Owner Author

kisielk commented Jul 14, 2015

I agree. Will have to learn about using the SSA package to figure out how
to do this. If you have any ideas or examples any help would be welcome.
On Sun, Jul 12, 2015 at 05:41 Oleg Bulatov notifications@github.com wrote:

Also it would be nice to catch unhandled error in this example:

package main

import "net/http"

func main() {
resp1, err := http.Get("1")
resp1 = resp1

resp2, err := http.Get("2")
if err != nil {
    panic(err)
}
resp2 = resp2

}


Reply to this email directly or view it on GitHub
#7 (comment).

@mhamrle
Copy link

mhamrle commented Mar 14, 2016

this tool does something similar https://github.com/gordonklaus/ineffassign . I can catch example from first comment.

@tamird
Copy link

tamird commented Nov 18, 2016

https://github.com/dominikh/go-staticcheck catches this sort of thing, FWIW.

@echlebek
Copy link
Collaborator

@kisielk should we close this, given that staticcheck has it covered?

@kisielk
Copy link
Owner Author

kisielk commented Oct 23, 2020

I think it still falls in the scope of this package, maybe it will get added at some point

@prestonvanloon
Copy link

Here's a test case for potential reassignment: 8320fd2f

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

7 participants