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

fix: nolint directives syntax #101

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
5 changes: 1 addition & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: lint
uses: golangci/golangci-lint-action@v5.3.0
uses: golangci/golangci-lint-action@v6
with:
version: latest
skip-build-cache: true
skip-pkg-cache: true
args: -D deadcode --skip-dirs "^(cmd|testdata)"

23 changes: 23 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
linters:
enable:
- nolintlint
- gci
- gofumpt
- testifylint

linters-settings:
nolintlint:
allow-unused: false
require-specific: true
gci:
sections:
- standard
- default
- prefix(github.com/butuzov/ireturn)

output:
show-stats: true
sort-results: true
sort-order:
- linter
- file
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ tests:
-coverprofile=coverage.cov ./...

lints:
golangci-lint run --no-config ./... -D deadcode
golangci-lint run

cover:
go tool cover -html=coverage.cov

generate:
scripts/generate-std.sh

install:
go install -trimpath -v -ldflags="-w -s" ./cmd/ireturn/

Expand Down
18 changes: 9 additions & 9 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"strings"
"sync"

"github.com/butuzov/ireturn/analyzer/internal/config"
"github.com/butuzov/ireturn/analyzer/internal/types"

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

"github.com/butuzov/ireturn/analyzer/internal/config"
"github.com/butuzov/ireturn/analyzer/internal/types"
)

const name string = "ireturn" // linter name
Expand All @@ -23,10 +23,10 @@ type validator interface {
}

type analyzer struct {
once sync.Once
mu sync.RWMutex
handler validator
err error
once sync.Once
mu sync.RWMutex
handler validator
err error
disabledNolint bool

found []analysis.Diagnostic
Expand Down Expand Up @@ -128,7 +128,7 @@ func (a *analyzer) readConfiguration(fs *flag.FlagSet) {
}

func NewAnalyzer() *analysis.Analyzer {
a := analyzer{} //nolint: exhaustivestruct
a := analyzer{}

return &analysis.Analyzer{
Name: name,
Expand Down Expand Up @@ -196,7 +196,7 @@ func filterInterfaces(p *analysis.Pass, ft *ast.FuncType, di map[string]struct{}

typeParams := val.String()
prefix, suffix := "interface{", "}"
if strings.HasPrefix(typeParams, prefix) { // nolint: gosimple
if strings.HasPrefix(typeParams, prefix) { //nolint:gosimple
typeParams = typeParams[len(prefix):]
}
if strings.HasSuffix(typeParams, suffix) {
Expand Down
11 changes: 4 additions & 7 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
//nolint: wrapcheck
//nolint: exhaustivestruct

package analyzer

import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/butuzov/ireturn/analyzer/internal/types"
"github.com/stretchr/testify/assert"
"golang.org/x/tools/go/analysis/analysistest"

"github.com/butuzov/ireturn/analyzer/internal/types"
)

const testPackageName = "example"
Expand Down Expand Up @@ -478,9 +475,9 @@ func (tc testCase) xerox(root string) error {
func cp(src, dst string) error {
if location, err := filepath.Abs(src); err != nil {
return err
} else if data, err := ioutil.ReadFile(location); err != nil {
} else if data, err := os.ReadFile(location); err != nil {
return err
} else if err := ioutil.WriteFile(filepath.Join(dst, filepath.Base(src)), data, 0o600); err != nil {
} else if err := os.WriteFile(filepath.Join(dst, filepath.Base(src)), data, 0o600); err != nil {
return err
}

Expand Down
1 change: 0 additions & 1 deletion analyzer/internal/config/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

var ErrCollisionOfInterests = errors.New("can't have both `-accept` and `-reject` specified at same time")

// nolint: exhaustivestruct
func DefaultValidatorConfig() *allowConfig {
return allowAll([]string{
types.NameEmpty, // "empty": empty interfaces (interface{})
Expand Down
2 changes: 1 addition & 1 deletion analyzer/internal/types/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (i IFace) HashString() string {
}

func (i IFace) ExportDiagnostic() analysis.Diagnostic {
return analysis.Diagnostic{ //nolint: exhaustivestruct
return analysis.Diagnostic{
Pos: i.Pos,
Message: i.String(),
}
Expand Down
2 changes: 1 addition & 1 deletion analyzer/std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test_isStdLib(t *testing.T) {
want, name := want, name
t.Run(name, func(t *testing.T) {
got := isStdPkgInterface(name)
assert.Equal(t, got, want,
assert.Equal(t, want, got,
"pkg %s doesn't match expectations (got %v vs want %v)", name, got, want)
})
}
Expand Down
12 changes: 6 additions & 6 deletions analyzer/testdata/disallow_directive_ok.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ package example
/*
Actual disallow directive.
*/
//nolint: ireturn
//nolint:ireturn
func dissAllowDirective1() interface{} { return 1 }

/*
Some other linter in disallow directive.
*/
//nolint: ireturn1
//nolint:return1
func dissAllowDirective2() interface{} { return 1 }

/*
Coma separate linters in nolint directive. golangci-lint compatible mode
*/
//nolint: ireturn, nocode
//nolint:ireturn,nocode
func dissAllowDirective3() interface{} { return 1 }

/*
Example of the good example if interlapsing name and actual ireturn with dot at the end.
*/
//nolint: ireturnlong, ireturn.
//nolint:ireturnlong,ireturn.
func dissAllowDirective4() interface{} { return 1 }

/*
Example of the good example if interlapsing name and actual ireturn with dot at the end.
*/
//nolint: ireturnlong, ireturn
//nolint:ireturnlong,ireturn
func dissAllowDirective5() interface{} { return 1 }

/*
Not works!
*/
//nolint: ireturnlong, itertut ireturn1.
//nolint:ireturnlong,itertutireturn1.
func dissAllowDirective6() interface{} { return 1 }
3 changes: 2 additions & 1 deletion cmd/ireturn/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/butuzov/ireturn/analyzer"
"golang.org/x/tools/go/analysis/singlechecker"

"github.com/butuzov/ireturn/analyzer"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ You can use shorthand for some types of interfaces:

### Disable directive

`golangci-lint` compliant disable directive `//nolint: ireturn` can be used with `ireturn`
`golangci-lint` compliant disable directive `//nolint:ireturn` can be used with `ireturn`

### GitHub Action

Expand Down