From a4188fd5c6259f0a76d58b3bb02f39dfe6fcb8ca Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Sat, 25 Feb 2023 23:53:39 +0700 Subject: [PATCH] chore: fix lint --- .golangci.yml | 32 +++++++++++++++++++++----------- internal/imports/formatter.go | 20 ++++++++++++++------ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 4e03a03..363296b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,10 +1,12 @@ run: + timeout: 2m tests: false skip-dirs: - ".*test.*" - ".*mock.*" - ".*generated.*" - ".*example.*" + - ".*utils.*" skip-files: - ".*Mock.*" - ".*_mock.*" @@ -16,6 +18,7 @@ output: linters: disable-all: true enable: + # Default - errcheck - gosimple - govet @@ -23,31 +26,30 @@ linters: - staticcheck - typecheck - unused + # Custom - errchkjson - errname - errorlint - execinquery + - forcetypeassert - gocritic - goerr113 - gofumpt + - gomodguard - gosec - importas - makezero - nilnil - prealloc + - reassign + # - rowserrcheck + - sqlclosecheck - unconvert + # - wastedassign fast: true linters-settings: - gosec: - excludes: - - G101 - - G112 - - G402 - - G404 - - G501 - - G505 - exclude-generated: true + # Default govet: check-shadowing: false disable-all: true @@ -67,9 +69,17 @@ linters-settings: - unmarshal - unreachable - unusedresult - staticcheck: - checks: ["all", "-SA1019"] + # Custom gocritic: + disabled-checks: + - ifElseChain + - singleCaseSwitch enabled-tags: + - diagnostic - style - performance + gosec: + exclude-generated: true + reassign: + patterns: + - ".*" diff --git a/internal/imports/formatter.go b/internal/imports/formatter.go index c678195..5e20f1d 100644 --- a/internal/imports/formatter.go +++ b/internal/imports/formatter.go @@ -36,6 +36,8 @@ var ( ErrEmptyImport = errors.New("empty import") ErrGoModNotExist = errors.New("go mod not exist") ErrGoModEmptyModule = errors.New("go mod empty module") + ErrNotBytesBuffer = errors.New("not bytes.Buffer") + ErrNotDSTGenDecl = errors.New("not dst.GenDecl") ) // https://pkg.go.dev/sync#Pool @@ -78,7 +80,6 @@ func NewFormmater(opts ...FormatterOptionFn) (*Formatter, error) { for _, stdPackage := range stdPackages { ft.stdPackages[stdPackage.PkgPath] = struct{}{} } - // ft.log("NewFormmater: stdPackages: %+v\n", ft.stdPackages) ft.moduleNames = make(map[string]string) ft.formattedPaths = make(map[string]struct{}) @@ -280,8 +281,12 @@ func (ft *Formatter) formatImports( // First update dstFile.Imports = formattedDSTImportSpecs - genSpecs := dstFile.Decls[0].(*dst.GenDecl).Specs - formattedGenSpecs := make([]dst.Spec, 0, len(genSpecs)) + genDecl, ok := dstFile.Decls[0].(*dst.GenDecl) + if !ok { + return nil, ErrNotDSTGenDecl + } + + formattedGenSpecs := make([]dst.Spec, 0, len(genDecl.Specs)) // Append all imports first for _, importSpec := range formattedDSTImportSpecs { @@ -289,7 +294,7 @@ func (ft *Formatter) formatImports( } // Append all non imports later - for _, genSpec := range genSpecs { + for _, genSpec := range genDecl.Specs { if _, ok := genSpec.(*dst.ImportSpec); !ok { formattedGenSpecs = append(formattedGenSpecs, genSpec) continue @@ -297,9 +302,12 @@ func (ft *Formatter) formatImports( } // Second update - dstFile.Decls[0].(*dst.GenDecl).Specs = formattedGenSpecs + genDecl.Specs = formattedGenSpecs - b := bufPool.Get().(*bytes.Buffer) + b, ok := bufPool.Get().(*bytes.Buffer) + if !ok { + return nil, ErrNotBytesBuffer + } b.Reset() defer bufPool.Put(b)