Skip to content

Commit

Permalink
Add gocritic linter, fix a bunch of stuff
Browse files Browse the repository at this point in the history
Brace yourselves! For there are many touched files here. No changes
in semantics however.

Spent a long time trying out the various optional rules gocritic
provides, and settled for a few of them. There are more I really
like, but that would take many hours to address across the codebase.

Perhaps others find gocritic too pedantic? If so, we can merge the
fixes without enabling the rule.

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Feb 23, 2025
1 parent 16762d7 commit 095308d
Show file tree
Hide file tree
Showing 118 changed files with 389 additions and 514 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.23.6
1.24.0
36 changes: 36 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ run:

issues:
max-same-issues: 0 # don't hide issues in CI runs because they are the same type
exclude-dirs:
- internal/gojsonschema
- internal/gqlparser
- internal/jwx
exclude-rules:
- path: ast/
linters:
Expand Down Expand Up @@ -140,6 +144,37 @@ issues:
linters-settings:
lll:
line-length: 200
gocritic:
disabled-checks:
- appendAssign
# NOTE(ae): this one should be enabled, but there were too
# many violations to fix in one go... revisit later
- singleCaseSwitch
# Reasonable rule, but not sure what to replace with in
# many locations, so disabling for now
- exitAfterDefer
# The following 3 rules are disabled from the perfomance tag
# enabled further down. The first two are reasonable, but not
# super important. appendCombine is really nice though! And
# should be enabled. Just many places to fix..
- hugeParam
- preferFprint
- appendCombine
enabled-checks:
# NOTE that these are rules enabled in addition to the default set
- filepathJoin
- dupImport
- redundantSprint
- stringConcatSimplify
enabled-tags:
- performance
settings:
ifElseChain:
# ridiculous value set for now, but this should be
# lowered to something more reasonable, as the rule
# is reasonable (replace long if-else chains with
# switch)... just too many violations right now
minThreshold: 10
govet:
enable:
- deepequalerrors
Expand Down Expand Up @@ -175,4 +210,5 @@ linters:
- unconvert
- copyloopvar
- perfsprint
- gocritic
# - gosec # too many false positives
6 changes: 2 additions & 4 deletions ast/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ func TestCompile_DefaultRegoVersion(t *testing.T) {

if len(tc.expErrs) > 0 {
assertErrors(t, compiler.Errors, tc.expErrs)
} else {
if len(compiler.Errors) > 0 {
t.Fatalf("Unexpected errors: %v", compiler.Errors)
}
} else if len(compiler.Errors) > 0 {
t.Fatalf("Unexpected errors: %v", compiler.Errors)
}
})
}
Expand Down
12 changes: 4 additions & 8 deletions ast/compilehelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ func TestCompileModules_DefaultRegoVersion(t *testing.T) {
t.Fatalf("Expected error to contain:\n\n%s\n\nbut got:\n\n%s", expErr, err)
}
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
} else if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
}
Expand Down Expand Up @@ -216,10 +214,8 @@ func TestCompileModulesWithOpt_DefaultRegoVersion(t *testing.T) {
t.Fatalf("Expected error to contain:\n\n%s\n\nbut got:\n\n%s", expErr, err)
}
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
} else if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestParser_DefaultRegoVersion(t *testing.T) {
p[x] {
c = ["a", "b", "c"][i]
}`,
expStmtCount: 2, //package, p
expStmtCount: 2, // package, p
},
{
note: "v1",
Expand All @@ -30,7 +30,7 @@ p contains x if {
c = ["a", "b", "c"][i]
}`,
// v1 Keywords are not recognized, and interpreted as individual statements
expStmtCount: 5, //package, p, contains, x, if
expStmtCount: 5, // package, p, contains, x, if
},
}

Expand Down
2 changes: 1 addition & 1 deletion build/generate-cli-docs/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {

err = doc.GenMarkdownTree(command, dir)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint: gocritic
}

files, err := os.ReadDir(dir)
Expand Down
12 changes: 0 additions & 12 deletions bundle/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@ func TestHasRootsOverlap(t *testing.T) {
}
}

//err := hasRootsOverlap(ctx, mockStore, txn, bundles)
//if !tc.overlaps && err != nil {
// t.Fatalf("unepected error: %s", err)
//} else if tc.overlaps && (err == nil || !strings.Contains(err.Error(), "detected overlapping roots in bundle manifest")) {
// t.Fatalf("expected overlapping roots error, got: %s", err)
//}

