kisielk / errcheck Goto Github PK
View Code? Open in Web Editor NEWerrcheck checks that you checked errors.
License: MIT License
errcheck checks that you checked errors.
License: MIT License
Looks like due to the recent domain change.
$ go get github.com/kisielk/errcheck
# code.google.com/p/go.tools/go/gcimporter
go.tools/go/gcimporter/gcimporter.go:817: cannot use val (type "golang.org/x/tools/go/exact".Value) as type "code.google.com/p/go.tools/go/exact".Value in argument to types.NewConst:
"golang.org/x/tools/go/exact".Value does not implement "code.google.com/p/go.tools/go/exact".Value (wrong type for Kind method)
have Kind() "golang.org/x/tools/go/exact".Kind
want Kind() "code.google.com/p/go.tools/go/exact".Kind
The go1.4 directory structure changed, the stdlib resides in goroot/src immedately instead of being in goroot/src/pkg
% go get github.com/kisielk/errcheck
errcheck/main.go:196: cannot use exprFn (type func(ast.Expr, types.Type, interface {})) as type func(ast.Expr, types.Type, exact.Value) in field value
% go version
go version devel +58f8a30f5b78 Tue Apr 16 14:20:06 2013 -0700 linux/amd64
% uname -a
Linux Gothmaug 3.9.0-rc6 #1 SMP Mon Apr 8 14:40:41 EDT 2013 x86_64 AMD Phenom(tm) 9950 Quad-Core Processor AuthenticAMD GNU/Linux
We use errcheck for CI tests, for instance:
https://travis-ci.org/ncw/rclone/builds/100797264
Since you require Go 1.5, should we just use another tool, if we want to support 1.4 or lower?
errcheck does not compile if I use go.tools repository 1.3 branch:
# github.com/kisielk/errcheck/lib
../../../github.com/kisielk/errcheck/lib/errcheck.go:46: unknown loader.Config field 'AllowErrors' in struct literal
It's a problem, because if you use Go 1.3, godoc requires go.tools 1.3 branch:
https://code.google.com/p/go/issues/detail?id=8769
During the package installation I get:
% go get github.com/kisielk/errcheck
package github.com/kisielk/errcheck
imports go/types: unrecognized import path "go/types"
Hello,
First, many thanks for your tool.
And, just for feedback, I've got some errors while checking some packages (building successfully):
$ errcheck github.com/gwenn/gosqlite
error:failed to check package: could not type check: /home/gwen/Projects/src/github.com/gwenn/gosqlite/cache.go:35:35: undeclared name: Stmt
$ errcheck github.com/gwenn/csvdiff
error:failed to check package: could not type check: /home/gwen/Projects/src/github.com/gwenn/csvdiff/csvdiff.go:274:20: cannot assign hasher (variable of type hash.Hash64) to variable of type Hasher
Regards.
I can't install on Windows.
C:\Users\Bruno>go get github.com/kisielk/errcheck
# code.google.com/p/go.tools/go/types
dev\go\gopath\src\code.google.com\p\go.tools\go\types\exprstring.go:80: x.Slice3 undefined (type *ast.SliceExpr has no field or method Slice3)
dev\go\gopath\src\code.google.com\p\go.tools\go\types\exprstring.go:82: x.Max undefined (type *ast.SliceExpr has no field or method Max)
dev\go\gopath\src\code.google.com\p\go.tools\go\types\exprstring.go:83: x.Max undefined (type *ast.SliceExpr has no field or method Max)
The import for "honnef.co/go/importer" appears to be failing using go1.3 behind a firewall, presumably because an improper redirect to "github.com/dominikh/go-importer" fails.
Please consider updating the import to use the "github.com/dominikh/go-importer" directly, which is more likely to be online all the time as well.
Thanks for writing errcheck!
I recently ran an automated build for boltdb/bolt
and received a bunch of errcheck
errors that were not being shown before. It looks like errcheck
is running against test files now. Is there anyway to set a flag to turn that off?
i use gometalinter -e .test -e vendor -e pkg ./... with errcheck
it report many report on
error return value not checked
but i dont know what mean for this warning. How to fix it?
Why does it take 2 seconds for ErrCheck to run on a small codebase? I wish it could be fast, so I could integrate it with Vim/Syntastic (See vim-syntastic/syntastic#1286) and run the check on every file save :w
.
This commit changes some things in the types.Info
struct.
The following error will fail compilation after trying to go get github.com/kisielk/errcheck:
src/github.com/kisielk/errcheck/lib/errcheck.go:106: unknown types.Info field 'Objects' in struct literal
When installing with go get github.com/kisielk/errcheck
on Windows7 64 bit I get the following error:
..\..\kisielk\errcheck\internal\errcheck.go:75: unknown loader.Config field 'ImportFromBinary' in struct literal
That is all the output.
Installation failed with the following message:
go get github.com/kisielk/errcheck
# github.com/kisielk/errcheck/internal
../../code/go/src/github.com/kisielk/errcheck/internal/errcheck.go:91: unknown loader.Config field 'ImportFromBinary' in struct literal
I'm running Go on tip:
go version
go version devel +bc953b2 Tue Oct 6 03:18:17 2015 +0000 darwin/amd64
go list std | grep -v cmd | xargs -n 1 ~/go/bin/errcheck -ignore 'Close|[wW]rite.|Flush|Seek|[rR]ead.'> stdlibcheck
panic: runtime error: index out of range
goroutine 1 [running]:
main.typeCheck(0xc2000acd40, 0x704298, 0x0, 0x0, 0x0, ...)
/home/dlin/go/src/github.com/kisielk/errcheck/main.go:201 +0x1f1
main.checkFiles(0x704298, 0x0, 0x0, 0xc20006aa00, 0xc200065d40, ...)
/home/dlin/go/src/github.com/kisielk/errcheck/main.go:308 +0x36c
main.main()
/home/dlin/go/src/github.com/kisielk/errcheck/main.go:109 +0x2c9
goroutine 2 [runnable]:
../../kisielk/errcheck/internal/errcheck.go:91: unknown loader.Config field 'ImportFromBinary' in struct literal
./errcheck.go:92: undefined: types.TypeAndValue
Currently things like
_ = f()
a, _ = g()
where _ is a return of type error are considered to be explicitly ignored. In some cases it would be good to check for those, so errcheck should support a flag that enables this.
Current version report lines like:
defer client.Close()
Surely, error check is missing here, but that's how defer works and how all guidelines recommend to use it. Asking user to replace it with:
defer func(){ _ = client.Close() }()
is probably overkill.
Example from @dominikh:
dominikh@dominikh-pc $ errcheck honnef.co/go/foo1
/home/dominikh/projects/go_projects/src/honnef.co/go/foo1/foo1.go:12:2 Meh()
dominikh@dominikh-pc $ errcheck honnef.co/go/foo2
error:failed to check package: could not type check: /home/dominikh/projects/go_projects/src/honnef.co/go/foo2/foo2.go:4:2: could not import honnef.co/go/foo1 (can't find import: honnef.co/go/foo1)
From http://golang.org/cmd/go/:
An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns. As a special case, x/... matches x as well as x's subdirectories. For example, net/... expands to net and packages in its subdirectories.
For example, instead of ignoring all of fmt the ability to ignore just the print functions would be nice.
The implementation of the matching is easy, settling on a good UI for specifying the ignores is the hard part.
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.
github.com/kisielk/errcheck/internal/errcheck.go:74: unknown loader.Config field 'SourceImports' in struct literal
Sorry for not sending a merge request. I am still trying to figure out how to write code. But go the following error while installing vim-go
currently errcheck finds a lot of calls like defer r.Body.Close() or defer db.Close(). Would it be possible to ignore them to make the tool's output more meaningful and get the real problems to better stand out? Thanks!
Not sure how to do this yet, but errcheck should be able to find errors such as:
go f()
and
defer f()
where f()
returns an error. Unfortunately when I tried to do this, it seem that go/types doesn't give a type to the call expression which is part of the go or defer statement.
At least, that is what it looks like:
../../.go/src/github.com/kisielk/errcheck/lib/errcheck.go:105: cannot use tp.callTypes (type map[ast.Expr]types.Type) as type map[ast.Expr]types.TypeAndValue in field value
Directories that begin with underscores (like _workspace) are ignored by most Go tools and the code inside them is not processed. This is used by godeps and other vendoring tools. Running errcheck ./...
in a directory will have errcheck running against the code in Godeps/_workspace/src, which is undesirable in general.
fractal ~/.local/src/github.com/kisielk/errcheck 24085 [:c061276|✔]
01:20 :) go build
# github.com/kisielk/errcheck
./main.go:201: cannot use fset (type *token.FileSet) as type string in function argument
./main.go:201: cannot use astFiles (type []*ast.File) as type *token.FileSet in function argument
./main.go:255: obj.GetPkg undefined (type types.Object has no field or method GetPkg)
./main.go:267: undefined: types.NamedType
./main.go:269: t.Obj undefined (type types.Type has no field or method Obj)
./main.go:272: undefined: types.Result
./main.go:274: t.Values undefined (type types.Type has no field or method Values)
./main.go:367: cannot use pkg.Path (type func() string) as type string in map index
./main.go:368: cannot use pkg.Path (type func() string) as type string in map index
./main.go:374: invalid operation: pkg != nil && pkg.Complete (mismatched types bool and func() bool)
./main.go:374: too many errors
fractal ~/.local/src/github.com/kisielk/errcheck 24086 [:c061276|✔]
01:20 :(
This kind of thing was supposed to be solved by the custom importer, but it doesn't seem to help in this particular case:
$ ./errcheck code.google.com/p/go.tools/go/types
error: failed to check package: could not type check: /home/kamil/src/code.google.com/p/go.tools/go/types/universe.go:61:55: cannot pass argument exact.MakeBool(true) (value of type exact.Value) to variable of type exact.Value
Tried it with types.GcImport as well but that returns a different similar error.
Import cycles as detected by the Go tool result in errcheck getting into an infinite loop :(
It is related to an old issue #45 but I had failed to document the problem properly; here it is:
Preparation
mkdir /mnt/foo/src
cd ~
ln -s /mnt/foo/src
# So that ~/src is a symlink to /mnt/foo/src
I don't think putting GOROOT under a symlink is necessary for this bug to reproduce but since that's my setup, exposing this for completeness. The following should take less than 7 minutes to complete;
git clone -b go1.4.2 https://go.googlesource.com/go ~/src/golang
export GOROOT=~/src/golang
cd ~/src/golang/src
./all.bash
The actual issue I suspect is GOPATH containing a symlink:
export GOPATH=~/src/gopath
mkdir $GOPATH
go get github.com/kisielk/errcheck
cd $GOPATH/src/github.com/kisielk/errcheck
errcheck ./...
Runs fine.
Instead of running on github.com/kisielk/errcheck, it runs on $GOROOT/src/runtime.
16:15:56 production] ~ $ go version
go version go1.5.1 darwin/amd64
[16:16:01 production] ~ $ go get -u github.com/kisielk/errcheck
# github.com/kisielk/errcheck/internal/errcheck
code/src/github.com/kisielk/errcheck/internal/errcheck/errcheck.go:233: impossible type switch case: v.pkg.Info.Types[call].Type (type "go/types".Type) cannot have dynamic type *"golang.org/x/tools/go/types".Named (wrong type for Underlying method)
have Underlying() "golang.org/x/tools/go/types".Type
want Underlying() "go/types".Type
code/src/github.com/kisielk/errcheck/internal/errcheck/errcheck.go:236: impossible type switch case: v.pkg.Info.Types[call].Type (type "go/types".Type) cannot have dynamic type *"golang.org/x/tools/go/types".Pointer (wrong type for Underlying method)
have Underlying() "golang.org/x/tools/go/types".Type
want Underlying() "go/types".Type
code/src/github.com/kisielk/errcheck/internal/errcheck/errcheck.go:239: impossible type switch case: v.pkg.Info.Types[call].Type (type "go/types".Type) cannot have dynamic type *"golang.org/x/tools/go/types".Tuple (wrong type for Underlying method)
have Underlying() "golang.org/x/tools/go/types".Type
want Underlying() "go/types".Type
code/src/github.com/kisielk/errcheck/internal/errcheck/errcheck.go:274: impossible type assertion:
*"golang.org/x/tools/go/types".Builtin does not implement "go/types".Object (wrong type for Parent method)
have Parent() *"golang.org/x/tools/go/types".Scope
want Parent() *"go/types".Scope
Note that errcheck depends on the go/types package which is currently only available in go tip.
I guess in Go 1.1 it works.
This is probably a known limitation, but I couldn't find a ticket for it, so here goes:
errcheck fails if a dependency uses cgo (has import "C"
).
Additionally, it seems impossible to ignore this lack of "C" via -ignorepkg (or I just don't know how to use the thing).
[0 tv@brute ~/src/2013/errcheck-bug-cgo]$ ls
repro.go
[0 tv@brute ~/src/2013/errcheck-bug-cgo]$ cat repro.go
package main
import _ "github.com/jmhodges/levigo"
func main() {
}
[0 tv@brute ~/src/2013/errcheck-bug-cgo]$ errcheck .
error:failed to check package: could not type check: repro.go:3:10: could not import github.com/jmhodges/levigo (/home/tv/go/src/github.com/jmhodges/levigo/conv.go:4:8: could not import C (cannot find package "C" in any of:
/home/tv/src/go/src/pkg/C (from $GOROOT)
/home/tv/go/src/C (from $GOPATH)))
[1 tv@brute ~/src/2013/errcheck-bug-cgo]$ errcheck -ignorepkg=github.com/jmhodges/levigo .
error:failed to check package: could not type check: repro.go:3:10: could not import github.com/jmhodges/levigo (/home/tv/go/src/github.com/jmhodges/levigo/cache.go:6:8: could not import C (cannot find package "C" in any of:
/home/tv/src/go/src/pkg/C (from $GOROOT)
/home/tv/go/src/C (from $GOPATH)))
[1 tv@brute ~/src/2013/errcheck-bug-cgo]$ errcheck -ignorepkg=C .
error:failed to check package: could not type check: repro.go:3:10: could not import github.com/jmhodges/levigo (/home/tv/go/src/github.com/jmhodges/levigo/iterator.go:6:8: could not import C (cannot find package "C" in any of:
/home/tv/src/go/src/pkg/C (from $GOROOT)
/home/tv/go/src/C (from $GOPATH)))
[1 tv@brute ~/src/2013/errcheck-bug-cgo]$
Make the changes from the comments in #9
App engine apps are difficult to configure for errcheck, and don't seem to work even when that is done. My GOPATH
is set to: /home/mjibson/go:/home/mjibson/go_appengine/goroot
. When running errcheck github.com/mjibson/goread/goapp
, I get the following error:
error: failed to check package github.com/mjibson/goread/goapp: could not type check: /home/mjibson/go/src/github.com/mjibson/goread/goapp/admin.go:26:6: could not import github.com/MiniProfiler/go/miniprofiler_gae (/home/mjibson/go/src/github.com/MiniProfiler/go/miniprofiler_gae/miniprofiler_gae.go:26:2: could not import github.com/mjibson/appstats (/home/mjibson/go/src/github.com/mjibson/appstats/cost.go:6:2: could not import code.google.com/p/goprotobuf/proto (/home/mjibson/go/src/code.google.com/p/goprotobuf/proto/pointer_unsafe.go:90:42: cannot convert invalid operand to uintptr)))
That file (pointer_unsafe.go
), has +build !appengine
at the top, which could explain the error. I added this line to lib/util.go, after ctx := build.Default
:
ctx.BuildTags = append(ctx.BuildTags, "appengine")
and the error didn't change. Do you have any other suggestions to get this to run?
Errcheck is case sensitive when searching the imported packages
Would be nice if this project could detect unhandled panic too or at least say where the code can panic.
So we could recover and the software dosen't crash.
unhandled expression at /Network/Cluster/home/kamil/go/src/pkg/net/fd_poll_unix.go:301:21: *ast.IndexExpr &{X:startServersOnce Lbrack:57551 Index:k Rbrack:57553}
Sorry for the potentially dumb question.
I am trying to ignore a package but cannot seem to find the correct format.
I am running the following command in the someone/somerepo
directory
errcheck -asserts -ignorepkg 'github.com/someone/somerepo/fu/bar' ./...
Before #44, one could use errcheck like other go tools (errcheck .
or errcheck ./...
):
$ cd $GOPATH/src/github.com/kisielk/errcheck
$ git checkout 125d5c0d507709f2cb85970b145ae03621f6ffe5
$ go run *.go ./...
example/main.go:17:10 recover() // UNCHECKED
example/main.go:20:15 defer recover() // UNCHECKED
example/main.go:26:3 a() // UNCHECKED
example/main.go:30:3 b() // UNCHECKED
example/main.go:35:5 x.a() // UNCHECKED
example/main.go:40:7 y.t.a() // UNCHECKED
example/main.go:44:9 m1["a"]() // UNCHECKED
example/main.go:53:6 go a() // UNCHECKED
example/main.go:54:9 defer a() // UNCHECKED
lib/errcheck.go:138:16 defer rd.Close()
exit status 1
After #44 :
$ git checkout master
$ git rev-parse --verify HEAD
6523a1db9b999015d645d53a36a450608d6111a2
$ go run *.go ./...
error: failed to check package: could not type check: import "./.": import relative to unknown directory
exit status 2
However, this way still works (fully qualified package name):
$ go run *.go github.com/kisielk/errcheck/...
github.com/kisielk/errcheck/example/main.go:17:10 recover() // UNCHECKED
github.com/kisielk/errcheck/example/main.go:20:15 defer recover() // UNCHECKED
github.com/kisielk/errcheck/example/main.go:26:3 a() // UNCHECKED
github.com/kisielk/errcheck/example/main.go:30:3 b() // UNCHECKED
github.com/kisielk/errcheck/example/main.go:35:5 x.a() // UNCHECKED
github.com/kisielk/errcheck/example/main.go:40:7 y.t.a() // UNCHECKED
github.com/kisielk/errcheck/example/main.go:44:9 m1["a"]() // UNCHECKED
github.com/kisielk/errcheck/example/main.go:53:6 go a() // UNCHECKED
github.com/kisielk/errcheck/example/main.go:54:9 defer a() // UNCHECKED
exit status 1
Hi, is there any plans for support new env variable GO15VENDOREXPERIMENT for go1.5 in errcheck? errcheck can't recognize a package if it was placed in $GOPATH/src/vendor folder.
See also for details https://groups.google.com/forum/#!msg/golang-dev/74zjMON9glU/4lWCRDCRZg0J and https://groups.google.com/d/msg/golang-nuts/nnud1w8N3Ik/hPZx32v7DbQJ
Thank you.
$ go get -u github.com/kisielk/errcheck
# github.com/kisielk/errcheck/lib
../../kisielk/errcheck/lib/errcheck.go:138: impossible type switch case: c.pkg.Info.Types[call].Type (type "golang.org/x/tools/go/types".Type) cannot have dynamic type *"code.google.com/p/go.tools/go/types".Named (wrong type for Underlying method)
have Underlying() "code.google.com/p/go.tools/go/types".Type
want Underlying() "golang.org/x/tools/go/types".Type
../../kisielk/errcheck/lib/errcheck.go:141: impossible type switch case: c.pkg.Info.Types[call].Type (type "golang.org/x/tools/go/types".Type) cannot have dynamic type *"code.google.com/p/go.tools/go/types".Tuple (wrong type for Underlying method)
have Underlying() "code.google.com/p/go.tools/go/types".Type
want Underlying() "golang.org/x/tools/go/types".Type
../../kisielk/errcheck/lib/errcheck.go:168: impossible type assertion:
*"code.google.com/p/go.tools/go/types".Builtin does not implement "golang.org/x/tools/go/types".Object (wrong type for Parent method)
have Parent() *"code.google.com/p/go.tools/go/types".Scope
want Parent() *"golang.org/x/tools/go/types".Scope
go/types is moving once https://codereview.appspot.com/7625048 is submitted. Fix the imports.
Hello,
Thanks for this useful tool, using it everyday. :)
We've started to use go-errors to easily get stacktraces when something goes wrong. It would be nice if errcheck could check for thoses as well.
I'm seeing an isErrorType()
function in the code, would changing this be enough to work?
Basically, I would need to match *errors.Error
as well as error
in the function signatures.
Would it be acceptable to add a flag for additional error types?
Another option would be to check for the error
interface rather than the concrete type, but I don't know if that's feasible.
Best
May simplify the machinery a bit. Need to look in to it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.