Skip to content

Commit

Permalink
Fix lint: Reportf expects string constant
Browse files Browse the repository at this point in the history
```
printf: non-constant format string in call to (*golang.org/x/tools/go/analysis.Pass).Reportf (govet)
```
  • Loading branch information
stbenjam committed Nov 26, 2024
1 parent f969f7e commit c875790
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
callExpr := node.(*ast.CallExpr)
if p, f, ok := getCallExprFunction(callExpr); ok && p == "fmt" && f == "Sprintf" {
if err := checkForHostPortConstruction(callExpr); err != nil {
pass.Reportf(node.Pos(), err.Error())
pass.Reportf(node.Pos(), "%s", err.Error())
}
}
})
Expand All @@ -52,7 +52,7 @@ func getCallExprFunction(callExpr *ast.CallExpr) (pkg string, fn string, result

// getStringLiteral returns the value at a position if it's a string literal.
func getStringLiteral(args []ast.Expr, pos int) (string, bool) {
if len(args) < pos + 1 {
if len(args) < pos+1 {
return "", false
}

Expand All @@ -72,9 +72,9 @@ func getStringLiteral(args []ast.Expr, pos int) (string, bool) {
// essentially scheme://%s:<something else>, or scheme://user:pass@%s:<something else>.
//
// Matching requirements:
// - Scheme as per RFC3986 is ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
// - A format string substitution in the host portion, preceded by an optional username/password@
// - A colon indicating a port will be specified
// - Scheme as per RFC3986 is ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
// - A format string substitution in the host portion, preceded by an optional username/password@
// - A colon indicating a port will be specified
func checkForHostPortConstruction(sprintf *ast.CallExpr) error {
fs, ok := getStringLiteral(sprintf.Args, 0)
if !ok {
Expand All @@ -93,4 +93,4 @@ func checkForHostPortConstruction(sprintf *ast.CallExpr) error {
}

return nil
}
}

0 comments on commit c875790

Please sign in to comment.