From a11269a7c546a76898160d2820f495abcc812976 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Tue, 25 Jun 2024 23:14:10 +0200 Subject: [PATCH] Update http_const.go --- internal/checkers/http_const.go | 44 +++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/internal/checkers/http_const.go b/internal/checkers/http_const.go index 1e6f593..041a730 100644 --- a/internal/checkers/http_const.go +++ b/internal/checkers/http_const.go @@ -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}) } 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) } } 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) } } if len(suggestedFixes) > 0 { @@ -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) + } + 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) + } + return nil +} + func newConstReplacement(bt *ast.BasicLit, currentVal, newVal string) analysis.SuggestedFix { return analysis.SuggestedFix{ Message: fmt.Sprintf("Replace %q with %s", currentVal, newVal),