From 28615eba5594af29737938ae5cece50d7cba0a17 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Thu, 4 Jan 2024 16:05:38 +0200 Subject: Change empty string checks to be more idiomatic (#1684) --- .golangci.yml | 3 +++ client.go | 2 +- expvarhandler/expvar.go | 2 +- fs.go | 12 ++++++------ header_test.go | 2 +- http.go | 4 ++-- http_test.go | 4 ++-- server.go | 2 +- 8 files changed, 17 insertions(+), 14 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d768d90..6d91345 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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. diff --git a/client.go b/client.go index 122894b..b5493e9 100644 --- a/client.go +++ b/client.go @@ -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 diff --git a/expvarhandler/expvar.go b/expvarhandler/expvar.go index 796954d..b750261 100644 --- a/expvarhandler/expvar.go +++ b/expvarhandler/expvar.go @@ -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) diff --git a/fs.go b/fs.go index 4a8e0f2..f35f22c 100644 --- a/fs.go +++ b/fs.go @@ -100,7 +100,7 @@ func ServeFile(ctx *RequestCtx, path string) { rootFSHandler = rootFS.NewRequestHandler() }) - if len(path) == 0 || !filepath.IsAbs(path) { + if path == "" || !filepath.IsAbs(path) { // extend relative path to absolute path hasTrailingSlash := len(path) > 0 && (path[len(path)-1] == '/' || path[len(path)-1] == '\\') @@ -429,7 +429,7 @@ func (fs *FS) normalizeRoot(root string) string { // fs.FS uses relative paths, that paths are slash-separated on all systems, even Windows. if fs.FS == nil { // Serve files from the current working directory if Root is empty or if Root is a relative path. - if (!fs.AllowEmptyRoot && len(root) == 0) || (len(root) > 0 && !filepath.IsAbs(root)) { + if (!fs.AllowEmptyRoot && root == "") || (root != "" && !filepath.IsAbs(root)) { path, err := os.Getwd() if err != nil { path = "." @@ -452,14 +452,14 @@ func (fs *FS) initRequestHandler() { root := fs.normalizeRoot(fs.Root) compressRoot := fs.CompressRoot - if len(compressRoot) == 0 { + if compressRoot == "" { compressRoot = root } else { compressRoot = fs.normalizeRoot(compressRoot) } compressedFileSuffixes := fs.CompressedFileSuffixes - if len(compressedFileSuffixes["br"]) == 0 || len(compressedFileSuffixes["gzip"]) == 0 || + if compressedFileSuffixes["br"] == "" || compressedFileSuffixes["gzip"] == "" || compressedFileSuffixes["br"] == compressedFileSuffixes["gzip"] { // Copy global map compressedFileSuffixes = make(map[string]string, len(FSCompressedFileSuffixes)) @@ -1474,7 +1474,7 @@ func (h *fsHandler) newCompressedFSFileCache(f fs.File, fileInfo fs.FileInfo, fi ext := fileExtension(fileInfo.Name(), false, h.compressedFileSuffixes[fileEncoding]) contentType := mime.TypeByExtension(ext) - if len(contentType) == 0 { + if contentType == "" { data, err := readFileHeader(f, false, fileEncoding) if err != nil { return nil, fmt.Errorf("cannot read header of the file %q: %w", fileInfo.Name(), err) @@ -1573,7 +1573,7 @@ func (h *fsHandler) newFSFile(f fs.File, fileInfo fs.FileInfo, compressed bool, // detect content-type ext := fileExtension(fileInfo.Name(), compressed, h.compressedFileSuffixes[fileEncoding]) contentType := mime.TypeByExtension(ext) - if len(contentType) == 0 { + if contentType == "" { data, err := readFileHeader(f, compressed, fileEncoding) if err != nil { return nil, fmt.Errorf("cannot read header of the file %q: %w", fileInfo.Name(), err) diff --git a/header_test.go b/header_test.go index eaa9a50..83b8a9c 100644 --- a/header_test.go +++ b/header_test.go @@ -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 } diff --git a/http.go b/http.go index 4785d12..5672301 100644 --- a/http.go +++ b/http.go @@ -958,7 +958,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 } @@ -1014,7 +1014,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") } diff --git a/http_test.go b/http_test.go index 7b526cb..098990a 100644 --- a/http_test.go +++ b/http_test.go @@ -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 { @@ -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) } } diff --git a/server.go b/server.go index 95eb2c0..dbee2a4 100644 --- a/server.go +++ b/server.go @@ -1736,7 +1736,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 } -- cgit v1.2.3