-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nosprintfhostport linter (#2749)
- Loading branch information
Showing
6 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |