Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ linters:
- tparallel
- unconvert
- unparam
- unqueryvet
- unused
- usestdlibvars
- usetesting
Expand Down Expand Up @@ -233,6 +234,7 @@ linters:
- tparallel
- unconvert
- unparam
- unqueryvet
- unused
- usestdlibvars
- usetesting
Expand Down Expand Up @@ -3884,6 +3886,21 @@ linters:
# Default: false
check-exported: true

unqueryvet:
# Enable SQL builder checking.
# Default: true
check-sql-builders: false
# Regex patterns for acceptable SELECT * usage.
# Default:
# - "SELECT \\* FROM information_schema\\..*"
# - "SELECT \\* FROM pg_catalog\\..*"
# - "SELECT COUNT\\(\\*\\)"
# - "SELECT MAX\\(\\*\\)"
# - "SELECT MIN\\(\\*\\)"
allowed-patterns:
- "SELECT \\* FROM temp_.*"
- "SELECT \\* FROM.*-- migration"

unused:
# Mark all struct fields that have been written to as used.
# Default: true
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/Antonboom/testifylint v1.6.4
github.com/BurntSushi/toml v1.5.0
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
github.com/MirrexOne/unqueryvet v1.2.1
github.com/OpenPeeDeeP/depguard/v2 v2.2.1
github.com/alecthomas/chroma/v2 v2.20.0
github.com/alecthomas/go-check-sumtype v0.3.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4000,6 +4000,24 @@
}
}
},
"unqueryvetSettings": {
"type": "object",
"additionalProperties": false,
"properties": {
"check-sql-builders": {
"description": "Enable SQL builder checking.",
"type": "boolean",
"default": true
},
"allowed-patterns": {
"description": "Regex patterns for acceptable SELECT * usage.",
"type": "array",
"items": {
"type": "string"
}
}
}
},
"unusedSettings": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -4796,6 +4814,9 @@
"unparam": {
"$ref": "#/definitions/settings/definitions/unparamSettings"
},
"unqueryvet": {
"$ref": "#/definitions/settings/definitions/unqueryvetSettings"
},
"unused": {
"$ref": "#/definitions/settings/definitions/unusedSettings"
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ var defaultLintersSettings = LintersSettings{
SkipRegexp: `(export|internal)_test\.go`,
AllowPackages: []string{"main"},
},
Unqueryvet: UnqueryvetSettings{
CheckSQLBuilders: true,
},
Unused: UnusedSettings{
FieldWritesAreUses: true,
PostStatementsAreReads: false,
Expand Down Expand Up @@ -250,6 +253,7 @@ type LintersSettings struct {
Gomodguard GoModGuardSettings `mapstructure:"gomodguard"`
Gosec GoSecSettings `mapstructure:"gosec"`
Gosmopolitan GosmopolitanSettings `mapstructure:"gosmopolitan"`
Unqueryvet UnqueryvetSettings `mapstructure:"unqueryvet"`
Govet GovetSettings `mapstructure:"govet"`
Grouper GrouperSettings `mapstructure:"grouper"`
Iface IfaceSettings `mapstructure:"iface"`
Expand Down Expand Up @@ -1004,6 +1008,11 @@ type UnparamSettings struct {
CheckExported bool `mapstructure:"check-exported"`
}

type UnqueryvetSettings struct {
CheckSQLBuilders bool `mapstructure:"check-sql-builders"`
AllowedPatterns []string `mapstructure:"allowed-patterns"`
}

type UnusedSettings struct {
FieldWritesAreUses bool `mapstructure:"field-writes-are-uses"`
PostStatementsAreReads bool `mapstructure:"post-statements-are-reads"`
Expand Down
43 changes: 43 additions & 0 deletions pkg/golinters/unqueryvet/testdata/unqueryvet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//golangcitest:args -Eunqueryvet
package testdata

import (
"database/sql"
"fmt"
"strconv"
)

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

var db *sql.DB
rows, _ := db.Query("SELECT * FROM orders WHERE status = ?", "active") // want "avoid SELECT \\* - explicitly specify needed columns for better performance, maintainability and stability"
_ = rows

count := "SELECT COUNT(*) FROM users"
_ = count

goodQuery := "SELECT id, name, email FROM users"
_ = goodQuery

fmt.Println(query)

_ = strconv.Itoa(42)
}

type SQLBuilder interface {
Select(columns ...string) SQLBuilder
From(table string) SQLBuilder
Where(condition string) SQLBuilder
Query() string
}

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

func _(builder SQLBuilder) {
query := builder.Select("id", "name", "price").From("products")
_ = query
}
29 changes: 29 additions & 0 deletions pkg/golinters/unqueryvet/testdata/unqueryvet_custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//golangcitest:args -Eunqueryvet
//golangcitest:config_path testdata/unqueryvet_custom.yml
package testdata

import (
"database/sql"
"fmt"
"strconv"
)

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

var db *sql.DB
rows, _ := db.Query("SELECT * FROM orders WHERE status = ?", "active") // want "avoid SELECT \\* - explicitly specify needed columns for better performance, maintainability and stability"
_ = rows

count := "SELECT COUNT(*) FROM users"
_ = count

goodQuery := "SELECT id, name, email FROM users"
_ = goodQuery

fmt.Println(query)

_ = strconv.Itoa(42)
}

// Custom allowed patterns test - SELECT * from temp tables should be allowed
6 changes: 6 additions & 0 deletions pkg/golinters/unqueryvet/testdata/unqueryvet_custom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "2"

linters:
settings:
unqueryvet:
check-sql-builders: false
24 changes: 24 additions & 0 deletions pkg/golinters/unqueryvet/unqueryvet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package unqueryvet

import (
"github.com/MirrexOne/unqueryvet"
pkgconfig "github.com/MirrexOne/unqueryvet/pkg/config"

"github.com/golangci/golangci-lint/v2/pkg/config"
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
)

func New(settings *config.UnqueryvetSettings) *goanalysis.Linter {
cfg := pkgconfig.DefaultSettings()

if settings != nil {
cfg.CheckSQLBuilders = settings.CheckSQLBuilders
if len(settings.AllowedPatterns) > 0 {
cfg.AllowedPatterns = settings.AllowedPatterns
}
}

return goanalysis.
NewLinterFromAnalyzer(unqueryvet.NewWithConfig(&cfg)).
WithLoadMode(goanalysis.LoadModeSyntax)
}
11 changes: 11 additions & 0 deletions pkg/golinters/unqueryvet/unqueryvet_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package unqueryvet

import (
"testing"

"github.com/golangci/golangci-lint/v2/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import (
"github.com/golangci/golangci-lint/v2/pkg/golinters/tparallel"
"github.com/golangci/golangci-lint/v2/pkg/golinters/unconvert"
"github.com/golangci/golangci-lint/v2/pkg/golinters/unparam"
"github.com/golangci/golangci-lint/v2/pkg/golinters/unqueryvet"
"github.com/golangci/golangci-lint/v2/pkg/golinters/unused"
"github.com/golangci/golangci-lint/v2/pkg/golinters/usestdlibvars"
"github.com/golangci/golangci-lint/v2/pkg/golinters/usetesting"
Expand Down Expand Up @@ -662,6 +663,10 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/mvdan/unparam"),

linter.NewConfig(unqueryvet.New(&cfg.Linters.Settings.Unqueryvet)).
WithSince("v2.5.0").
WithURL("https://github.com/MirrexOne/unqueryvet"),

linter.NewConfig(unused.New(&cfg.Linters.Settings.Unused)).
WithGroups(config.GroupStandard).
WithSince("v1.20.0").
Expand Down
Loading