blob: a55e7591cc7d40606d15b7dff076fc7e73beaefb [file] [log] [blame]
Tim Windelschmidte2b28652024-04-12 16:49:16 +02001load("@com_github_sluongng_nogo_analyzer//staticcheck:def.bzl", ALL_STATICCHECK_ANALYZERS = "ANALYZERS", format_staticcheck_analyzers = "staticcheck_analyzers")
2load("@com_github_sluongng_nogo_analyzer//:def.bzl", gen_nogo_config = "nogo_config")
3load("@bazel_skylib//rules:write_file.bzl", "write_file")
Serge Bazanski0ef96292021-05-21 15:41:32 +02004load("@io_bazel_rules_go//go:def.bzl", "nogo")
5
Tim Windelschmidte2b28652024-04-12 16:49:16 +02006# NOGO_PASSES contains all enabled analyzers that nogo should execute.
7NOGO_PASSES = []
8
Serge Bazanski0ef96292021-05-21 15:41:32 +02009# These deps enable the analyses equivalent to running `go vet`.
10# Passing vet = True enables only a tiny subset of these (the ones
11# that are always correct).
12# You can see the what `go vet` does by running `go doc cmd/vet`.
Tim Windelschmidte2b28652024-04-12 16:49:16 +020013NOGO_PASSES += [
14 "@org_golang_x_tools//go/analysis/passes/appends",
15 "@org_golang_x_tools//go/analysis/passes/asmdecl",
16 "@org_golang_x_tools//go/analysis/passes/assign",
17 "@org_golang_x_tools//go/analysis/passes/atomic",
18 "@org_golang_x_tools//go/analysis/passes/atomicalign",
19 "@org_golang_x_tools//go/analysis/passes/bools",
20 "@org_golang_x_tools//go/analysis/passes/buildssa",
21 "@org_golang_x_tools//go/analysis/passes/buildtag",
Serge Bazanski0ef96292021-05-21 15:41:32 +020022 # Disable cgocall because it fails processing com_github_mattn_go_sqlite3 before exclusions are applied
Tim Windelschmidte2b28652024-04-12 16:49:16 +020023 #"@org_golang_x_tools//go/analysis/passes/cgocall",
24 "@org_golang_x_tools//go/analysis/passes/composite",
25 "@org_golang_x_tools//go/analysis/passes/copylock",
26 "@org_golang_x_tools//go/analysis/passes/deepequalerrors",
27 "@org_golang_x_tools//go/analysis/passes/defers",
28 "@org_golang_x_tools//go/analysis/passes/directive",
29 "@org_golang_x_tools//go/analysis/passes/errorsas",
30 # Disabled as there is no real benefit from it.
31 #"@org_golang_x_tools//go/analysis/passes/fieldalignment",
32 "@org_golang_x_tools//go/analysis/passes/framepointer",
33 "@org_golang_x_tools//go/analysis/passes/httpmux",
34 "@org_golang_x_tools//go/analysis/passes/httpresponse",
35 "@org_golang_x_tools//go/analysis/passes/ifaceassert",
36 # Disabled because we are using Go 1.22
37 # https://go.dev/blog/loopvar-preview
38 #"@org_golang_x_tools//go/analysis/passes/loopclosure",
39 "@org_golang_x_tools//go/analysis/passes/lostcancel",
40 "@org_golang_x_tools//go/analysis/passes/nilfunc",
41 "@org_golang_x_tools//go/analysis/passes/nilness",
42 "@org_golang_x_tools//go/analysis/passes/printf",
43 "@org_golang_x_tools//go/analysis/passes/reflectvaluecompare",
44 # Disabled because of too many false positives
45 # "@org_golang_x_tools//go/analysis/passes/shadow",
46 "@org_golang_x_tools//go/analysis/passes/shift",
47 "@org_golang_x_tools//go/analysis/passes/sigchanyzer",
48 "@org_golang_x_tools//go/analysis/passes/slog",
49 "@org_golang_x_tools//go/analysis/passes/sortslice",
50 "@org_golang_x_tools//go/analysis/passes/stdmethods",
51 "@org_golang_x_tools//go/analysis/passes/stringintconv",
52 "@org_golang_x_tools//go/analysis/passes/structtag",
53 "@org_golang_x_tools//go/analysis/passes/testinggoroutine",
54 "@org_golang_x_tools//go/analysis/passes/tests",
55 "@org_golang_x_tools//go/analysis/passes/timeformat",
56 "@org_golang_x_tools//go/analysis/passes/unmarshal",
57 "@org_golang_x_tools//go/analysis/passes/unreachable",
58 "@org_golang_x_tools//go/analysis/passes/unsafeptr",
59 "@org_golang_x_tools//go/analysis/passes/unusedresult",
60 "@org_golang_x_tools//go/analysis/passes/unusedwrite",
Serge Bazanski0ef96292021-05-21 15:41:32 +020061]
62
Tim Windelschmidte2b28652024-04-12 16:49:16 +020063# Append some passes provided by CockroachDB.
64NOGO_PASSES += [
65 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errcmp",
Tim Windelschmidt5f1a7de2024-09-19 02:00:14 +020066 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errwrap",
Tim Windelschmidte2b28652024-04-12 16:49:16 +020067 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/hash",
68 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/nilness",
69 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/nocopy",
70 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/returnerrcheck",
71 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/timer",
72 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/unconvert",
73]
74
75# Combine all staticcheck analyzers with a list
76# of all globally disabled staticcheck analyzers
77# and append them to the nogo passes.
78NOGO_PASSES += format_staticcheck_analyzers(ALL_STATICCHECK_ANALYZERS + [
79 "-ST1000", # at least one file in a package should have a package comment
80 "-ST1003", # should not use ALL_CAPS in Go names; use CamelCase instead
81 "-QF1006", # could lift into loop condition
82 "-QF1003", # could use tagged switch
83 "-QF1008", # Omit embedded fields from selector expression
Tim Windelschmidte2b28652024-04-12 16:49:16 +020084])
85
86NOGO_PASSES += [
87 # This analyzer ensures that all comment lines are <= 80 characters long
88 # in Go source. This is in line with general practices around the Go
89 # community, where code lines can be as long as needed (and is expected
90 # to be soft-reflowable by text editors), but comments are kept at a
91 # 'standard' 80 characters long, as prose within comment blocks does not
92 # soft-reflow well.
93 "@com_github_corverroos_commentwrap//:go_default_library",
94 "//build/analysis/checkcompilerdirectives",
95 "//build/analysis/noioutil",
96 "//build/analysis/importsort",
97]
98
99# NOGO_CONFIG contains the overrides for nogo to exempt specific files
100# from being analyzed.
101NOGO_CONFIG = {
102 "shift": {
103 "exclude_files": {
104 "external/dev_gvisor_gvisor": "third_party",
105 },
106 },
107 "stringintconv": {
108 "exclude_files": {
109 "external/com_github_masterminds_goutils": "third_party",
110 },
111 },
112 "noioutil": {
113 "exclude_files": {
114 "external/": "TODO(tim): break me up and filter out unmaintained dependencies",
115 },
116 },
117 "nilness": {
118 "exclude_files": {
119 "external/org_golang_x_tools": "third_party",
120 "external/in_gopkg_yaml_v2": "third_party",
121 "external/com_github_google_cadvisor": "third_party",
122 "external/com_github_pkg_sftp": "third_party",
123 "external/com_github_vishvananda_netlink": "third_party",
124 "external/com_github_go_sql_driver_mysql": "third_party",
125 "external/com_github_google_go_tpm": "third_party",
126 "external/com_github_json_iterator_go": "third_party",
127 "external/com_github_gregjones_httpcache": "third_party",
128 "external/com_github_cilium_ebpf": "third_party",
129 "external/com_github_urfave_cli": "third_party",
130 "external/in_gopkg_square_go_jose_v2": "third_party",
131 "external/com_github_alecthomas_kingpin_v2": "third_party",
132 "external/io_k8s_mount_utils": "third_party",
133 "external/com_github_stefanberger_go_pkcs11uri": "third_party",
134 "external/com_github_go_delve_delve": "third_party",
135 "external/io_opencensus_go": "third_party",
136 "external/io_k8s_apimachinery": "third_party",
137 "external/io_k8s_kubernetes": "third_party",
138 "external/io_k8s_kube_openapi": "third_party",
139 "external/io_k8s_apiextensions_apiserver": "third_party",
140 "external/io_etcd_go_etcd_client_v3": "third_party",
141 "external/com_github_coredns_coredns": "third_party",
142 "external/io_etcd_go_etcd_server_v3": "third_party",
143 "external/com_github_containerd_containerd": "third_party",
144 "external/io_k8s_client_go": "third_party",
145 "external/io_k8s_apiserver": "third_party",
146 "external/io_k8s_kubectl": "third_party",
147 "external/com_github_spf13_pflag": "third_party",
148 "external/com_github_burntsushi_toml": "third_party",
149 },
150 },
151 "unsafeptr": {
152 "exclude_files": {
153 "external/com_github_modern_go_reflect2/": "third_party",
154 "sqlite3.*go": "third_party",
155 "external/dev_gvisor_gvisor/": "third_party",
156 "external/io_k8s_sigs_structured_merge_diff/": "third_party",
157 "external/com_github_go_delve_delve/": "third_party",
158 "external/com_github_mailru_easyjson/jlexer/": "third_party",
159 "external/com_github_cilium_ebpf/": "third_party",
160 "external/org_golang_x_sys": "third_party",
161 "external/net_starlark_go": "third_party",
162 "external/com_github_pingcap_tidb_parser": "third_party",
163 "external/com_github_dennwc_btrfs": "third_party",
164 },
165 },
166 "lostcancel": {
167 "exclude_files": {
168 "external/org_golang_x_tools": "third_party",
169 "external/com_github_grpc_ecosystem_grpc_gateway": "third_party",
170 },
171 },
172 "deepequalerrors": {
173 "exclude_files": {
174 "external/com_github_u_root_uio": "third_party",
175 },
176 },
177 "copylocks": {
178 "exclude_files": {
179 "external/org_golang_google_protobuf": "third_party",
180 "external/com_github_derekparker_trie": "third_party",
181 "external/com_github_hodgesds_perf_utils": "third_party",
182 "external/com_github_google_gnostic": "third_party",
183 "external/com_github_coredns_coredns": "third_party",
184 "external/com_github_pseudomuto_protoc_gen_doc": "third_party",
185 "external/io_k8s_apiserver": "third_party",
186 },
187 },
188 "defers": {
189 "exclude_files": {
190 "external/com_github_sbezverk_nfproxy": "third_party",
191 },
192 },
193 "unparam": {
194 "exclude_files": {
195 "external/": "third_party",
196 "bazel-out/": "generated_output",
197 "cgo/": "cgo",
198 },
199 },
200}
201
202# All analyzers that should be disabled for external, generated or cgo code.
203DISABLED_FOR_EXTERNAL_CODE = [
204 "exclude_files",
205 "commentwrap",
206 "importsort",
207 "unreachable",
208 "unusedwrite",
209 "composites",
210 "stdmethods",
211 "reflectvaluecompare",
212 "unconvert",
213 "errwrap",
214 "ruleguard",
215 "returnerrcheck",
216 "hash",
217 "errcmp",
218] + ALL_STATICCHECK_ANALYZERS
219
220# We override the variable with itself unioned with the other
221# config part, as the Intellij integration doesn't understand
222# the |= expression which makes editing this file kinda annoying.
223NOGO_CONFIG = NOGO_CONFIG | {
224 analyzer: {
225 "exclude_files": {
226 # Don't run linters on external dependencies
227 "external/": "third_party",
228 "bazel-out/": "generated_output",
229 "cgo/": "cgo",
230 },
231 }
232 for analyzer in DISABLED_FOR_EXTERNAL_CODE
233}
234
235write_file(
236 name = "nogo_config",
237 out = "nogo_config.json",
238 content = [json.encode_indent(NOGO_CONFIG)],
239)
240
Serge Bazanski0ef96292021-05-21 15:41:32 +0200241nogo(
242 name = "nogo",
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200243 config = ":nogo_config",
Serge Bazanski0ef96292021-05-21 15:41:32 +0200244 visibility = ["//visibility:public"],
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200245 deps = NOGO_PASSES,
Serge Bazanski0ef96292021-05-21 15:41:32 +0200246)