Skip to content

Commit

Permalink
try out nakedret
Browse files Browse the repository at this point in the history
  • Loading branch information
katbyte committed Nov 9, 2024
1 parent fab6d73 commit c80730e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 62 deletions.
8 changes: 5 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ linters:
- govet # reports suspicious constructs. It is roughly the same as 'go vet' (replaced vet and vetshadow)
- ineffassign # detects when assignments to existing variables are not used
- misspell # finds commonly misspelled English words.
#- nakedret # Checks that functions with naked returns are not longer than a maximum size
#- nilerr # Finds the code that returns nil even if it checks that the error is not nil.
#- nlreturn # Nlreturn checks for a new line before return and branch statements to increase code clarity.
#- paralleltest # detects missing usage of t.Parallel() method in your Go test.
Expand All @@ -48,14 +47,17 @@ linters:
- unparam # reports unused function parameters.
- wastedassign # finds wasted assignment statements
- whitespace # checks for unnecessary newlines at the start and end of functions, if, for, etc. (
#- wsl Add or remove empty lines.
#- wsl # add or remove empty lines.

###### DISABLED because : integer overflow conversion int -> int32
# - gosec # Gosec is a security linter for Go source code

##### DISABLED as i was fixing them with other linters, and not realizing how many there are - disabling for now even thou it is valid
#- perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative.

#### VALID but DISABLED as we have a lot of these in the codebase to switch over
#- nakedret # Checks that functions with naked returns are not longer than a maximum size

#### DISABLED TO DO IN ITS OWN PR - should be enabled and done #####
#- tenv #detects using os.Setenv instead of t.Setenv since Go1.17.

Expand All @@ -80,7 +82,7 @@ linters-settings:
- hdinsight
- exportfs
nakedret:
max-func-lines: 40
max-func-lines: 30
tagalign:
sort: true
order:
Expand Down
20 changes: 7 additions & 13 deletions internal/services/datafactory/azuresdkhacks/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,18 @@ func (client PipelinesClient) CreateOrUpdate(ctx context.Context, resourceGroupN

req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, factoryName, pipelineName, pipeline, ifMatch)
if err != nil {
err = autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "CreateOrUpdate", nil, "Failure preparing request")
return
return result, autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "CreateOrUpdate", nil, "Failure preparing request")
}

resp, err := client.OriginalClient.CreateOrUpdateSender(req)
if err != nil {
result.Response = autorest.Response{Response: resp}
err = autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "CreateOrUpdate", resp, "Failure sending request")
return
return result, autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "CreateOrUpdate", resp, "Failure sending request")
}

result, err = client.CreateOrUpdateResponder(resp)
if err != nil {
err = autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "CreateOrUpdate", resp, "Failure responding to request")
return
return result, autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "CreateOrUpdate", resp, "Failure responding to request")
}

return
Expand Down Expand Up @@ -157,24 +154,21 @@ func (client PipelinesClient) Get(ctx context.Context, resourceGroupName string,

req, err := client.GetPreparer(ctx, resourceGroupName, factoryName, pipelineName, ifNoneMatch)
if err != nil {
err = autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "Get", nil, "Failure preparing request")
return
return result, autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "Get", nil, "Failure preparing request")
}

resp, err := client.OriginalClient.GetSender(req)
if err != nil {
result.Response = autorest.Response{Response: resp}
err = autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "Get", resp, "Failure sending request")
return
return result, autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "Get", resp, "Failure sending request")
}

result, err = client.GetResponder(resp)
if err != nil {
err = autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "Get", resp, "Failure responding to request")
return
return result, autorest.NewErrorWithError(err, "datafactory.PipelinesClient", "Get", resp, "Failure responding to request")
}

return
return result, err
}

