Skip to content

Commit

Permalink
Merge pull request Antonboom#1 from Antonboom/nilnil
Browse files Browse the repository at this point in the history
nilnil
  • Loading branch information
Antonboom authored Sep 14, 2021
2 parents 1b3fd75 + 81ae6e9 commit df053ae
Show file tree
Hide file tree
Showing 16 changed files with 620 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
go_get:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: ^1.17
- run: cd /tmp && go install github.com/Antonboom/nilnil@latest && nilnil -h

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: golangci/golangci-lint-action@v2
with:
version: latest

tests:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: ^1.17
- uses: actions/checkout@v2
- run: go test -coverprofile=coverage.out ./...
- uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
.vscode/
.DS_Store
coverage.out
70 changes: 70 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
run:
tests: true

issues:
max-same-issues: 0

linters-settings:
gci:
local-prefixes: github.com/Antonboom/nilnil

linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errname
- exhaustive
- exportloopref
- forbidigo
- gci
- goconst
- gocritic
- gocyclo
- godot
- godox
- gofmt
- gofumpt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- ifshort
- importas
- ineffassign
- lll
- makezero
- misspell
- nakedret
- nilerr
- nestif
- noctx
- nolintlint
- prealloc
- predeclared
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
- tagliatelle
- testpackage
- thelper
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign
- whitespace
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Anton Telyshev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# nilnil
50 changes: 50 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# https://taskfile.dev/#/installation
version: '3'

silent: true

tasks:
default:
cmds:
- task: tools:install
- task: tidy
- task: fmt
- task: lint
- task: tests
- task: install

tools:install:
- echo "Install local tools..."
- (which gci > /dev/null) || GO111MODULE=off go install github.com/daixiang0/gci@latest
- (which gofumpt > /dev/null) || GO111MODULE=off go install mvdan.cc/gofumpt@latest

tidy:
cmds:
- echo "Tidy..."
- go mod tidy

fmt:
cmds:
- echo "Fmt..."
- gofumpt -w .
- gci -local github.com/Antonboom/nilnil -w . > /dev/null

lint:
cmds:
- echo "Lint..."
- golangci-lint run --fix ./...

tests:
cmds:
- echo "Tests..."
- go test ./...

tests:coverage:
cmds:
- echo "Tests with coverage..."
- go test -coverprofile=coverage.out ./...

install:
cmds:
- echo "Install..."
- go install ./...
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/Antonboom/nilnil

go 1.17

require golang.org/x/tools v0.1.5

require (
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"golang.org/x/tools/go/analysis/singlechecker"

"github.com/Antonboom/nilnil/pkg/analyzer"
)

func main() {
singlechecker.Main(analyzer.New())
}
115 changes: 115 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package analyzer

import (
"go/ast"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

const reportMsg = "return both the `nil` error and invalid value: use a sentinel error instead"

// New returns new nilnil analyzer.
func New() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "nilnil",
Doc: "Checks that there is no simultaneous return of `nil` error and an invalid value.",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
}

var (
types = []ast.Node{(*ast.TypeSpec)(nil)}

funcAndReturns = []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.FuncLit)(nil),
(*ast.ReturnStmt)(nil),
}
)

type typeSpecByName map[string]*ast.TypeSpec

func run(pass *analysis.Pass) (interface{}, error) {
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

typeSpecs := typeSpecByName{}
insp.Preorder(types, func(n ast.Node) {
t := n.(*ast.TypeSpec)
typeSpecs[t.Name.Name] = t
})

var fs FuncTypeStack
insp.Nodes(funcAndReturns, func(n ast.Node, push bool) (proceed bool) {
switch v := n.(type) {
case *ast.FuncLit:
if push {
fs.Push(v.Type)
} else {
fs.Pop()
}

case *ast.FuncDecl:
if push {
fs.Push(v.Type)
} else {
fs.Pop()
}

case *ast.ReturnStmt:
ft := fs.Top() // Current function.

if !push || len(v.Results) != 2 || ft == nil || ft.Results == nil || len(ft.Results.List) != 2 {
return false
}

fRes1, fRes2 := ft.Results.List[0], ft.Results.List[1]
if !(isDangerNilField(fRes1, typeSpecs) && isErrorField(fRes2)) {
return
}

rRes1, rRes2 := v.Results[0], v.Results[1]
if isNil(rRes1) && isNil(rRes2) {
pass.Reportf(v.Pos(), reportMsg)
}
}

return true
})

return nil, nil
}

func isDangerNilField(f *ast.Field, types typeSpecByName) bool {
return isDangerNilType(f.Type, types)
}

func isDangerNilType(t ast.Expr, types typeSpecByName) bool {
switch v := t.(type) {
case *ast.StarExpr, *ast.ChanType, *ast.FuncType, *ast.InterfaceType, *ast.MapType:
return true
case *ast.Ident:
if t, ok := types[v.Name]; ok {
return isDangerNilType(t.Type, nil)
}
}
return false
}

func isErrorField(f *ast.Field) bool {
return isIdent(f.Type, "error")
}

func isNil(e ast.Expr) bool {
return isIdent(e, "nil")
}

func isIdent(n ast.Node, name string) bool {
i, ok := n.(*ast.Ident)
if !ok {
return false
}
return i.Name == name
}
16 changes: 16 additions & 0 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package analyzer_test

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"

"github.com/Antonboom/nilnil/pkg/analyzer"
)

func TestNilNil(t *testing.T) {
pkgs := []string{
"examples",
}
analysistest.Run(t, analysistest.TestData(), analyzer.New(), pkgs...)
}
Loading

0 comments on commit df053ae

Please sign in to comment.