Skip to content

Commit

Permalink
feat: add rowserr linter
Browse files Browse the repository at this point in the history
Add rowserr a linter which checks for missing sql Rows.Err() calls
that supports generics.
  • Loading branch information
stevenh committed Nov 6, 2022
1 parent 95813b7 commit ef1e87c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,14 @@ linters-settings:
severity: warning
disabled: false

rowserr:
# packages sets additional packages to check.
# The following know sql packages are always checked:
# - database/sql
# - github.com/jmoiron/sqlx
# Default: []
packages: []

rowserrcheck:
# database/sql is always checked
# Default: []
Expand Down Expand Up @@ -2035,6 +2043,7 @@ linters:
- promlinter
- reassign
- revive
- rowserr
- rowserrcheck
- scopelint
- sqlclosecheck
Expand Down Expand Up @@ -2142,6 +2151,7 @@ linters:
- promlinter
- reassign
- revive
- rowserr
- rowserrcheck
- scopelint
- sqlclosecheck
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ require (
github.com/spf13/viper v1.12.0
github.com/ssgreg/nlreturn/v2 v2.2.1
github.com/stbenjam/no-sprintf-host-port v0.1.1
github.com/stevenh/go-rowserr v0.2.0
github.com/stretchr/testify v1.8.1
github.com/tdakkota/asciicheck v0.1.1
github.com/tetafro/godot v1.4.11
Expand Down
2 changes: 2 additions & 0 deletions go.sum

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

4 changes: 4 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ type LintersSettings struct {
Promlinter PromlinterSettings
Reassign ReassignSettings
Revive ReviveSettings
RowsErr RowsErrSettings
RowsErrCheck RowsErrCheckSettings
Staticcheck StaticCheckSettings
Structcheck StructCheckSettings
Expand Down Expand Up @@ -599,6 +600,9 @@ type ReviveSettings struct {
Severity string
}
}
type RowsErrSettings struct {
Packages []string
}

type RowsErrCheckSettings struct {
Packages []string
Expand Down
19 changes: 19 additions & 0 deletions pkg/golinters/rowserr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/stevenh/go-rowserr/pkg/rowserr"
"golang.org/x/tools/go/analysis"

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

func NewRowsErr(settings *config.RowsErrSettings) *goanalysis.Linter {
a := rowserr.NewAnalyzer(settings.Packages...)
return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
8 changes: 8 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
promlinterCfg *config.PromlinterSettings
reassignCfg *config.ReassignSettings
reviveCfg *config.ReviveSettings
rowserrCfg *config.RowsErrSettings
rowserrcheckCfg *config.RowsErrCheckSettings
staticcheckCfg *config.StaticCheckSettings
structcheckCfg *config.StructCheckSettings
Expand Down Expand Up @@ -234,6 +235,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
promlinterCfg = &m.cfg.LintersSettings.Promlinter
reassignCfg = &m.cfg.LintersSettings.Reassign
reviveCfg = &m.cfg.LintersSettings.Revive
rowserrCfg = &m.cfg.LintersSettings.RowsErr
rowserrcheckCfg = &m.cfg.LintersSettings.RowsErrCheck
staticcheckCfg = &m.cfg.LintersSettings.Staticcheck
structcheckCfg = &m.cfg.LintersSettings.Structcheck
Expand Down Expand Up @@ -707,6 +709,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
ConsiderSlow().
WithURL("https://github.com/mgechev/revive"),

linter.NewConfig(golinters.NewRowsErr(rowserrCfg)).
WithSince("v1.51.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs, linter.PresetSQL).
WithURL("https://github.com/stevenh/go-rowserr"),

linter.NewConfig(golinters.NewRowsErrCheck(rowserrcheckCfg)).
WithSince("v1.23.0").
WithLoadForGoAnalysis().
Expand Down
17 changes: 17 additions & 0 deletions test/testdata/rowserr/rowserr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//golangcitest:args -Erowserr
package testdata

import (
"database/sql"
)

func RowsErrNotChecked(db *sql.DB) {
rows, err := db.Query("select id from tb") // want "rows.Err\\(\\) must be checked"
if err != nil {
// Handle error.
}

for rows.Next() {
// Handle row.
}
}

0 comments on commit ef1e87c

Please sign in to comment.