func (client PipelinesClient) GetPreparer(ctx context.Context, resourceGroupName string, factoryName string, pipelineName string, ifNoneMatch string) (*http.Request, error) {
Expand Down
57 changes: 20 additions & 37 deletions internal/services/oracle/validate/autonomous_database_regular.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,35 @@ import (
func AutonomousDatabaseName(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
return []string{}, append(errors, fmt.Errorf("expected type of %s to be string", k))
}

firstChar, _ := utf8.DecodeRuneInString(v)
if !unicode.IsLetter(firstChar) {
errors = append(errors, fmt.Errorf("%v must start with a letter", k))
return
return []string{}, append(errors, fmt.Errorf("%v must start with a letter", k))
}

for _, r := range v {
if !unicode.IsLetter(r) && !unicode.IsNumber(r) {
errors = append(errors, fmt.Errorf("%v must contain only letters and numbers", k))
return
return []string{}, append(errors, fmt.Errorf("%v must contain only letters and numbers", k))
}
}

if len(v) > 30 {
errors = append(errors, fmt.Errorf("%v must be 30 characers max", k))
return
return []string{}, append(errors, fmt.Errorf("%v must be 30 characers max", k))
}

return
return []string{}, []error{}
}

func AutonomousDatabasePassword(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
return []string{}, append(errors, fmt.Errorf("expected type of %s to be string", k))
}

if len(v) < 12 || len(v) > 30 {
errors = append(errors, fmt.Errorf("%v must be 12 to 30 characters", k))
return
return []string{}, append(errors, fmt.Errorf("%v must be 12 to 30 characters", k))
}

hasUpper := false
Expand All @@ -72,57 +66,46 @@ func AutonomousDatabasePassword(i interface{}, k string) (warnings []string, err
}
}
if hasDoubleQuote {
errors = append(errors, fmt.Errorf("%v must not contain the double quote (\") character", k))
return
return []string{}, append(errors, fmt.Errorf("%v must not contain the double quote (\") character", k))
}
if !hasUpper {
errors = append(errors, fmt.Errorf("%v must contain at least one uppercase letter", k))
return
return []string{}, append(errors, fmt.Errorf("%v must contain at least one uppercase letter", k))
}
if !hasLower {
errors = append(errors, fmt.Errorf("%v must contain at least one lowercase letter", k))
return
return []string{}, append(errors, fmt.Errorf("%v must contain at least one lowercase letter", k))
}
if !hasNumber {
errors = append(errors, fmt.Errorf("%v must contain at least one number", k))
return
return []string{}, append(errors, fmt.Errorf("%v must contain at least one number", k))
}
if strings.Contains(v, "admin") {
errors = append(errors, fmt.Errorf("%v must not contain the username \"admin\"", k))
return
return []string{}, append(errors, fmt.Errorf("%v must not contain the username \"admin\"", k))
}

return
return []string{}, []error{}
}

func CustomerContactEmail(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
return []string{}, append(errors, fmt.Errorf("expected type of %s to be string", k))
}

_, err := mail.ParseAddress(v)
if err != nil {
errors = append(errors, fmt.Errorf("%v must be a valid email address", k))
return
if _, err := mail.ParseAddress(v); err != nil {
return []string{}, append(errors, fmt.Errorf("%v must be a valid email address", k))
}

return warnings, errors
return []string{}, []error{}
}

func AdbsComputeModel(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
return []string{}, append(errors, fmt.Errorf("expected type of %s to be string", k))
}

if v != string(autonomousdatabases.ComputeModelECPU) && v != string(autonomousdatabases.ComputeModelOCPU) {
errors = append(errors, fmt.Errorf("%v must be %v or %v", k,
string(autonomousdatabases.ComputeModelECPU), string(autonomousdatabases.ComputeModelOCPU)))
return
return []string{}, append(errors, fmt.Errorf("%v must be %v or %v", k, string(autonomousdatabases.ComputeModelECPU), string(autonomousdatabases.ComputeModelOCPU)))
}

return
return []string{}, []error{}
}
12 changes: 6 additions & 6 deletions internal/tools/document-lint/check/crossproperty.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func shouldSkipDocProp(rt, path string) bool {
func diffDocMiss(rt, path string, s *schema2.Schema, f *model.Field) (res []Checker) {
// skip deprecated property
if shouldSkipDocProp(rt, path) {
return
return res
}

if isSkipProp(rt, path) {
return
return res
}

if f == nil {
Expand All @@ -93,7 +93,7 @@ func diffDocMiss(rt, path string, s *schema2.Schema, f *model.Field) (res []Chec
case *schema2.Resource:
if f.Subs == nil {
res = append(res, newMissBlockDeclare(path, f))
return
return res
}
for key, val := range ele.Schema {
subField := f.Subs[key]
Expand All @@ -120,10 +120,10 @@ func shouldSkipCodeProp(rt, path string) bool {

func diffCodeMiss(rt, path string, f *model.Field, s *schema2.Schema) (res []Checker) {
if shouldSkipCodeProp(rt, path) {
return
return res
}
if isSkipProp(rt, path) {
return
return res
}

if f != nil && f.FormatErr != "" {
Expand All @@ -139,7 +139,7 @@ func diffCodeMiss(rt, path string, f *model.Field, s *schema2.Schema) (res []Che
} else {
res = append(res, newFormatErr(f.Content, f.FormatErr, newCheckBase(f.Line, path, f)))
}
return
return res
}

if s == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/tools/document-lint/check/document_fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func tryFixTimeouts(rt string, lines []string, diffs []TimeoutDiffItem) []string
func (f *Fixer) TryFix() (err error) {
// read file as bytes
if len(f.Diff) == 0 {
return
return err
}
if d, ok := f.Diff[0].(diffWithMessage); ok {
log.Printf("%s: %s", f.ResourceType, d.msg)
return
return err
}
content, err := os.ReadFile(f.MDFile)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/tools/document-lint/md/resource_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func extractFieldFromLine(line string) (field *model.Field) {
if len(res) <= 1 || res[1] == "" {
field.Name = util.FirstCodeValue(line) // try to use the first code as name
field.FormatErr = "no field name found"
return
return field
}
field.Name = res[1]
if field.Name == "" {
Expand Down

0 comments on commit c80730e

Please sign in to comment.