Skip to content

Commit

Permalink
Update http_const.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorel-35 authored Jun 25, 2024
1 parent 01a770e commit a11269a
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions internal/checkers/http_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,34 @@ func (checker HTTPConst) Check(_ *analysis.Pass, call *CallMeta) *analysis.Diagn
switch call.Fn.NameFTrimmed {
case "HTTPBody",
"HTTPBodyContains",
"HTTPBodyContainsf",
"HTTPBodyNotContains",
"HTTPBodyNotContainsf",
"HTTPError",
"HTTPErrorf",
"HTTPRedirect",
"HTTPSuccess",
"HTTPSuccessf":
"HTTPSuccess":

if len(call.Args) < 2 {
return nil
}

if bt, ok := safeTypedBasicLit(call.Args[1], token.STRING); ok {
currentVal := unquoteBasicLitValue(bt)
key := strings.ToUpper(currentVal)
if newVal, ok := httpMethod[key]; ok {
return newHTTPConstDiagnostic(checker.Name(), call, []analysis.SuggestedFix{newConstReplacement(bt, currentVal, newVal)})
if suggestedFix := newHTTPMethodReplacement(bt); suggestedFix != nil {
return newHTTPConstDiagnostic(checker.Name(), call, []analysis.SuggestedFix{suggestedFix})

Check failure on line 42 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / test

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in array or slice literal

Check failure on line 42 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in array or slice literal

Check failure on line 42 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in array or slice literal
}
return nil
}
case "HTTPStatusCode", "HTTPStatusCodef":
case "HTTPStatusCode":
if len(call.Args) < 5 {
return nil
}
var suggestedFixes []analysis.SuggestedFix
if bt, ok := safeTypedBasicLit(call.Args[1], token.STRING); ok {
currentVal := unquoteBasicLitValue(bt)
key := strings.ToUpper(currentVal)
if newVal, ok := httpMethod[key]; ok {
suggestedFixes = append(suggestedFixes, newConstReplacement(bt, currentVal, newVal))
if suggestedFix := newHTTPMethodReplacement(bt); suggestedFix != nil {
suggestedFixes = append(suggestedFixes, suggestedFix)

Check failure on line 53 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / test

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in argument to append

Check failure on line 53 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in argument to append

Check failure on line 53 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in argument to append
}
}
if bt, ok := safeTypedBasicLit(call.Args[4], token.INT); ok {
currentVal := bt.Value
key := strings.ToUpper(currentVal)
if newVal, ok := httpStatusCode[key]; ok {
suggestedFixes = append(suggestedFixes, newConstReplacement(bt, currentVal, newVal))
if suggestedFix := newHTTPStatusCodeReplacement(bt); suggestedFix != nil {
suggestedFixes = append(suggestedFixes, suggestedFix)

Check failure on line 58 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / test

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in argument to append

Check failure on line 58 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in argument to append

Check failure on line 58 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

cannot use suggestedFix (variable of type *analysis.SuggestedFix) as analysis.SuggestedFix value in argument to append
}
}
if len(suggestedFixes) > 0 {
Expand All @@ -75,6 +65,24 @@ func (checker HTTPConst) Check(_ *analysis.Pass, call *CallMeta) *analysis.Diagn
return nil
}

func newHTTPMethodReplacement(bt *ast.BasicLit) *analysis.SuggestedFix {
currentVal := unquoteBasicLitValue(bt)
key := strings.ToUpper(currentVal)
if newVal, ok := httpMethod[key]; ok {
return &newConstReplacement(bt, currentVal, newVal)

Check failure on line 72 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / test

invalid operation: cannot take address of newConstReplacement(bt, currentVal, newVal) (value of type analysis.SuggestedFix)

Check failure on line 72 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: cannot take address of newConstReplacement(bt, currentVal, newVal) (value of type analysis.SuggestedFix)

Check failure on line 72 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: cannot take address of newConstReplacement(bt, currentVal, newVal) (value of type analysis.SuggestedFix)
}
return nil
}

func newHTTPStatusCodeReplacement(bt *ast.BasicLit) *analysis.SuggestedFix {
currentVal := bt.Value
key := strings.ToUpper(currentVal)
if newVal, ok := httpStatusCode[key]; ok {
return &newConstReplacement(bt, currentVal, newVal)

Check failure on line 81 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / test

invalid operation: cannot take address of newConstReplacement(bt, currentVal, newVal) (value of type analysis.SuggestedFix)

Check failure on line 81 in internal/checkers/http_const.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: cannot take address of newConstReplacement(bt, currentVal, newVal) (value of type analysis.SuggestedFix) (typecheck)
}
return nil
}

func newConstReplacement(bt *ast.BasicLit, currentVal, newVal string) analysis.SuggestedFix {
return analysis.SuggestedFix{
Message: fmt.Sprintf("Replace %q with %s", currentVal, newVal),
Expand Down

0 comments on commit a11269a

Please sign in to comment.