Skip to content

Commit

Permalink
fix: nolint directives syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Nov 29, 2024
1 parent eeba613 commit 0ff0134
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 32 deletions.
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)"

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

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

0 comments on commit 0ff0134

Please sign in to comment.