Skip to content

Commit

Permalink
refactor: simplify by using switch instead of if
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Dec 12, 2024
1 parent d64bddc commit 414348b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
18 changes: 8 additions & 10 deletions rule/enforce_repeated_arg_type_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.
ast.Inspect(astFile, func(n ast.Node) bool {
switch fn := n.(type) {
case *ast.FuncDecl:
if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeFull {
switch r.funcArgStyle {
case enforceRepeatedArgTypeStyleTypeFull:
if fn.Type.Params != nil {
for _, field := range fn.Type.Params.List {
if len(field.Names) > 1 {
Expand All @@ -133,11 +134,9 @@ func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.
}
}
}
}

if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeShort {
var prevType ast.Expr
case enforceRepeatedArgTypeStyleTypeShort:
if fn.Type.Params != nil {
var prevType ast.Expr
for _, field := range fn.Type.Params.List {
prevTypeStr := gofmt(prevType)
currentTypeStr := gofmt(field.Type)
Expand All @@ -154,7 +153,8 @@ func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.
}
}

if r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeFull {
switch r.funcRetValStyle {
case enforceRepeatedArgTypeStyleTypeFull:
if fn.Type.Results != nil {
for _, field := range fn.Type.Results.List {
if len(field.Names) > 1 {
Expand All @@ -167,11 +167,9 @@ func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.
}
}
}
}

if r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeShort {
var prevType ast.Expr
case enforceRepeatedArgTypeStyleTypeShort:
if fn.Type.Results != nil {
var prevType ast.Expr
for _, field := range fn.Type.Results.List {
prevTypeStr := gofmt(prevType)
currentTypeStr := gofmt(field.Type)
Expand Down
10 changes: 3 additions & 7 deletions rule/struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,9 @@ func (w lintStructTagRule) checkTagNameIfNeed(tag *structtag.Tag) (string, bool)
return "", true
}

needsToCheckTagName := tag.Key == keyBSON ||
tag.Key == keyJSON ||
tag.Key == keyXML ||
tag.Key == keyYAML ||
tag.Key == keyProtobuf

if !needsToCheckTagName {
switch tag.Key {
case keyBSON, keyJSON, keyXML, keyYAML, keyProtobuf:
default:
return "", true
}

Expand Down

0 comments on commit 414348b

Please sign in to comment.