Skip to content

Commit b8c061e

Browse files
authored
Add execinquery linter (#2677)
1 parent 293b83d commit b8c061e

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

go.mod

+4-7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/gostaticanalysis/forcetypeassert v0.1.0
4141
github.com/gostaticanalysis/nilerr v0.1.1
4242
github.com/hashicorp/go-multierror v1.1.1
43+
github.com/hashicorp/go-version v1.4.0
4344
github.com/jgautheron/goconst v1.5.1
4445
github.com/jingyugao/rowserrcheck v1.1.1
4546
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
@@ -51,6 +52,7 @@ require (
5152
github.com/ldez/gomoddirectives v0.2.2
5253
github.com/ldez/tagliatelle v0.3.1
5354
github.com/leonklingele/grouper v1.1.0
55+
github.com/lufeee/execinquery v1.0.0
5456
github.com/maratori/testpackage v1.0.1
5557
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0
5658
github.com/mattn/go-colorable v0.1.12
@@ -126,6 +128,7 @@ require (
126128
github.com/gostaticanalysis/comment v1.4.2 // indirect
127129
github.com/hashicorp/errwrap v1.0.0 // indirect
128130
github.com/hashicorp/hcl v1.0.0 // indirect
131+
github.com/hexops/gotextdiff v1.0.3 // indirect
129132
github.com/inconshreveable/mousetrap v1.0.0 // indirect
130133
github.com/kisielk/gotool v1.0.0 // indirect
131134
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
@@ -157,6 +160,7 @@ require (
157160
github.com/valyala/bytebufferpool v1.0.0 // indirect
158161
github.com/yusufpapurcu/wmi v1.2.2 // indirect
159162
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
163+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
160164
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
161165
golang.org/x/text v0.3.7 // indirect
162166
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
@@ -165,10 +169,3 @@ require (
165169
gopkg.in/yaml.v2 v2.4.0 // indirect
166170
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
167171
)
168-
169-
require github.com/hashicorp/go-version v1.4.0
170-
171-
require (
172-
github.com/hexops/gotextdiff v1.0.3 // indirect
173-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
174-
)

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/execinquery.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/lufeee/execinquery"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewExecInQuery() *goanalysis.Linter {
11+
a := execinquery.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeSyntax)
19+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
266266
WithLoadForGoAnalysis().
267267
WithURL("https://github.com/polyfloyd/go-errorlint"),
268268

269+
linter.NewConfig(golinters.NewExecInQuery()).
270+
WithSince("v1.46.0").
271+
WithPresets(linter.PresetSQL).
272+
WithURL("https://github.com/lufeee/execinquery"),
273+
269274
linter.NewConfig(golinters.NewExhaustive(exhaustiveCfg)).
270275
WithSince(" v1.28.0").
271276
WithPresets(linter.PresetBugs).

test/testdata/execinquery.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// args: -Eexecinquery
2+
package testdata
3+
4+
import (
5+
"context"
6+
"database/sql"
7+
)
8+
9+
func execInQuery(db *sql.DB) {
10+
test := "a"
11+
12+
_, err := db.Query("Update * FROM hoge where id = ?", test) // ERROR "It's better to use Execute method instead of Query method to execute `UPDATE` query"
13+
if err != nil {
14+
return
15+
}
16+
17+
db.QueryRow("Update * FROM hoge where id = ?", test) // ERROR "It's better to use Execute method instead of QueryRow method to execute `UPDATE` query"
18+
if err != nil {
19+
return
20+
}
21+
22+
ctx := context.Background()
23+
24+
_, err = db.QueryContext(ctx, "Update * FROM hoge where id = ?", test) // ERROR "It's better to use Execute method instead of QueryContext method to execute `UPDATE` query "
25+
if err != nil {
26+
return
27+
}
28+
29+
db.QueryRowContext(ctx, "Update * FROM hoge where id = ?", test) // ERROR "It's better to use Execute method instead of QueryRowContext method to execute `UPDATE` query"
30+
}

0 commit comments

Comments
 (0)