Skip to content

Commit

Permalink
Add nosprintfhostport linter (#2749)
Browse files Browse the repository at this point in the history
  • Loading branch information
stbenjam authored Apr 23, 2022
1 parent f79bc88 commit 89e6cd6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,7 @@ linters:
- nlreturn
- noctx
- nolintlint
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
Expand Down Expand Up @@ -1769,6 +1770,7 @@ linters:
- nlreturn
- noctx
- nolintlint
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.11.0
github.com/ssgreg/nlreturn/v2 v2.2.1
github.com/stbenjam/no-sprintf-host-port v0.1.0
github.com/stretchr/testify v1.7.1
github.com/sylvia7788/contextcheck v1.0.4
github.com/tdakkota/asciicheck v0.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/golinters/nosprintfhostport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/stbenjam/no-sprintf-host-port/pkg/analyzer"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewNoSprintfHostPort() *goanalysis.Linter {
a := analyzer.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/firefart/nonamedreturns"),

linter.NewConfig(golinters.NewNoSprintfHostPort()).
WithSince("v1.46.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/stbenjam/no-sprintf-host-port"),

linter.NewConfig(golinters.NewParallelTest()).
WithSince("v1.33.0").
WithPresets(linter.PresetStyle, linter.PresetTest).
Expand Down
47 changes: 47 additions & 0 deletions test/testdata/nosprintfhostport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//args: -Enosprintfhostport
package testdata

import (
"fmt"
"net"
)

func _() {

_ = fmt.Sprintf("postgres://%s:%[email protected]/%s", "foo", "bar", "baz")

_ = fmt.Sprintf("http://api.%s/foo", "example.com")

_ = fmt.Sprintf("http://api.%s:6443/foo", "example.com")

_ = fmt.Sprintf("http://%s/foo", net.JoinHostPort("foo", "80"))

_ = fmt.Sprintf("9invalidscheme://%s:%d", "myHost", 70)

_ = fmt.Sprintf("gopher://%s/foo", net.JoinHostPort("foo", "80"))

_ = fmt.Sprintf("telnet+ssl://%s/foo", net.JoinHostPort("foo", "80"))

_ = fmt.Sprintf("http://%s/foo:bar", net.JoinHostPort("foo", "80"))

_ = fmt.Sprintf("http://user:password@%s/foo:bar", net.JoinHostPort("foo", "80"))

_ = fmt.Sprintf("http://example.com:9211")

_ = fmt.Sprintf("gopher://%s:%d", "myHost", 70) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"

_ = fmt.Sprintf("telnet+ssl://%s:%d", "myHost", 23) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"

_ = fmt.Sprintf("weird3.6://%s:%d", "myHost", 23) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"

_ = fmt.Sprintf("https://user@%s:%d", "myHost", 8443) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"

_ = fmt.Sprintf("postgres://%s:%s@%s:5050/%s", "foo", "bar", "baz", "qux") // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"

_ = fmt.Sprintf("https://%s:%d", "myHost", 8443) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"

_ = fmt.Sprintf("https://%s:9211", "myHost") // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"

ip := "fd00::1"
_ = fmt.Sprintf("http://%s:1936/healthz", ip) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"
}

0 comments on commit 89e6cd6

Please sign in to comment.