Skip to content

Commit 3817c96

Browse files
committed
review
1 parent 0dc997d commit 3817c96

File tree

9 files changed

+131
-36
lines changed

9 files changed

+131
-36
lines changed

.golangci.next.reference.yml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ linters:
6666
- gomodguard
6767
- goprintffuncname
6868
- gosec
69-
- gounqvet
7069
- gosmopolitan
70+
- gounqvet
7171
- govet
7272
- grouper
7373
- iface
@@ -178,8 +178,8 @@ linters:
178178
- gomodguard
179179
- goprintffuncname
180180
- gosec
181-
- gounqvet
182181
- gosmopolitan
182+
- gounqvet
183183
- govet
184184
- grouper
185185
- iface
@@ -1580,16 +1580,6 @@ linters:
15801580
# Default: "0600"
15811581
G306: "0600"
15821582

1583-
gounqvet:
1584-
# Enable SQL builder checking.
1585-
# Default: true
1586-
check-sql-builders: false
1587-
# Regex patterns for acceptable SELECT * usage.
1588-
# Default: ["SELECT \\* FROM information_schema\\..*", "SELECT \\* FROM pg_catalog\\..*", "SELECT COUNT\\(\\*\\)", "SELECT MAX\\(\\*\\)", "SELECT MIN\\(\\*\\)"]
1589-
allowed-patterns:
1590-
- "SELECT \\* FROM temp_.*"
1591-
- "SELECT \\* FROM.*-- migration"
1592-
15931583
gosmopolitan:
15941584
# Allow and ignore `time.Local` usages.
15951585
#
@@ -1617,6 +1607,21 @@ linters:
16171607
- Hiragana
16181608
- Katakana
16191609

1610+
gounqvet:
1611+
# Enable SQL builder checking.
1612+
# Default: true
1613+
check-sql-builders: false
1614+
# Regex patterns for acceptable SELECT * usage.
1615+
# Default:
1616+
# - "SELECT \\* FROM information_schema\\..*"
1617+
# - "SELECT \\* FROM pg_catalog\\..*"
1618+
# - "SELECT COUNT\\(\\*\\)"
1619+
# - "SELECT MAX\\(\\*\\)"
1620+
# - "SELECT MIN\\(\\*\\)"
1621+
allowed-patterns:
1622+
- "SELECT \\* FROM temp_.*"
1623+
- "SELECT \\* FROM.*-- migration"
1624+
16201625
govet:
16211626
# Disable all analyzers.
16221627
# Default: false

