Skip to content

Commit e93138f

Browse files
jingyugaojirfag
andcommitted
Add rows.Err weather checked linter (#849)
Add rowserrcheck Co-authored-by: Isaev Denis <idenx@yandex.com>
1 parent 93e93bc commit e93138f

File tree

11 files changed

+402
-0
lines changed

11 files changed

+402
-0
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ linters:
5555
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
5656
disable-all: true
5757
enable:
58+
# - rowserrcheck
5859
- bodyclose
5960
- deadcode
6061
- depguard

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ maligned: Tool to detect Go structs that would take less memory if their fields
228228
misspell: Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
229229
nakedret: Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
230230
prealloc: Finds slice declarations that could potentially be preallocated [fast: true, auto-fix: false]
231+
rowserrcheck: checks whether Err of rows is checked successfully [fast: true, auto-fix: false]
231232
scopelint: Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false]
232233
stylecheck: Stylecheck is a replacement for golint [fast: true, auto-fix: false]
233234
unconvert: Remove unnecessary type conversions [fast: true, auto-fix: false]
@@ -456,6 +457,7 @@ golangci-lint help linters
456457
457458
- [bodyclose](https://github.com/timakin/bodyclose) - checks whether HTTP response body is closed successfully
458459
- [golint](https://github.com/golang/lint) - Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes
460+
- [rowserrcheck](https://github.com/jingyugao/rowserr) - checks whether Err of rows is checked successfully
459461
- [stylecheck](https://github.com/dominikh/go-tools/tree/master/stylecheck) - Stylecheck is a replacement for golint
460462
- [gosec](https://github.com/securego/gosec) - Inspects source code for security problems
461463
- [interfacer](https://github.com/mvdan/interfacer) - Linter that suggests narrower interface types
@@ -983,6 +985,7 @@ linters:
983985
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
984986
disable-all: true
985987
enable:
988+
# - rowserrcheck
986989
- bodyclose
987990
- deadcode
988991
- depguard
@@ -1204,6 +1207,7 @@ Thanks to developers and authors of used linters:
12041207
- [timakin](https://github.com/timakin)
12051208
- [kisielk](https://github.com/kisielk)
12061209
- [golang](https://github.com/golang)
1210+
- [jingyugao](https://github.com/jingyugao)
12071211
- [dominikh](https://github.com/dominikh)
12081212
- [securego](https://github.com/securego)
12091213
- [opennota](https://github.com/opennota)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21
2424
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
2525
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
26+
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a
2627
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
2728
github.com/mattn/go-colorable v0.1.4
2829
github.com/mitchellh/go-homedir v1.1.0

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
4242
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
4343
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
4444
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
45+
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
46+
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
4547
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
4648
github.com/go-toolsmith/astcast v1.0.0 h1:JojxlmI6STnFVG9yOImLeGREv8W2ocNUM+iOhR6jE7g=
4749
github.com/go-toolsmith/astcast v1.0.0/go.mod h1:mt2OdQTeAQcY4DQgPSArJjHCcOwlX+Wl/kwN+LbLGQ4=
@@ -128,6 +130,10 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
128130
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
129131
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
130132
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
133+
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a h1:GmsqmapfzSJkm28dhRoHz2tLRbJmqhU86IPgBtN3mmk=
134+
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a/go.mod h1:xRskid8CManxVta/ALEhJha/pweKBaVG6fWgc0yH25s=
135+
github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5 h1:lrdPtrORjGv1HbbEvKWDUAy97mPpFm4B8hp77tcCUJY=
136+
github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
131137
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
132138
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
133139
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
@@ -148,6 +154,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
148154
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
149155
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
150156
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
157+
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
158+
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
151159
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
152160
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
153161
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
@@ -159,6 +167,8 @@ github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaa
159167
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
160168
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
161169
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
170+
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
171+
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
162172
github.com/mattn/goveralls v0.0.2 h1:7eJB6EqsPhRVxvwEXGnqdO2sJI0PTsrWoTMXEk9/OQc=
163173
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
164174
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
@@ -311,6 +321,7 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
311321
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
312322
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
313323
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
324+
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
314325
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
315326
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
316327
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=

pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ type LintersSettings struct {
179179
MultiIf bool `mapstructure:"multi-if"`
180180
MultiFunc bool `mapstructure:"multi-func"`
181181
}
182+
RowsErrCheck struct {
183+
Packages []string
184+
}
182185

183186
WSL WSLSettings
184187
Lll LllSettings

pkg/golinters/rowerrcheck.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package golinters
2+
3+
import (
4+
"github.com/jingyugao/rowserrcheck/passes/rowserr"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
"github.com/golangci/golangci-lint/pkg/lint/linter"
9+
)
10+
11+
func NewRowsErrCheck() *goanalysis.Linter {
12+
analyzer := rowserr.NewAnalyzer()
13+
return goanalysis.NewLinter(
14+
"rowserrcheck",
15+
"checks whether Err of rows is checked successfully",
16+
[]*analysis.Analyzer{analyzer},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeTypesInfo).
19+
WithContextSetter(func(lintCtx *linter.Context) {
20+
pkgs := lintCtx.Settings().RowsErrCheck.Packages
21+
analyzer.Run = rowserr.NewRun(pkgs...)
22+
})
23+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
108108
WithLoadForGoAnalysis().
109109
WithPresets(linter.PresetStyle).
110110
WithURL("https://github.com/golang/lint"),
111+
linter.NewConfig(golinters.NewRowsErrCheck()).
112+
WithLoadForGoAnalysis().
113+
WithPresets(linter.PresetPerformance, linter.PresetBugs).
114+
WithURL("https://github.com/jingyugao/rowserr"),
111115

112116
linter.NewConfig(golinters.NewStaticcheck()).
113117
WithLoadForGoAnalysis().

test/testdata/rowserrcheck.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//args: -Erowserrcheck
2+
package testdata
3+
4+
import (
5+
"database/sql"
6+
)
7+
8+
func RowsErrNotChecked(db *sql.DB) {
9+
rows, _ := db.Query("select id from tb") // ERROR "rows.Err must be checked"
10+
for rows.Next() {
11+
12+
}
13+
}

vendor/github.com/jingyugao/rowserrcheck/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)