From a55d2f4c0b259959c0f5c22d130e273970108995 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Wed, 2 Oct 2024 12:39:10 +0200 Subject: [PATCH] Fix some minor issues reported by IntelliJ (#1159) Would be good to know why golangci-lint fails to report these, as some were fairly obvious. But oh well! Signed-off-by: Anders Eknert --- e2e/cli_test.go | 6 +++--- internal/capabilities/capabilities.go | 4 ++-- internal/compile/compile.go | 7 +++---- internal/embeds/embeds.go | 4 ---- internal/explorer/stages.go | 2 +- internal/lsp/opa/tokens/tokens.go | 6 ------ internal/lsp/server_test.go | 22 ---------------------- internal/mode/standalone.go | 2 +- internal/util/util.go | 10 ---------- pkg/fixer/reporter.go | 8 ++------ 10 files changed, 12 insertions(+), 59 deletions(-) diff --git a/e2e/cli_test.go b/e2e/cli_test.go index f62c0998..200ad41c 100644 --- a/e2e/cli_test.go +++ b/e2e/cli_test.go @@ -790,7 +790,7 @@ test_allow { } for file, content := range initialState { - mustWriteToFile(t, filepath.Join(td, file), string(content)) + mustWriteToFile(t, filepath.Join(td, file), content) } // --force is required to make the changes when there is no git repo @@ -917,7 +917,7 @@ import rego.v1 } for file, content := range initialState { - mustWriteToFile(t, filepath.Join(td, file), string(content)) + mustWriteToFile(t, filepath.Join(td, file), content) } // --force is required to make the changes when there is no git repo @@ -989,7 +989,7 @@ import rego.v1 } for file, content := range initialState { - mustWriteToFile(t, filepath.Join(td, file), string(content)) + mustWriteToFile(t, filepath.Join(td, file), content) } // --force is required to make the changes when there is no git repo diff --git a/internal/capabilities/capabilities.go b/internal/capabilities/capabilities.go index 1a7b3ed5..4271a249 100644 --- a/internal/capabilities/capabilities.go +++ b/internal/capabilities/capabilities.go @@ -1,4 +1,4 @@ -// package capabilities provides convenient access to OPA capabilities +// Package capabilities provides convenient access to OPA capabilities // definitions that are embedded within Regal. package capabilities @@ -17,7 +17,7 @@ import ( "github.com/open-policy-agent/opa/ast" - embedded "github.com/styrainc/regal/internal/capabilities/embedded" + "github.com/styrainc/regal/internal/capabilities/embedded" ) const ( diff --git a/internal/compile/compile.go b/internal/compile/compile.go index dc35ea75..d1ac27fd 100644 --- a/internal/compile/compile.go +++ b/internal/compile/compile.go @@ -2,7 +2,7 @@ package compile import ( "github.com/open-policy-agent/opa/ast" - opa_util "github.com/open-policy-agent/opa/util" + opaUtil "github.com/open-policy-agent/opa/util" "github.com/styrainc/regal/internal/embeds" "github.com/styrainc/regal/internal/util" @@ -26,14 +26,13 @@ func Capabilities() *ast.Capabilities { func RegalSchemaSet() *ast.SchemaSet { schemaSet := ast.NewSchemaSet() - astSchema := util.Must(embeds.SchemasFS.ReadFile("schemas/regal-ast.json")) - schemaSet.Put(ast.MustParseRef("schema.regal.ast"), opa_util.MustUnmarshalJSON(astSchema)) + schemaSet.Put(ast.MustParseRef("schema.regal.ast"), opaUtil.MustUnmarshalJSON(astSchema)) aggregateSchema := util.Must(embeds.SchemasFS.ReadFile("schemas/aggregate.json")) - schemaSet.Put(ast.MustParseRef("schema.regal.aggregate"), opa_util.MustUnmarshalJSON(aggregateSchema)) + schemaSet.Put(ast.MustParseRef("schema.regal.aggregate"), opaUtil.MustUnmarshalJSON(aggregateSchema)) return schemaSet } diff --git a/internal/embeds/embeds.go b/internal/embeds/embeds.go index 6422b3e1..f1cbd538 100644 --- a/internal/embeds/embeds.go +++ b/internal/embeds/embeds.go @@ -3,12 +3,8 @@ package embeds import ( "embed" - - "github.com/styrainc/regal/bundle" ) -var EmbedBundleFS = bundle.Bundle - //go:embed templates var EmbedTemplatesFS embed.FS diff --git a/internal/explorer/stages.go b/internal/explorer/stages.go index 0e16c55b..e522e735 100644 --- a/internal/explorer/stages.go +++ b/internal/explorer/stages.go @@ -25,7 +25,7 @@ type stage struct{ name, metricName string } // NOTE(sr): copied from 0.68.0. // //nolint:gochecknoglobals -var stages []stage = []stage{ +var stages = []stage{ {"ResolveRefs", "compile_stage_resolve_refs"}, {"InitLocalVarGen", "compile_stage_init_local_var_gen"}, {"RewriteRuleHeadRefs", "compile_stage_rewrite_rule_head_refs"}, diff --git a/internal/lsp/opa/tokens/tokens.go b/internal/lsp/opa/tokens/tokens.go index 3db60b3f..4da7159f 100644 --- a/internal/lsp/opa/tokens/tokens.go +++ b/internal/lsp/opa/tokens/tokens.go @@ -145,9 +145,3 @@ func Keywords() map[string]Token { } return cpy } - -// IsKeyword returns if a token is a keyword -func IsKeyword(tok Token) bool { - _, ok := keywords[strings[tok]] - return ok -} diff --git a/internal/lsp/server_test.go b/internal/lsp/server_test.go index f4bb58ab..209e4148 100644 --- a/internal/lsp/server_test.go +++ b/internal/lsp/server_test.go @@ -1,7 +1,6 @@ package lsp import ( - "bytes" "context" "encoding/json" "errors" @@ -36,27 +35,6 @@ const defaultTimeout = 20 * time.Second const defaultBufferedChannelSize = 5 -// InMemoryReadWriteCloser is an in-memory implementation of jsonrpc2.ReadWriteCloser. -type InMemoryReadWriteCloser struct { - Buffer bytes.Buffer -} - -func (m *InMemoryReadWriteCloser) Read(p []byte) (int, error) { - c, err := m.Buffer.Read(p) - - return c, fmt.Errorf("in memory read failed with error: %w", err) -} - -func (m *InMemoryReadWriteCloser) Write(p []byte) (int, error) { - c, err := m.Buffer.Write(p) - - return c, fmt.Errorf("in memory write failed with error: %w", err) -} - -func (*InMemoryReadWriteCloser) Close() error { - return nil // No-op for in-memory implementation -} - const fileURIScheme = "file://" // TestLanguageServerSingleFile tests that changes to a single file and Regal config are handled correctly by the diff --git a/internal/mode/standalone.go b/internal/mode/standalone.go index dee2710f..3f331eea 100644 --- a/internal/mode/standalone.go +++ b/internal/mode/standalone.go @@ -3,6 +3,6 @@ package mode // Standalone lets us change the output of some commands when Regal -// us used as a binary, as opposed to when it's embedded via its +// is used as a binary, as opposed to when it's embedded via its // Go module. const Standalone = false diff --git a/internal/util/util.go b/internal/util/util.go index d1afc10f..8b400e16 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -86,16 +86,6 @@ func Map[T, U any](f func(T) U, a []T) []U { return b } -// MapInvert inverts a map (swaps keys and values). -func MapInvert[K comparable, V comparable](m map[K]V) map[V]K { - n := make(map[V]K, len(m)) - for k, v := range m { - n[v] = k - } - - return n -} - // FindClosestMatchingRoot finds the closest matching root for a given path. // If no matching root is found, an empty string is returned. func FindClosestMatchingRoot(path string, roots []string) string { diff --git a/pkg/fixer/reporter.go b/pkg/fixer/reporter.go index e3c60bf3..702173aa 100644 --- a/pkg/fixer/reporter.go +++ b/pkg/fixer/reporter.go @@ -50,9 +50,7 @@ func (r *PrettyReporter) ReportConflicts(fixReport *Report) error { if len(roots) > 0 { fmt.Fprintln(r.outputWriter, "Source file conflicts:") - i := 0 - - for _, rootKey := range roots { + for i, rootKey := range roots { if i > 0 { fmt.Fprintln(r.outputWriter) } @@ -90,9 +88,7 @@ func (r *PrettyReporter) ReportConflicts(fixReport *Report) error { fmt.Fprintln(r.outputWriter, "Many to one conflicts:") - i := 0 - - for _, rootKey := range roots { + for i, rootKey := range roots { if i > 0 { fmt.Fprintln(r.outputWriter) }