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

Lines changed: 4 additions & 7 deletions
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

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

pkg/golinters/execinquery.go

Lines changed: 19 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 30 additions & 0 deletions
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)