jsonschema/golangci.next.jsonschema.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,24 @@
22602260
}
22612261
}
22622262
},
2263+
"gounqvetSettings": {
2264+
"type": "object",
2265+
"additionalProperties": false,
2266+
"properties": {
2267+
"check-sql-builders": {
2268+
"description": "Enable SQL builder checking.",
2269+
"type": "boolean",
2270+
"default": true
2271+
},
2272+
"allowed-patterns": {
2273+
"description": "Regex patterns for acceptable SELECT * usage.",
2274+
"type": "array",
2275+
"items": {
2276+
"type": "string"
2277+
}
2278+
}
2279+
}
2280+
},
22632281
"govetSettings": {
22642282
"type": "object",
22652283
"additionalProperties": false,
@@ -4571,6 +4589,9 @@
45714589
"gosmopolitan": {
45724590
"$ref": "#/definitions/settings/definitions/gosmopolitanSettings"
45734591
},
4592+
"gounqvet": {
4593+
"$ref": "#/definitions/settings/definitions/gounqvetSettings"
4594+
},
45744595
"govet": {
45754596
"$ref": "#/definitions/settings/definitions/govetSettings"
45764597
},

pkg/config/linters_settings.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ var defaultLintersSettings = LintersSettings{
8686
EscapeHatches: []string{},
8787
WatchForScripts: []string{"Han"},
8888
},
89+
Gounqvet: GounqvetSettings{
90+
CheckSQLBuilders: true,
91+
},
8992
Inamedparam: INamedParamSettings{
9093
SkipSingleParam: false,
9194
},
@@ -249,8 +252,8 @@ type LintersSettings struct {
249252
Gomodguard GoModGuardSettings `mapstructure:"gomodguard"`
250253
Gosec GoSecSettings `mapstructure:"gosec"`
251254
Gosmopolitan GosmopolitanSettings `mapstructure:"gosmopolitan"`
252-
Govet GovetSettings `mapstructure:"govet"`
253255
Gounqvet GounqvetSettings `mapstructure:"gounqvet"`
256+
Govet GovetSettings `mapstructure:"govet"`
254257
Grouper GrouperSettings `mapstructure:"grouper"`
255258
Iface IfaceSettings `mapstructure:"iface"`
256259
ImportAs ImportAsSettings `mapstructure:"importas"`
@@ -591,6 +594,11 @@ type GosmopolitanSettings struct {
591594
WatchForScripts []string `mapstructure:"watch-for-scripts"`
592595
}
593596

597+
type GounqvetSettings struct {
598+
CheckSQLBuilders bool `mapstructure:"check-sql-builders"`
599+
AllowedPatterns []string `mapstructure:"allowed-patterns"`
600+
}
601+
594602
type GovetSettings struct {
595603
Go string `mapstructure:"-"`
596604

@@ -1079,8 +1087,3 @@ func (s *CustomLinterSettings) Validate() error {
10791087

10801088
return nil
10811089
}
1082-
1083-
type GounqvetSettings struct {
1084-
CheckSQLBuilders bool `mapstructure:"check-sql-builders"`
1085-
AllowedPatterns []string `mapstructure:"allowed-patterns"`
1086-
}

pkg/golinters/gounqvet/gounqvet.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
package gounqvet
22

33
import (
4-
"golang.org/x/tools/go/analysis"
5-
64
"github.com/MirrexOne/gounqvet"
75
pkgconfig "github.com/MirrexOne/gounqvet/pkg/config"
6+
87
"github.com/golangci/golangci-lint/v2/pkg/config"
98
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
109
)
1110

1211
func New(settings *config.GounqvetSettings) *goanalysis.Linter {
1312
var cfg *pkgconfig.GounqvetSettings
13+
1414
if settings != nil {
1515
cfg = &pkgconfig.GounqvetSettings{
1616
CheckSQLBuilders: settings.CheckSQLBuilders,
1717
AllowedPatterns: settings.AllowedPatterns,
1818
}
1919
}
2020

21-
analyzer := gounqvet.NewWithConfig(cfg)
22-
23-
return goanalysis.NewLinter(
24-
"gounqvet",
25-
"Detects SELECT * usage in SQL queries and SQL builders, encouraging explicit column selection",
26-
[]*analysis.Analyzer{analyzer},
27-
nil,
28-
).WithLoadMode(goanalysis.LoadModeSyntax)
21+
return goanalysis.
22+
NewLinterFromAnalyzer(gounqvet.NewWithConfig(cfg)).
23+
WithLoadMode(goanalysis.LoadModeSyntax)
2924
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package gounqvet
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/v2/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}

pkg/golinters/gounqvet/testdata/gounqvet.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"strconv"
88
)
99

10-
func badQueries() {
10+
// badQueries
11+
func _() {
1112
query := "SELECT * FROM users" // want "avoid SELECT \\* - explicitly specify needed columns for better performance, maintainability and stability"
1213

1314
var db *sql.DB
@@ -35,12 +36,14 @@ type SQLBuilder interface {
3536
Query() string
3637
}
3738

38-
func badSQLBuilder(builder SQLBuilder) {
39+
// badSQLBuilder
40+
func _(builder SQLBuilder) {
3941
query := builder.Select("*").From("products") // want "avoid SELECT \\* in SQL builder - explicitly specify columns to prevent unnecessary data transfer and schema change issues"
4042
_ = query
4143
}
4244

43-
func goodSQLBuilder(builder SQLBuilder) {
45+
// goodSQLBuilder
46+
func _(builder SQLBuilder) {
4447
// Good usage - should not trigger
4548
query := builder.Select("id", "name", "price").From("products")
4649
_ = query
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//golangcitest:args -Egounqvet
2+
//golangcitest:config_path testdata/gounqvet_custom.yml
3+
package testdata
4+
5+
import (
6+
"database/sql"
7+
"fmt"
8+
"strconv"
9+
)
10+
11+
// badQueries
12+
func _() {
13+
query := "SELECT * FROM users" // want "avoid SELECT \\* - explicitly specify needed columns for better performance, maintainability and stability"
14+
15+
var db *sql.DB
16+
rows, _ := db.Query("SELECT * FROM orders WHERE status = ?", "active") // want "avoid SELECT \\* - explicitly specify needed columns for better performance, maintainability and stability"
17+
_ = rows
18+
19+
// This should not trigger because it's a COUNT function
20+
count := "SELECT COUNT(*) FROM users"
21+
_ = count
22+
23+
// Good queries (should not trigger)
24+
goodQuery := "SELECT id, name, email FROM users"
25+
_ = goodQuery
26+
27+
fmt.Println(query)
28+
29+
// Use strconv to satisfy std lib import requirement
30+
_ = strconv.Itoa(42)
31+
}
32+
33+
type SQLBuilder interface {
34+
Select(columns ...string) SQLBuilder
35+
From(table string) SQLBuilder
36+
Where(condition string) SQLBuilder
37+
Query() string
38+
}
39+
40+
// badSQLBuilder
41+
func _(builder SQLBuilder) {
42+
query := builder.Select("*").From("products")
43+
_ = query
44+
}
45+
46+
// goodSQLBuilder
47+
func _(builder SQLBuilder) {
48+
// Good usage - should not trigger
49+
query := builder.Select("id", "name", "price").From("products")
50+
_ = query
51+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
gounqvet:
6+
check-sql-builders: false

pkg/lint/lintersdb/builder_linter.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ import (
5555
"github.com/golangci/golangci-lint/v2/pkg/golinters/goprintffuncname"
5656
"github.com/golangci/golangci-lint/v2/pkg/golinters/gosec"
5757
"github.com/golangci/golangci-lint/v2/pkg/golinters/gosmopolitan"
58-
"github.com/golangci/golangci-lint/v2/pkg/golinters/govet"
5958
"github.com/golangci/golangci-lint/v2/pkg/golinters/gounqvet"
59+
"github.com/golangci/golangci-lint/v2/pkg/golinters/govet"
6060
"github.com/golangci/golangci-lint/v2/pkg/golinters/grouper"
6161
"github.com/golangci/golangci-lint/v2/pkg/golinters/iface"
6262
"github.com/golangci/golangci-lint/v2/pkg/golinters/importas"
@@ -393,15 +393,15 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
393393
WithLoadForGoAnalysis().
394394
WithURL("https://github.com/securego/gosec"),
395395

396-
linter.NewConfig(gounqvet.New(&cfg.Linters.Settings.Gounqvet)).
397-
WithSince("v2.5.0").
398-
WithURL("https://github.com/MirrexOne/gounqvet"),
399-
400396
linter.NewConfig(gosmopolitan.New(&cfg.Linters.Settings.Gosmopolitan)).
401397
WithSince("v1.53.0").
402398
WithLoadForGoAnalysis().
403399
WithURL("https://github.com/xen0n/gosmopolitan"),
404400

401+
linter.NewConfig(gounqvet.New(&cfg.Linters.Settings.Gounqvet)).
402+
WithSince("v2.5.0").
403+
WithURL("https://github.com/MirrexOne/gounqvet"),
404+
405405
linter.NewConfig(govet.New(&cfg.Linters.Settings.Govet)).
406406
WithGroups(config.GroupStandard).
407407
WithSince("v1.0.0").

0 commit comments

Comments
 (0)