//err = mockStore.Commit(ctx, txn)
//if err != nil {
// t.Fatalf("unexpected error: %s", err)
//}

mockStore.AssertValid(t)
})
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestRunBenchmarkE2EWithOPAConfigFile(t *testing.T) {

params := testBenchParams()
params.e2e = true
params.configFile = filepath.Join(testDirRoot, "/config.yaml")
params.configFile = filepath.Join(testDirRoot, "config.yaml")

args := []string{"1 + 1"}
var buf bytes.Buffer
Expand Down Expand Up @@ -580,7 +580,7 @@ func TestBenchMainInvalidInputFile(t *testing.T) {
}
args := []string{"1+1"}
test.WithTempFS(files, func(path string) {
params.inputPath = filepath.Join(path, "definitely/not/input.yaml")
params.inputPath = filepath.Join(path, "definitely", "not", "input.yaml")

var buf bytes.Buffer

Expand Down Expand Up @@ -662,7 +662,7 @@ func TestBenchMainInvalidInputFileE2E(t *testing.T) {
}
args := []string{"1+1"}
test.WithTempFS(files, func(path string) {
params.inputPath = filepath.Join(path, "definitely/not/input.yaml")
params.inputPath = filepath.Join(path, "definitely", "not", "input.yaml")

var buf bytes.Buffer

Expand Down
2 changes: 1 addition & 1 deletion cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ import rego.v1`,
for i := range tests {
tc := tests[i]
tc.bundleMode = true
tc.note = tc.note + " (as bundle)"
tc.note += " (as bundle)"
tests = append(tests, tc)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ import rego.v1`,
for i := range tests {
tc := tests[i]
tc.bundleMode = true
tc.note = tc.note + " (as bundle)"
tc.note += " (as bundle)"
tests = append(tests, tc)
}

Expand Down
18 changes: 6 additions & 12 deletions cmd/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ a contains x if {
t.Fatalf("Expected error:\n\n%s\n\ngot:\n\n%s", expErr, err.Error())
}
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
} else if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
})
Expand Down Expand Up @@ -249,10 +247,8 @@ p contains 3 if {
t.Fatalf("Expected error:\n\n%s\n\ngot:\n\n%s", expErr, err.Error())
}
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
} else if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
})
Expand Down Expand Up @@ -566,10 +562,8 @@ p contains 4 if {
t.Fatalf("Expected error:\n\n%s\n\ngot:\n\n%s", expErr, err.Error())
}
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
} else if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
})
Expand Down
6 changes: 2 additions & 4 deletions cmd/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,8 @@ p contains v if {
t.Fatalf("Expected error:\n\n%v\n\nbut got:\n\n%v", expErr, err.Error())
}
}
} else {
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
} else if err != nil {
t.Fatalf("Unexpected error %v", err)
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (cf cmdFlagsImpl) CheckEnvironmentVariables(command *cobra.Command) error {
}
command.Flags().VisitAll(func(f *pflag.Flag) {
configName := f.Name
configName = strings.Replace(configName, "-", "_", -1)
configName = strings.ReplaceAll(configName, "-", "_")
if !f.Changed && v.IsSet(configName) {
val := v.Get(configName)
err := command.Flags().Set(f.Name, fmt.Sprintf("%v", val))
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestExec(t *testing.T) {
})

err := Exec(ctx, opa, params)
output := strings.Replace(buf.String(), dir, "%ROOT%", -1)
output := strings.ReplaceAll(buf.String(), dir, "%ROOT%")
tt.assertion(t, output, err)
})
})
Expand Down
24 changes: 10 additions & 14 deletions cmd/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ p = 1
t.Fatalf("Expected no stderr output, got:\n%s\n", string(stderr))
}

expectedOutput := strings.Replace(`{
expectedOutput := strings.ReplaceAll(`{
"package": {
"location": {
"file": "TEMPDIR/x.rego",
Expand Down Expand Up @@ -238,7 +238,7 @@ p = 1
}
]
}
`, "TEMPDIR", tempDirPath, -1)
`, "TEMPDIR", tempDirPath)

gotLines := strings.Split(string(stdout), "\n")
wantLines := strings.Split(expectedOutput, "\n")
Expand Down Expand Up @@ -350,7 +350,7 @@ a.b.c := true
t.Fatalf("Expected no stderr output, got:\n%s\n", string(stderr))
}

