Skip to content

Commit

Permalink
Change empty string checks to be more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Jan 1, 2024
1 parent 9ba1646 commit 477db14
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 14 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
12 changes: 6 additions & 6 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] == '\\')

Expand Down Expand Up @@ -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 = "."
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2286,7 +2286,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 @@ -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
}

Expand Down Expand Up @@ -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")
}

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 @@ -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
}

Expand Down

0 comments on commit 477db14

Please sign in to comment.