Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linter: gocritic #5714

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ linters:
enable:
- bodyclose
- errcheck
- gocritic
- gci
- gofumpt
- gosec
Expand Down
2 changes: 1 addition & 1 deletion caddyconfig/caddyfile/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (a Adapter) Adapt(body []byte, options map[string]any) ([]byte, []caddyconf
// TODO: also perform this check on imported files
func FormattingDifference(filename string, body []byte) (caddyconfig.Warning, bool) {
// replace windows-style newlines to normalize comparison
normalizedBody := bytes.Replace(body, []byte("\r\n"), []byte("\n"), -1)
normalizedBody := bytes.ReplaceAll(body, []byte("\r\n"), []byte("\n"))

formatted := Format(normalizedBody)
if bytes.Equal(formatted, normalizedBody) {
Expand Down
7 changes: 3 additions & 4 deletions caddyconfig/caddyfile/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ func (l *lexer) next() (bool, error) {
val = append(val, '\\')
}
escaped = false
} else {
if (quoted && ch == '"') || (btQuoted && ch == '`') {
return makeToken(ch), nil
}
} else if (quoted && ch == '"') || (btQuoted && ch == '`') {
return makeToken(ch), nil

}
// allow quoted text to wrap continue on multiple lines
if ch == '\n' {
Expand Down
39 changes: 25 additions & 14 deletions caddyconfig/caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@

// splice the imported tokens in the place of the import statement
// and rewind cursor so Next() will land on first imported token
p.tokens = append(tokensBefore, append(tokensCopy, tokensAfter...)...)

Check failure on line 512 in caddyconfig/caddyfile/parse.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

appendAssign: append result not assigned to the same slice (gocritic)
p.cursor -= len(args) + 1

return nil
Expand Down Expand Up @@ -573,33 +573,44 @@
segment = append(segment, p.Token())

for p.Next() {
if p.Val() == "{" {
switch p.Val() {
case "{":
p.nesting++
if !p.isNextOnNewLine() && p.Token().wasQuoted == 0 {
return p.Err("Unexpected next token after '{' on same line")
}
if p.isNewLine() {
return p.Err("Unexpected '{' on a new line; did you mean to place the '{' on the previous line?")
}
} else if p.Val() == "{}" {
case "{}":
if p.isNextOnNewLine() && p.Token().wasQuoted == 0 {
return p.Err("Unexpected '{}' at end of line")
}
} else if p.isNewLine() && p.nesting == 0 {
case "import":
if p.isNewLine() {
if err := p.doImport(1); err != nil {
return err
}
p.cursor-- // cursor is advanced when we continue, so roll back one more
continue
}
case "}":
if p.nesting > 0 {
p.nesting--
} else {
return p.Err("Unexpected '}' because no matching opening brace")
}
default:
if p.isNewLine() && p.nesting == 0 {
p.cursor-- // read too far
break
}
segment = append(segment, p.Token())
}
if p.isNewLine() && p.nesting == 0 {
p.cursor-- // read too far
break
} else if p.Val() == "}" && p.nesting > 0 {
p.nesting--
} else if p.Val() == "}" && p.nesting == 0 {
return p.Err("Unexpected '}' because no matching opening brace")
} else if p.Val() == "import" && p.isNewLine() {
if err := p.doImport(1); err != nil {
return err
}
p.cursor-- // cursor is advanced when we continue, so roll back one more
continue
}

segment = append(segment, p.Token())
}

Expand Down
12 changes: 5 additions & 7 deletions caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,16 +837,14 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
return nil, h.Err("duplicate global log option for: " + logName)
}
globalLogNames[logName] = struct{}{}
} else {
// An optional override of the logger name can be provided;
// otherwise a default will be used, like "log0", "log1", etc.
if h.NextArg() {
logName = h.Val()
} else if h.NextArg() {
logName = h.Val()

// Only a single argument is supported.
if h.NextArg() {
return nil, h.ArgErr()
}
// Only a single argument is supported.
if h.NextArg() {
return nil, h.ArgErr()
}
}

Expand Down
9 changes: 2 additions & 7 deletions modules/caddyhttp/celmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,8 @@ func isCELStringExpr(e *exprpb.Expr) bool {

// isCELStringLiteral returns whether the expression is a CEL string literal.
func isCELStringLiteral(e *exprpb.Expr) bool {
switch e.GetExprKind().(type) {
case *exprpb.Expr_ConstExpr:
constant := e.GetConstExpr()
switch constant.GetConstantKind().(type) {
case *exprpb.Constant_StringValue:
return true
}
if _, ok := e.GetConstExpr().GetConstantKind().(*exprpb.Constant_StringValue); ok {
return true
}
return false
}
Expand Down
6 changes: 5 additions & 1 deletion modules/caddyhttp/reverseproxy/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,11 @@ func (h *Handler) FinalizeUnmarshalCaddyfile(helper httpcaddyfile.Helper) error
withMatchers = append(withMatchers, hr)
}
}
h.HandleResponse = append(withMatchers, withoutMatchers...)
h.HandleResponse = make([]caddyhttp.ResponseHandler, 0, len(h.HandleResponse))
h.HandleResponse = append(h.HandleResponse, withMatchers...)
h.HandleResponse = append(h.HandleResponse, withoutMatchers...)
h.HandleResponse = append(h.HandleResponse, withMatchers...)
h.HandleResponse = append(h.HandleResponse, withoutMatchers...)

// clean up the bits we only needed for adapting
h.handleResponseSegments = nil
Expand Down
2 changes: 1 addition & 1 deletion modules/caddytls/connpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ type ClientAuthentication struct {
// these CA certificates will be rejected.
TrustedCACertPEMFiles []string `json:"trusted_ca_certs_pem_files,omitempty"`

// DEPRECATED: This field is deprecated and will be removed in
// Deprecated: This field is deprecated and will be removed in
// a future version. Please use the `validators` field instead
// with the tls.client_auth.leaf module instead.
//
Expand Down
Loading