Skip to content

Commit

Permalink
Change empty string checks to be more idiomatic (valyala#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-Cheng committed Feb 11, 2024
1 parent bbeaa88 commit b9b7a05
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ linters-settings:
"all",
"-ST1000", # at least one file in a package should have a package comment
]
gocritic:
enabled-checks:
- emptyStringTest

issues:
# Show all issues from a linter.
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ func newClientTLSConfig(c *tls.Config, addr string) *tls.Config {
c = c.Clone()
}

if len(c.ServerName) == 0 {
if c.ServerName == "" {
serverName := tlsServerName(addr)
if serverName == "*" {
c.InsecureSkipVerify = true
Expand Down
2 changes: 1 addition & 1 deletion expvarhandler/expvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ExpvarHandler(ctx *fasthttp.RequestCtx) {

func getExpvarRegexp(ctx *fasthttp.RequestCtx) (*regexp.Regexp, error) {
r := string(ctx.QueryArgs().Peek("r"))
if len(r) == 0 {
if r == "" {
return defaultRE, nil
}
rr, err := regexp.Compile(r)
Expand Down
4 changes: 2 additions & 2 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"github.com/klauspost/compress/zstd"
"html"
"io"
"io/fs"
Expand All @@ -17,6 +16,7 @@ import (
"sync"
"time"

"github.com/klauspost/compress/zstd"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/gzip"
"github.com/valyala/bytebufferpool"
Expand Down Expand Up @@ -461,7 +461,7 @@ func (fs *FS) initRequestHandler() {
}

compressedFileSuffixes := fs.CompressedFileSuffixes
if len(compressedFileSuffixes["br"]) == 0 || len(compressedFileSuffixes["gzip"]) == 0 ||
if compressedFileSuffixes["br"] == "" || compressedFileSuffixes["gzip"] == "" ||
len(compressedFileSuffixes["zstd"]) == 0 || compressedFileSuffixes["br"] == compressedFileSuffixes["gzip"] ||
compressedFileSuffixes["br"] == compressedFileSuffixes["zstd"] ||
compressedFileSuffixes["gzip"] == compressedFileSuffixes["zstd"] {
Expand Down
2 changes: 1 addition & 1 deletion header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ type bufioPeekReader struct {
}

func (r *bufioPeekReader) Read(b []byte) (int, error) {
if len(r.s) == 0 {
if r.s == "" {
return 0, io.EOF
}

Expand Down
4 changes: 2 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ func (req *Request) MultipartForm() (*multipart.Form, error) {
}

req.multipartFormBoundary = string(req.Header.MultipartFormBoundary())
if len(req.multipartFormBoundary) == 0 {
if req.multipartFormBoundary == "" {
return nil, ErrNoMultipartForm
}

Expand Down Expand Up @@ -1035,7 +1035,7 @@ func marshalMultipartForm(f *multipart.Form, boundary string) ([]byte, error) {
func WriteMultipartForm(w io.Writer, f *multipart.Form, boundary string) error {
// Do not care about memory allocations here, since multipart
// form processing is slow.
if len(boundary) == 0 {
if boundary == "" {
return errors.New("form boundary cannot be empty")
}

Expand Down
4 changes: 2 additions & 2 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ func testRequestSuccess(t *testing.T, method, requestURI, host, userAgent, body,
if string(req1.Header.Method()) != expectedMethod {
t.Fatalf("Unexpected method: %q. Expected %q", req1.Header.Method(), expectedMethod)
}
if len(requestURI) == 0 {
if requestURI == "" {
requestURI = "/"
}
if string(req1.Header.RequestURI()) != requestURI {
Expand Down Expand Up @@ -2467,7 +2467,7 @@ func testRequestPostArgsError(t *testing.T, req *Request, s string) {
t.Fatalf("Unexpected error when reading %q: %v", s, err)
}
ss := req.PostArgs().String()
if len(ss) != 0 {
if ss != "" {
t.Fatalf("unexpected post args: %q. Expecting empty post args", ss)
}
}
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ func (s *Server) ServeTLSEmbed(ln net.Listener, certData, keyData []byte) error
// This function allows programmer to handle multiple domains
// in one server structure. See examples/multidomain.
func (s *Server) AppendCert(certFile, keyFile string) error {
if len(certFile) == 0 && len(keyFile) == 0 {
if certFile == "" && keyFile == "" {
return errNoCertOrKeyProvided
}

Expand Down

0 comments on commit b9b7a05

Please sign in to comment.