Skip to content

Commit

Permalink
improve gocritic linter and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed May 30, 2023
1 parent e663076 commit e7a7718
Show file tree
Hide file tree
Showing 85 changed files with 835 additions and 664 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
*.out
vendor
changes.txt
./internal/cmd/gtrace/gtrace
./internal/cmd/gtrace/gtrace
examples/.golangci.yml
tests/slo/.golangci.yml
101 changes: 100 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,105 @@ linters-settings:
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false

gocritic:
disabled-checks:
- whyNoLint # https://github.com/go-critic/go-critic/issues/1063
- importShadow
- sloppyReassign
# - typeDefFirst
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks.
# See https://github.com/go-critic/go-critic#usage -> section "Tags".
# Default: []
enabled-tags:
- diagnostic
- style
- performance
- experimental
- opinionated
# Settings passed to gocritic.
# The settings key is the name of a supported gocritic checker.
# The list of supported checkers can be find in https://go-critic.github.io/overview.
settings:
# Must be valid enabled check name.
captLocal:
# Whether to restrict checker to params only.
# Default: true
paramsOnly: false
elseif:
# Whether to skip balanced if-else pairs.
# Default: true
skipBalanced: false
hugeParam:
# Size in bytes that makes the warning trigger.
# Default: 80
sizeThreshold: 70
nestingReduce:
# Min number of statements inside a branch to trigger a warning.
# Default: 5
bodyWidth: 4
rangeExprCopy:
# Size in bytes that makes the warning trigger.
# Default: 512
sizeThreshold: 516
# Whether to check test functions
# Default: true
skipTestFuncs: false
rangeValCopy:
# Size in bytes that makes the warning trigger.
# Default: 128
sizeThreshold: 32
# Whether to check test functions.
# Default: true
skipTestFuncs: false
ruleguard:
# Enable debug to identify which 'Where' condition was rejected.
# The value of the parameter is the name of a function in a ruleguard file.
#
# When a rule is evaluated:
# If:
# The Match() clause is accepted; and
# One of the conditions in the Where() clause is rejected,
# Then:
# ruleguard prints the specific Where() condition that was rejected.
#
# The flag is passed to the ruleguard 'debug-group' argument.
# Default: ""
debug: 'emptyDecl'
# Deprecated, use 'failOn' param.
# If set to true, identical to failOn='all', otherwise failOn=''
failOnError: false
# Determines the behavior when an error occurs while parsing ruleguard files.
# If flag is not set, log error and skip rule files that contain an error.
# If flag is set, the value must be a comma-separated list of error conditions.
# - 'all': fail on all errors.
# - 'import': ruleguard rule imports a package that cannot be found.
# - 'dsl': gorule file does not comply with the ruleguard DSL.
# Default: ""
failOn: dsl
# Comma-separated list of enabled groups or skip empty to enable everything.
# Tags can be defined with # character prefix.
# Default: "<all>"
enable: "myGroupName,#myTagName"
# Comma-separated list of disabled groups or skip empty to enable everything.
# Tags can be defined with # character prefix.
# Default: ""
disable: "myGroupName,#myTagName"
tooManyResultsChecker:
# Maximum number of results.
# Default: 5
maxResults: 10
truncateCmp:
# Whether to skip int/uint/uintptr types.
# Default: true
skipArchDependent: false
underef:
# Whether to skip (*x).method() calls where x is a pointer receiver.
# Default: true
skipRecvDeref: false
unnamedResult:
# Whether to check exported functions.
# Default: false
checkExported: true
linters:
disable-all: true
enable:
Expand Down Expand Up @@ -238,5 +336,6 @@ issues:
linters:
- unused
- unparam
- gocritic
- path: _test\.go
text: "ydb.Connection is deprecated"
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Improved code with `go-critic` linter

## v3.47.3
* Added `table/options.Description.Tiering` field

Expand Down
36 changes: 18 additions & 18 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,76 +30,76 @@ type Config struct {
grpcOptions []grpc.DialOption
credentials credentials.Credentials
tlsConfig *tls.Config
meta meta.Meta
meta *meta.Meta

excludeGRPCCodesForPessimization []grpcCodes.Code
}

func (c Config) Credentials() credentials.Credentials {
func (c *Config) Credentials() credentials.Credentials {
return c.credentials
}

// ExcludeGRPCCodesForPessimization defines grpc codes for exclude its from pessimization trigger
func (c Config) ExcludeGRPCCodesForPessimization() []grpcCodes.Code {
func (c *Config) ExcludeGRPCCodesForPessimization() []grpcCodes.Code {
return c.excludeGRPCCodesForPessimization
}

// GrpcDialOptions reports about used grpc dialing options
func (c Config) GrpcDialOptions() []grpc.DialOption {
func (c *Config) GrpcDialOptions() []grpc.DialOption {
return append(
defaultGrpcOptions(c.trace, c.secure, c.tlsConfig),
c.grpcOptions...,
)
}

// Meta reports meta information about database connection
func (c Config) Meta() meta.Meta {
func (c *Config) Meta() *meta.Meta {
return c.meta
}

// ConnectionTTL defines interval for parking grpc connections.
//
// If ConnectionTTL is zero - connections are not park.
func (c Config) ConnectionTTL() time.Duration {
func (c *Config) ConnectionTTL() time.Duration {
return c.connectionTTL
}

// Secure is a flag for secure connection
func (c Config) Secure() bool {
func (c *Config) Secure() bool {
return c.secure
}

// Endpoint is a required starting endpoint for connect
func (c Config) Endpoint() string {
func (c *Config) Endpoint() string {
return c.endpoint
}

// TLSConfig reports about TLS configuration
func (c Config) TLSConfig() *tls.Config {
func (c *Config) TLSConfig() *tls.Config {
return c.tlsConfig
}

// DialTimeout is the maximum amount of time a dial will wait for a connect to
// complete.
//
// If DialTimeout is zero then no timeout is used.
func (c Config) DialTimeout() time.Duration {
func (c *Config) DialTimeout() time.Duration {
return c.dialTimeout
}

// Database is a required database name.
func (c Config) Database() string {
func (c *Config) Database() string {
return c.database
}

// Trace contains driver tracing options.
func (c Config) Trace() *trace.Driver {
func (c *Config) Trace() *trace.Driver {
return c.trace
}

// Balancer is an optional configuration related to selected balancer.
// That is, some balancing methods allow to be configured.
func (c Config) Balancer() *balancerConfig.Config {
func (c *Config) Balancer() *balancerConfig.Config {
return c.balancerConfig
}

Expand Down Expand Up @@ -149,7 +149,7 @@ func WithTLSConfig(tlsConfig *tls.Config) Option {
}
}

func WithTrace(t trace.Driver, opts ...trace.DriverComposeOption) Option {
func WithTrace(t trace.Driver, opts ...trace.DriverComposeOption) Option { //nolint:gocritic
return func(c *Config) {
c.trace = c.trace.Compose(&t, opts...)
}
Expand Down Expand Up @@ -259,12 +259,12 @@ func ExcludeGRPCCodesForPessimization(codes ...grpcCodes.Code) Option {
}
}

func New(opts ...Option) Config {
func New(opts ...Option) *Config {
c := defaultConfig()

for _, o := range opts {
if o != nil {
o(&c)
o(c)
}
}

Expand All @@ -274,10 +274,10 @@ func New(opts ...Option) Config {
}

// With makes copy of current Config with specified options
func (c Config) With(opts ...Option) Config {
func (c *Config) With(opts ...Option) *Config {
for _, o := range opts {
if o != nil {
o(&c)
o(c)
}
}
c.meta = meta.New(
Expand Down
4 changes: 2 additions & 2 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func defaultTLSConfig() *tls.Config {
}
}

func defaultConfig() (c Config) {
return Config{
func defaultConfig() (c *Config) {
return &Config{
credentials: credentials.NewAnonymousCredentials(
credentials.WithSourceInfo("default"),
),
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type Driver struct { //nolint:maligned

opts []Option

config config.Config
config *config.Config
options []config.Option

discoveryOnce initOnce
Expand Down
2 changes: 1 addition & 1 deletion credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func NewAnonymousCredentials(opts ...option) Credentials {
}

// NewStaticCredentials makes static credentials object
func NewStaticCredentials(user, password string, authEndpoint string, opts ...grpc.DialOption) Credentials {
func NewStaticCredentials(user, password, authEndpoint string, opts ...grpc.DialOption) Credentials {
return credentials.NewStaticCredentials(user, password, authEndpoint, opts...)
}
14 changes: 7 additions & 7 deletions examples/basic/database_sql/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func seriesData(id string, released time.Time, title, info, comment string) type
)
}

func seasonData(seriesID, seasonID string, title string, first, last time.Time) types.Value {
func seasonData(seriesID, seasonID, title string, first, last time.Time) types.Value {
return types.StructValue(
types.StructFieldValue("series_id", types.BytesValueFromString(seriesID)),
types.StructFieldValue("season_id", types.BytesValueFromString(seasonID)),
Expand All @@ -38,7 +38,7 @@ func seasonData(seriesID, seasonID string, title string, first, last time.Time)
)
}

func episodeData(seriesID, seasonID, episodeID string, title string, date time.Time) types.Value {
func episodeData(seriesID, seasonID, episodeID, title string, date time.Time) types.Value {
return types.StructValue(
types.StructFieldValue("series_id", types.BytesValueFromString(seriesID)),
types.StructFieldValue("season_id", types.BytesValueFromString(seasonID)),
Expand All @@ -48,7 +48,7 @@ func episodeData(seriesID, seasonID, episodeID string, title string, date time.T
)
}

func getData() (series []types.Value, seasons []types.Value, episodes []types.Value) {
func getData() (series, seasons, episodes []types.Value) {
for seriesID, fill := range map[string]func(seriesID string) (
seriesData types.Value, seasons []types.Value, episodes []types.Value,
){
Expand All @@ -63,14 +63,14 @@ func getData() (series []types.Value, seasons []types.Value, episodes []types.Va
return
}

func getDataForITCrowd(seriesID string) (series types.Value, seasons []types.Value, episodes []types.Value) {
func getDataForITCrowd(seriesID string) (series types.Value, seasons, episodes []types.Value) {
series = seriesData(
seriesID, date("2006-02-03"), "IT Crowd", ""+
"The IT Crowd is a British sitcom produced by Channel 4, written by Graham Linehan, produced by "+
"Ash Atalla and starring Chris O'Dowd, Richard Ayoade, Katherine Parkinson, and Matt Berry.",
"", // NULL comment.
)
for _, season := range []struct {
for _, season := range []struct { //nolint:gocritic
title string
first time.Time
last time.Time
Expand Down Expand Up @@ -118,14 +118,14 @@ func getDataForITCrowd(seriesID string) (series types.Value, seasons []types.Val
return series, seasons, episodes
}

func getDataForSiliconValley(seriesID string) (series types.Value, seasons []types.Value, episodes []types.Value) {
func getDataForSiliconValley(seriesID string) (series types.Value, seasons, episodes []types.Value) {
series = seriesData(
seriesID, date("2014-04-06"), "Silicon Valley", ""+
"Silicon Valley is an American comedy television series created by Mike Judge, John Altschuler and "+
"Dave Krinsky. The series focuses on five young men who founded a startup company in Silicon Valley.",
"Some comment here",
)
for _, season := range []struct {
for _, season := range []struct { //nolint:gocritic
title string
first time.Time
last time.Time
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/database_sql/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func selectDefault(ctx context.Context, db *sql.DB) (err error) {
if err = row.Scan(&ast, &plan); err != nil {
return err
}
// log.Printf("AST = %s\n\nPlan = %s", ast, plan)
log.Printf("AST = %s\n\nPlan = %s", ast, plan)
return nil
}, retry.WithDoRetryOptions(retry.WithIdempotent(true)))
if err != nil {
Expand Down
Loading

0 comments on commit e7a7718

Please sign in to comment.