Skip to content

Commit

Permalink
lint: move linters from oncilla/gochecks
Browse files Browse the repository at this point in the history
Move the linters from https://github.com/oncilla/gochecks and update them to the updated package paths.

Closes #4174

GitOrigin-RevId: 219b897858ff56ff94dd7103ab0d44aad283282e
  • Loading branch information
oncilla committed Mar 26, 2022
1 parent ea83919 commit 5558ba7
Show file tree
Hide file tree
Showing 19 changed files with 814 additions and 82 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin
docker/_build
rules_openapi/tools/node_modules
tools/lint/logctxcheck/testdata/src
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ load("@cgrindel_bazel_starlib//updatesrc:defs.bzl", "updatesrc_update_all")
# gazelle:exclude proto/**
# gazelle:exclude doc/**
# gazelle:exclude rules_openapi/tools/node_modules/**
# gazelle:exclude tools/lint/**/testdata/src/**
gazelle(name = "gazelle")

go_lint_config(
Expand Down
21 changes: 0 additions & 21 deletions licenses/data/com_github_oncilla_gochecks/LICENSE

This file was deleted.

4 changes: 2 additions & 2 deletions nogo.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
nogo_deps = [
"@com_github_scionproto_scion//tools/lint/log:go_default_library",
"@com_github_scionproto_scion//tools/lint/maincheck:go_default_library",
"@com_github_oncilla_gochecks//logcheck:go_default_library",
"@com_github_oncilla_gochecks//serrorscheck:go_default_library",
"@com_github_scionproto_scion//tools/lint/serrorscheck:go_default_library",
"@com_github_scionproto_scion//tools/lint/logctxcheck:go_default_library",
"@org_golang_x_tools//go/analysis/passes/asmdecl:go_default_library",
"@org_golang_x_tools//go/analysis/passes/assign:go_default_library",
"@org_golang_x_tools//go/analysis/passes/atomic:go_default_library",
Expand Down
119 changes: 60 additions & 59 deletions nogo.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"/com_github_grpc_ecosystem_grpc_opentracing/go/otgrpc/": "",
"/com_github_vishvananda_netlink/": "",
"/org_golang_x_tools/internal": "",
"/org_golang_google_protobuf": ""
"/org_golang_google_protobuf": "",
"/org_golang_x_tools/go/analysis/internal/checker": ""
}
},
"copylocks": {
Expand Down Expand Up @@ -145,64 +146,64 @@
"/com_github_golang_mock/mockgen": ""
}
},
"reflectvaluecompare": {
"exclude_files": {
"in_gopkg_yaml_v2": "",
"com_github_smartystreets_assertions": ""
}
},
"shadow": {
"exclude_files": {
"org_golang_x_text": "",
"org_golang_x_net": "",
"org_golang_x_sys/unix": "",
"org_golang_x_crypto/": "",
"org_golang_x_tools": "",
"org_golang_google_protobuf": "",
"org_golang_google_grpc": "",
"in_gopkg_yaml_v2/": "",
"in_gopkg_yaml_v3": "",
"in_gopkg_ini_v1": "",
"com_github_google_go_cmp/cmp": "",
"com_github_google_gopacket": "",
"com_github_golang_protobuf": "",
"com_github_matttproud_golang_protobuf_extensions": "",
"com_github_getkin_kin_openapi": "",
"com_github_buildkite_go_buildkite_v2/buildkite": "",
"com_github_deepmap_oapi_codegen/pkg/runtime": "",
"com_github_uber_jaeger_client_go/thrift": "",
"com_github_hashicorp_hcl": "",
"com_github_stretchr_testify/assert": "",
"com_github_spf13_afero": "",
"com_github_marten_seemann_qtls_go1_16": "",
"com_github_marten_seemann_qtls_go1_17": "",
"com_github_lucas_clemente_quic_go": "",
"com_github_lestrrat_go_jwx": "",
"com_github_pelletier_go_toml": "",
"com_github_mdlayher_raw": "",
"com_github_vishvananda_netlink": "",
"com_github_prometheus_common/internal": "",
"com_github_prometheus_procfs": "",
"com_github_prometheus_client_golang": "",
"com_github_uber_jaeger_client_go": "",
"com_github_spf13_viper": "",
"com_github_labstack_echo_v4": "",
"com_github_deepmap_oapi_codegen": "",
"reflectvaluecompare": {
"exclude_files": {
"in_gopkg_yaml_v2": "",
"com_github_smartystreets_assertions": ""
}
},
"shadow": {
"exclude_files": {
"org_golang_x_text": "",
"org_golang_x_net": "",
"org_golang_x_sys/unix": "",
"org_golang_x_crypto/": "",
"org_golang_x_tools": "",
"org_golang_google_protobuf": "",
"org_golang_google_grpc": "",
"in_gopkg_yaml_v2/": "",
"in_gopkg_yaml_v3": "",
"in_gopkg_ini_v1": "",
"com_github_google_go_cmp/cmp": "",
"com_github_google_gopacket": "",
"com_github_golang_protobuf": "",
"com_github_matttproud_golang_protobuf_extensions": "",
"com_github_getkin_kin_openapi": "",
"com_github_buildkite_go_buildkite_v2/buildkite": "",
"com_github_deepmap_oapi_codegen/pkg/runtime": "",
"com_github_uber_jaeger_client_go/thrift": "",
"com_github_hashicorp_hcl": "",
"com_github_stretchr_testify/assert": "",
"com_github_spf13_afero": "",
"com_github_marten_seemann_qtls_go1_16": "",
"com_github_marten_seemann_qtls_go1_17": "",
"com_github_lucas_clemente_quic_go": "",
"com_github_lestrrat_go_jwx": "",
"com_github_pelletier_go_toml": "",
"com_github_mdlayher_raw": "",
"com_github_vishvananda_netlink": "",
"com_github_prometheus_common/internal": "",
"com_github_prometheus_procfs": "",
"com_github_prometheus_client_golang": "",
"com_github_uber_jaeger_client_go": "",
"com_github_spf13_viper": "",
"com_github_labstack_echo_v4": "",
"com_github_deepmap_oapi_codegen": "",

"pkg/private/serrors/errors.go": "err shadowed",
"pkg/slayers/path/scion/raw.go": "err shadowed",
"pkg/scrypto/cppki/trc.go": "err shadowed",
"private/mgmtapi/jwtauth/jwt.go": "err shadowed",
"dispatcher/underlay.go": "err shadowed"
}
},
"unusedwrite": {
"exclude_files": {
"org_golang_x_mod": "",
"org_golang_x_crypto/ed25519/internal/edwards25519": "",
"com_github_vishvananda_netlink/nl": "",
"com_github_deepmap_oapi_codegen/pkg/codegen": "",
"control/colibri/reservation/e2e/request_test.go": ""
"pkg/private/serrors/errors.go": "err shadowed",
"pkg/slayers/path/scion/raw.go": "err shadowed",
"pkg/scrypto/cppki/trc.go": "err shadowed",
"private/mgmtapi/jwtauth/jwt.go": "err shadowed",
"dispatcher/underlay.go": "err shadowed"
}
},
"unusedwrite": {
"exclude_files": {
"org_golang_x_mod": "",
"org_golang_x_crypto/ed25519/internal/edwards25519": "",
"com_github_vishvananda_netlink/nl": "",
"com_github_deepmap_oapi_codegen/pkg/codegen": "",
"control/colibri/reservation/e2e/request_test.go": ""
}
}
}
}
1 change: 1 addition & 0 deletions tools/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go_lint() {
-a '!' -ipath '*/node_modules/*' \
-a '!' -ipath './scion-pki/certs/certinfo.go' \
-a '!' -ipath './scion-pki/certs/certformat.go' \
-a '!' -ipath './tools/lint/*/testdata/src/*' \
-a '!' -ipath '*mock_*' > $TMPDIR/gofiles.list
lint_step "Building lint tools"

Expand Down
9 changes: 9 additions & 0 deletions tools/lint/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("//tools/lint:go.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["lint.go"],
importpath = "github.com/scionproto/scion/tools/lint",
visibility = ["//visibility:public"],
deps = ["@org_golang_x_tools//go/analysis:go_default_library"],
)
68 changes: 68 additions & 0 deletions tools/lint/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2022 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package lint contains helpers for linting and static analysis.
package lint

import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
"go/types"
"path"
"strings"

"golang.org/x/tools/go/analysis"
)

