Skip to content

Commit 9cf3ad7

Browse files
ldezMirrexOne
authored andcommitted
review
1 parent 78dcd8b commit 9cf3ad7

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
@@ -179,8 +179,8 @@ linters:
179179
- gomodguard
180180
- goprintffuncname
181181
- gosec
182-
- gounqvet
183182
- gosmopolitan
183+
- gounqvet
184184
- govet
185185
- grouper
186186
- iface
@@ -1582,16 +1582,6 @@ linters:
15821582
# Default: "0600"
15831583
G306: "0600"
15841584

1585-
gounqvet:
1586-
# Enable SQL builder checking.
1587-
# Default: true
1588-
check-sql-builders: false
1589-
# Regex patterns for acceptable SELECT * usage.
1590-
# Default: ["SELECT \\* FROM information_schema\\..*", "SELECT \\* FROM pg_catalog\\..*", "SELECT COUNT\\(\\*\\)", "SELECT MAX\\(\\*\\)", "SELECT MIN\\(\\*\\)"]
1591-
allowed-patterns:
1592-
- "SELECT \\* FROM temp_.*"
1593-
- "SELECT \\* FROM.*-- migration"
1594-
15951585
gosmopolitan:
15961586
# Allow and ignore `time.Local` usages.
15971587
#
@@ -1619,6 +1609,21 @@ linters:
16191609
- Hiragana
16201610
- Katakana
16211611

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

jsonschema/golangci.next.jsonschema.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,24 @@
22612261
}
22622262
}
22632263
},
2264+
"gounqvetSettings": {
2265+
"type": "object",
2266+
"additionalProperties": false,
2267+
"properties": {
2268+
"check-sql-builders": {
2269+
"description": "Enable SQL builder checking.",
2270+
"type": "boolean",
2271+
"default": true
2272+
},
2273+
"allowed-patterns": {
2274+
"description": "Regex patterns for acceptable SELECT * usage.",
2275+
"type": "array",
2276+
"items": {
2277+
"type": "string"
2278+
}
2279+
}
2280+
}
2281+
},
22642282
"govetSettings": {
22652283
"type": "object",
22662284
"additionalProperties": false,
@@ -4583,6 +4601,9 @@
45834601
"gosmopolitan": {
45844602
"$ref": "#/definitions/settings/definitions/gosmopolitanSettings"
45854603
},
4604+
"gounqvet": {
4605+
"$ref": "#/definitions/settings/definitions/gounqvetSettings"
4606+
},
45864607
"govet": {
45874608
"$ref": "#/definitions/settings/definitions/govetSettings"
45884609
},

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"`
@@ -592,6 +595,11 @@ type GosmopolitanSettings struct {
592595
WatchForScripts []string `mapstructure:"watch-for-scripts"`
593596
}
594597

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

@@ -1084,8 +1092,3 @@ func (s *CustomLinterSettings) Validate() error {
10841092

10851093
return nil
10861094
}
1087-
1088-
type GounqvetSettings struct {
1089-
CheckSQLBuilders bool `mapstructure:"check-sql-builders"`
1090-
AllowedPatterns []string `mapstructure:"allowed-patterns"`
1091-
}

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"
@@ -394,15 +394,15 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
394394
WithLoadForGoAnalysis().
395395
WithURL("https://github.com/securego/gosec"),
396396

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

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

0 commit comments

Comments
 (0)