GithubHelp home page GithubHelp logo

antonboom / nilnil Goto Github PK

View Code? Open in Web Editor NEW
49.0 49.0 3.0 72 KB

The Golang linter that checks that there is no simultaneous return of `nil` error and an invalid value.

License: MIT License

Go 100.00%
go golang linter static-analysis

nilnil's Introduction

Anton Telyshev (Антон Телышев)

Solution Architect (ex Lead Go Developer) at Exness 💹
Look my way at Linkedin 👨‍💻


Author and teacher of «GOLANG NINJA» online courses:


logo

nilnil's People

Contributors

antonboom avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nilnil's Issues

Detect reverse situation?

return entity, errors.New("something went wrong")

// should be
return nil, errors.New("something went wrong")

Doesn't work with multiple packages

To reproduce, add this to the examples:

import "examples/other"

func iface2() (other.Iface, error) {
	return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

And add this to a new package under "examples":

package other

type Iface interface {
	Do()
}

Reading the code of the linter, the package of the type is not considered in the analysis and *ast.SelectorExpr is not traversed in isDangerNilType, so only types form the same package are analyzed.

One way to "fix" this might be to create a config that bypasses this type based analysis entirely by saying "all types should be checked". That still wouldn't work if a custom config is given and typed based analysis is still desired, but I think it would work for most users.

panic: runtime error when using nilnil ./...

go version: go version go1.22.1 darwin/arm64
nilnil version: v0.1.7

command: nilnil ./...

error message:

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1004baac4]

goroutine 3 [running]:
go/types.(*Checker).handleBailout(0x14000356400, 0x1400158bb98)
        /opt/homebrew/opt/go/libexec/src/go/types/check.go:367 +0x9c
panic({0x100693d80?, 0x100897ca0?})
        /opt/homebrew/opt/go/libexec/src/runtime/panic.go:770 +0x124
go/types.(*StdSizes).Sizeof(0x0, {0x1006ea5e8, 0x10089b8a0})
        /opt/homebrew/opt/go/libexec/src/go/types/sizes.go:228 +0x314
go/types.(*Config).sizeof(...)
        /opt/homebrew/opt/go/libexec/src/go/types/sizes.go:333
go/types.representableConst.func1({0x1006ea5e8?, 0x10089b8a0?})
        /opt/homebrew/opt/go/libexec/src/go/types/const.go:76 +0x9c
go/types.representableConst({0x1006eb928, 0x1008906f0}, 0x14000356400, 0x10089b8a0, 0x1400158b2e8)
        /opt/homebrew/opt/go/libexec/src/go/types/const.go:92 +0x138
go/types.(*Checker).representation(0x14000356400, 0x140015a7c00, 0x10089b8a0)
        /opt/homebrew/opt/go/libexec/src/go/types/const.go:256 +0x68
go/types.(*Checker).implicitTypeAndValue(0x14000356400, 0x140015a7c00, {0x1006ea5e8, 0x10089b8a0})
        /opt/homebrew/opt/go/libexec/src/go/types/expr.go:375 +0x304
go/types.(*Checker).assignment(0x14000356400, 0x140015a7c00, {0x1006ea5e8, 0x10089b8a0}, {0x1005e6e4c, 0x14})
        /opt/homebrew/opt/go/libexec/src/go/types/assignments.go:52 +0x23c
go/types.(*Checker).initConst(0x14000356400, 0x14000e6aae0, 0x140015a7c00)
        /opt/homebrew/opt/go/libexec/src/go/types/assignments.go:126 +0x274
go/types.(*Checker).constDecl(0x14000356400, 0x14000e6aae0, {0x1006eab88, 0x1400051e220}, {0x1006eaeb8, 0x140013260f0}, 0x0)
        /opt/homebrew/opt/go/libexec/src/go/types/decl.go:490 +0x250
go/types.(*Checker).objDecl(0x14000356400, {0x1006ee138, 0x14000e6aae0}, 0x0)
        /opt/homebrew/opt/go/libexec/src/go/types/decl.go:191 +0x84c
go/types.(*Checker).packageObjects(0x14000356400)
        /opt/homebrew/opt/go/libexec/src/go/types/resolver.go:693 +0x468
go/types.(*Checker).checkFiles(0x14000356400, {0x140009f9500, 0xb, 0xb})
        /opt/homebrew/opt/go/libexec/src/go/types/check.go:408 +0x164
go/types.(*Checker).Files(...)
        /opt/homebrew/opt/go/libexec/src/go/types/check.go:372
golang.org/x/tools/go/packages.(*loader).loadPackage(0x1400010e000, 0x140002278f0)
        /Users/jinwu.li/go/pkg/mod/golang.org/x/[email protected]/go/packages/packages.go:1058 +0x8cc
golang.org/x/tools/go/packages.(*loader).loadRecursive.func1()
        /Users/jinwu.li/go/pkg/mod/golang.org/x/[email protected]/go/packages/packages.go:854 +0x178
sync.(*Once).doSlow(0x0?, 0x0?)
        /opt/homebrew/opt/go/libexec/src/sync/once.go:74 +0x100
sync.(*Once).Do(...)
        /opt/homebrew/opt/go/libexec/src/sync/once.go:65
golang.org/x/tools/go/packages.(*loader).loadRecursive(0x0?, 0x0?)
        /Users/jinwu.li/go/pkg/mod/golang.org/x/[email protected]/go/packages/packages.go:842 +0x50
golang.org/x/tools/go/packages.(*loader).refine.func2(0x0?)
        /Users/jinwu.li/go/pkg/mod/golang.org/x/[email protected]/go/packages/packages.go:777 +0x30
created by golang.org/x/tools/go/packages.(*loader).refine in goroutine 1
        /Users/jinwu.li/go/pkg/mod/golang.org/x/[email protected]/go/packages/packages.go:776 +0xaa4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.