// FindPackageNames returns all the imported packages mapped to their name in
// the file.
func FindPackageNames(file *ast.File) map[string]string {
pkgs := map[string]string{}
for _, imp := range file.Imports {
pkg := strings.Trim(imp.Path.Value, `"`)
name := path.Base(pkg)
if imp.Name != nil {
name = imp.Name.Name
}
pkgs[pkg] = name
}
return pkgs
}

// IsString checks if the underlying type of the literal is string.
func IsString(pass *analysis.Pass, lit ast.Expr) bool {
t, ok := pass.TypesInfo.TypeOf(lit).Underlying().(*types.Basic)
return ok && t.Info()&types.IsString != 0
}

// RenderList renders the list of expressions.
func RenderList(fset *token.FileSet, list []ast.Expr) string {
var p []string
for _, arg := range list {
p = append(p, Render(fset, arg))
}
return fmt.Sprintf("[%s]", strings.Join(p, ","))
}

// Render renders the the expression.
func Render(fset *token.FileSet, x interface{}) string {
var buf bytes.Buffer
if err := printer.Fprint(&buf, fset, x); err != nil {
panic(err)
}
return buf.String()
}
22 changes: 22 additions & 0 deletions tools/lint/logctxcheck/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//tools/lint:go.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["analyzer.go"],
importpath = "github.com/scionproto/scion/tools/lint/logctxcheck",
visibility = ["//visibility:public"],
deps = [
"//tools/lint:go_default_library",
"@org_golang_x_tools//go/analysis:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["analyzer_test.go"],
data = glob(["testdata/**"]),
deps = [
":go_default_library",
"@org_golang_x_tools//go/analysis/analysistest:go_default_library",
],
)
Loading

0 comments on commit 5558ba7

Please sign in to comment.