Skip to content

feat: add nomainreturn linter #3524

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,7 @@ linters:
- nlreturn
- noctx
- nolintlint
- nomainreturn
- nonamedreturns
- nosnakecase
- nosprintfhostport
Expand Down Expand Up @@ -2174,6 +2175,7 @@ linters:
- nlreturn
- noctx
- nolintlint
- nomainreturn
- nonamedreturns
- nosnakecase
- nosprintfhostport
Expand Down
17 changes: 9 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/alingse/asasalint v0.0.11
github.com/ashanbrown/forbidigo v1.3.0
github.com/ashanbrown/makezero v1.1.1
github.com/bedakb/nomainreturn v0.0.0-20221126133835-2e22db9471ad
github.com/bkielbasa/cyclop v1.2.0
github.com/blizzy78/varnamelen v0.8.0
github.com/bombsimon/wsl/v3 v3.3.0
Expand Down Expand Up @@ -93,7 +94,7 @@ require (
github.com/sourcegraph/go-diff v0.6.2-0.20221031073116-7ef5f68ebea1
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0
github.com/spf13/viper v1.14.0
github.com/ssgreg/nlreturn/v2 v2.2.1
github.com/stbenjam/no-sprintf-host-port v0.1.1
github.com/stretchr/testify v1.8.1
Expand Down Expand Up @@ -126,7 +127,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ettle/strcase v0.1.1 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-toolsmith/astcast v1.0.0 // indirect
github.com/go-toolsmith/astcopy v1.0.2 // indirect
Expand All @@ -153,7 +154,7 @@ require (
github.com/nbutton23/zxcvbn-go v0.0.0-20210217022336-fa2cb2858354 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
Expand All @@ -165,7 +166,7 @@ require (
github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 // indirect
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
github.com/sivchari/nosnakecase v1.7.0
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
Expand All @@ -175,16 +176,16 @@ require (
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/exp/typeparams v0.0.0-20220827204233-334a2380cb91 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
Expand Down
38 changes: 23 additions & 15 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/golinters/nomainreturn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/bedakb/nomainreturn"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewNoMainReturn() *goanalysis.Linter {
a := nomainreturn.NewAnalyzer(nomainreturn.DefaultConfig)

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetPerformance, linter.PresetBugs).
WithURL("https://github.com/sonatard/noctx"),

linter.NewConfig(golinters.NewNoMainReturn()).
WithSince("v1.51.0").
WithPresets(linter.PresetBugs).
WithURL("https://github.com/bedakb/nomainreturn"),

linter.NewConfig(golinters.NewNoNamedReturns(noNamedReturnsCfg)).
WithSince("v1.46.0").
WithLoadForGoAnalysis().
Expand Down
28 changes: 28 additions & 0 deletions test/testdata/nomainreturn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//golangcitest:args -Enomainreturn
package testdata

func main() {
myFunc := func() {
ok := true
if !ok {
return
}
}
myFunc()

if e := err(); e != nil {
return // want "return found in main"
} else if true {
return // want "return found in main"
} else if false {
return // want "return found in main"
} else {
return // want "return found in main"
}

return // want "return found in main"
}

func err() error {
return nil
}