expectedOutput := strings.Replace(`{
expectedOutput := strings.ReplaceAll(`{
"package": {
"location": {
"file": "TEMPDIR/x.rego",
Expand Down Expand Up @@ -464,7 +464,7 @@ a.b.c := true
}
]
}
`, "TEMPDIR", tempDirPath, -1)
`, "TEMPDIR", tempDirPath)

gotLines := strings.Split(string(stdout), "\n")
wantLines := strings.Split(expectedOutput, "\n")
Expand Down Expand Up @@ -508,7 +508,7 @@ allow = true if {
t.Fatalf("Expected no stderr output, got:\n%s\n", string(stderr))
}

expectedOutput := strings.Replace(`{
expectedOutput := strings.ReplaceAll(`{
"package": {
"location": {
"file": "TEMPDIR/x.rego",
Expand Down Expand Up @@ -925,7 +925,7 @@ allow = true if {
}
]
}
`, "TEMPDIR", tempDirPath, -1)
`, "TEMPDIR", tempDirPath)

gotLines := strings.Split(string(stdout), "\n")
wantLines := strings.Split(expectedOutput, "\n")
Expand Down Expand Up @@ -1015,10 +1015,8 @@ a contains x if {
t.Fatalf("Expected error:\n\n%q\n\ngot:\n\n%s", expErr, errs)
}
}
} else {
if len(stderr) > 0 {
t.Fatalf("Expected no stderr output, got:\n%s\n", string(stderr))
}
} else if len(stderr) > 0 {
t.Fatalf("Expected no stderr output, got:\n%s\n", string(stderr))
}
})
}
Expand Down Expand Up @@ -1172,10 +1170,8 @@ p contains v if {
t.Fatalf("Expected error:\n\n%q\n\ngot:\n\n%s", expErr, errs)
}
}
} else {
if len(stderr) > 0 {
t.Fatalf("Expected no stderr output, got:\n%s\n", string(stderr))
}
} else if len(stderr) > 0 {
t.Fatalf("Expected no stderr output, got:\n%s\n", string(stderr))
}
})
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,8 @@ func TestValidateSignParams(t *testing.T) {
if tc.err != nil && tc.err.Error() != err.Error() {
t.Fatalf("Expected error message %v but got %v", tc.err.Error(), err.Error())
}
} else {
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
} else if err != nil {
t.Fatalf("Unexpected error %v", err)
}
})
}
Expand Down
6 changes: 2 additions & 4 deletions compile/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,8 @@ p contains "B" if {
t.Fatalf("expected error to contain:\n\n%s\n\ngot:\n\n%v", expErr, err)
}
}
} else {
if err != nil {
t.Fatal(err)
}
} else if err != nil {
t.Fatal(err)
}
})
})
Expand Down
6 changes: 3 additions & 3 deletions internal/bundle/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func TestGenerateBundleInfoWithFileDir(t *testing.T) {
expectedNamespaces := map[string][]string{
"data": {filepath.Join(rootDir, "data.json")},
"data.bar": {filepath.Join(rootDir, "base.rego")},
"data.foo": {filepath.Join(rootDir, "baz/authz.rego"), filepath.Join(rootDir, "foo/policy.rego")},
"data.fuz": {filepath.Join(rootDir, "fuz/fuz.rego"), filepath.Join(rootDir, "fuz/data.json")},
"data.foo": {filepath.Join(rootDir, "baz", "authz.rego"), filepath.Join(rootDir, "foo", "policy.rego")},
"data.fuz": {filepath.Join(rootDir, "fuz", "fuz.rego"), filepath.Join(rootDir, "fuz", "data.json")},
}

if !reflect.DeepEqual(info.Namespaces, expectedNamespaces) {
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestGenerateBundleInfoWithBundleTarGz(t *testing.T) {
expectedWasmModules := []map[string]interface{}{}
expectedWasmModule1 := map[string]interface{}{
"path": "/example/policy.wasm",
"url": filepath.Join(bundleFile, "/example/policy.wasm"),
"url": filepath.Join(bundleFile, "example", "policy.wasm"),
"entrypoints": []string{"data.http.example.foo.allow"},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/bundle/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func LoadBundleFromDiskForRegoVersion(regoVersion ast.RegoVersion, path, name st

_, err := os.Stat(bundlePath)
if err == nil {
f, err := os.Open(filepath.Join(bundlePath))
f, err := os.Open(bundlePath)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 095308d

Please sign in to comment.