| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame^] | 1 | load("@com_github_sluongng_nogo_analyzer//staticcheck:def.bzl", ALL_STATICCHECK_ANALYZERS = "ANALYZERS", format_staticcheck_analyzers = "staticcheck_analyzers") |
| 2 | load("@com_github_sluongng_nogo_analyzer//:def.bzl", gen_nogo_config = "nogo_config") |
| 3 | load("@bazel_skylib//rules:write_file.bzl", "write_file") |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 4 | load("@io_bazel_rules_go//go:def.bzl", "nogo") |
| 5 | |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame^] | 6 | # NOGO_PASSES contains all enabled analyzers that nogo should execute. |
| 7 | NOGO_PASSES = [] |
| 8 | |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 9 | # 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 Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame^] | 13 | NOGO_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 Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 22 | # Disable cgocall because it fails processing com_github_mattn_go_sqlite3 before exclusions are applied |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame^] | 23 | #"@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 Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 61 | ] |
| 62 | |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame^] | 63 | # Append some passes provided by CockroachDB. |
| 64 | NOGO_PASSES += [ |
| 65 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errcmp", |
| 66 | # TODO(tim): Enable when fixed |
| 67 | # "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errwrap", |
| 68 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/hash", |
| 69 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/nilness", |
| 70 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/nocopy", |
| 71 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/returnerrcheck", |
| 72 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/timer", |
| 73 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/unconvert", |
| 74 | ] |
| 75 | |
| 76 | # Combine all staticcheck analyzers with a list |
| 77 | # of all globally disabled staticcheck analyzers |
| 78 | # and append them to the nogo passes. |
| 79 | NOGO_PASSES += format_staticcheck_analyzers(ALL_STATICCHECK_ANALYZERS + [ |
| 80 | "-ST1000", # at least one file in a package should have a package comment |
| 81 | "-ST1003", # should not use ALL_CAPS in Go names; use CamelCase instead |
| 82 | "-QF1006", # could lift into loop condition |
| 83 | "-QF1003", # could use tagged switch |
| 84 | "-QF1008", # Omit embedded fields from selector expression |
| 85 | |
| 86 | # TODO: Enable when fixed |
| 87 | "-SA4003", # no value of type uint32 is less than 0 |
| 88 | ]) |
| 89 | |
| 90 | NOGO_PASSES += [ |
| 91 | # This analyzer ensures that all comment lines are <= 80 characters long |
| 92 | # in Go source. This is in line with general practices around the Go |
| 93 | # community, where code lines can be as long as needed (and is expected |
| 94 | # to be soft-reflowable by text editors), but comments are kept at a |
| 95 | # 'standard' 80 characters long, as prose within comment blocks does not |
| 96 | # soft-reflow well. |
| 97 | "@com_github_corverroos_commentwrap//:go_default_library", |
| 98 | "//build/analysis/checkcompilerdirectives", |
| 99 | "//build/analysis/noioutil", |
| 100 | "//build/analysis/importsort", |
| 101 | ] |
| 102 | |
| 103 | # NOGO_CONFIG contains the overrides for nogo to exempt specific files |
| 104 | # from being analyzed. |
| 105 | NOGO_CONFIG = { |
| 106 | "shift": { |
| 107 | "exclude_files": { |
| 108 | "external/dev_gvisor_gvisor": "third_party", |
| 109 | }, |
| 110 | }, |
| 111 | "stringintconv": { |
| 112 | "exclude_files": { |
| 113 | "external/com_github_masterminds_goutils": "third_party", |
| 114 | }, |
| 115 | }, |
| 116 | "noioutil": { |
| 117 | "exclude_files": { |
| 118 | "external/": "TODO(tim): break me up and filter out unmaintained dependencies", |
| 119 | }, |
| 120 | }, |
| 121 | "nilness": { |
| 122 | "exclude_files": { |
| 123 | "external/org_golang_x_tools": "third_party", |
| 124 | "external/in_gopkg_yaml_v2": "third_party", |
| 125 | "external/com_github_google_cadvisor": "third_party", |
| 126 | "external/com_github_pkg_sftp": "third_party", |
| 127 | "external/com_github_vishvananda_netlink": "third_party", |
| 128 | "external/com_github_go_sql_driver_mysql": "third_party", |
| 129 | "external/com_github_google_go_tpm": "third_party", |
| 130 | "external/com_github_json_iterator_go": "third_party", |
| 131 | "external/com_github_gregjones_httpcache": "third_party", |
| 132 | "external/com_github_cilium_ebpf": "third_party", |
| 133 | "external/com_github_urfave_cli": "third_party", |
| 134 | "external/in_gopkg_square_go_jose_v2": "third_party", |
| 135 | "external/com_github_alecthomas_kingpin_v2": "third_party", |
| 136 | "external/io_k8s_mount_utils": "third_party", |
| 137 | "external/com_github_stefanberger_go_pkcs11uri": "third_party", |
| 138 | "external/com_github_go_delve_delve": "third_party", |
| 139 | "external/io_opencensus_go": "third_party", |
| 140 | "external/io_k8s_apimachinery": "third_party", |
| 141 | "external/io_k8s_kubernetes": "third_party", |
| 142 | "external/io_k8s_kube_openapi": "third_party", |
| 143 | "external/io_k8s_apiextensions_apiserver": "third_party", |
| 144 | "external/io_etcd_go_etcd_client_v3": "third_party", |
| 145 | "external/com_github_coredns_coredns": "third_party", |
| 146 | "external/io_etcd_go_etcd_server_v3": "third_party", |
| 147 | "external/com_github_containerd_containerd": "third_party", |
| 148 | "external/io_k8s_client_go": "third_party", |
| 149 | "external/io_k8s_apiserver": "third_party", |
| 150 | "external/io_k8s_kubectl": "third_party", |
| 151 | "external/com_github_spf13_pflag": "third_party", |
| 152 | "external/com_github_burntsushi_toml": "third_party", |
| 153 | }, |
| 154 | }, |
| 155 | "unsafeptr": { |
| 156 | "exclude_files": { |
| 157 | "external/com_github_modern_go_reflect2/": "third_party", |
| 158 | "sqlite3.*go": "third_party", |
| 159 | "external/dev_gvisor_gvisor/": "third_party", |
| 160 | "external/io_k8s_sigs_structured_merge_diff/": "third_party", |
| 161 | "external/com_github_go_delve_delve/": "third_party", |
| 162 | "external/com_github_mailru_easyjson/jlexer/": "third_party", |
| 163 | "external/com_github_cilium_ebpf/": "third_party", |
| 164 | "external/org_golang_x_sys": "third_party", |
| 165 | "external/net_starlark_go": "third_party", |
| 166 | "external/com_github_pingcap_tidb_parser": "third_party", |
| 167 | "external/com_github_dennwc_btrfs": "third_party", |
| 168 | }, |
| 169 | }, |
| 170 | "lostcancel": { |
| 171 | "exclude_files": { |
| 172 | "external/org_golang_x_tools": "third_party", |
| 173 | "external/com_github_grpc_ecosystem_grpc_gateway": "third_party", |
| 174 | }, |
| 175 | }, |
| 176 | "deepequalerrors": { |
| 177 | "exclude_files": { |
| 178 | "external/com_github_u_root_uio": "third_party", |
| 179 | }, |
| 180 | }, |
| 181 | "copylocks": { |
| 182 | "exclude_files": { |
| 183 | "external/org_golang_google_protobuf": "third_party", |
| 184 | "external/com_github_derekparker_trie": "third_party", |
| 185 | "external/com_github_hodgesds_perf_utils": "third_party", |
| 186 | "external/com_github_google_gnostic": "third_party", |
| 187 | "external/com_github_coredns_coredns": "third_party", |
| 188 | "external/com_github_pseudomuto_protoc_gen_doc": "third_party", |
| 189 | "external/io_k8s_apiserver": "third_party", |
| 190 | }, |
| 191 | }, |
| 192 | "defers": { |
| 193 | "exclude_files": { |
| 194 | "external/com_github_sbezverk_nfproxy": "third_party", |
| 195 | }, |
| 196 | }, |
| 197 | "unparam": { |
| 198 | "exclude_files": { |
| 199 | "external/": "third_party", |
| 200 | "bazel-out/": "generated_output", |
| 201 | "cgo/": "cgo", |
| 202 | }, |
| 203 | }, |
| 204 | } |
| 205 | |
| 206 | # All analyzers that should be disabled for external, generated or cgo code. |
| 207 | DISABLED_FOR_EXTERNAL_CODE = [ |
| 208 | "exclude_files", |
| 209 | "commentwrap", |
| 210 | "importsort", |
| 211 | "unreachable", |
| 212 | "unusedwrite", |
| 213 | "composites", |
| 214 | "stdmethods", |
| 215 | "reflectvaluecompare", |
| 216 | "unconvert", |
| 217 | "errwrap", |
| 218 | "ruleguard", |
| 219 | "returnerrcheck", |
| 220 | "hash", |
| 221 | "errcmp", |
| 222 | ] + ALL_STATICCHECK_ANALYZERS |
| 223 | |
| 224 | # We override the variable with itself unioned with the other |
| 225 | # config part, as the Intellij integration doesn't understand |
| 226 | # the |= expression which makes editing this file kinda annoying. |
| 227 | NOGO_CONFIG = NOGO_CONFIG | { |
| 228 | analyzer: { |
| 229 | "exclude_files": { |
| 230 | # Don't run linters on external dependencies |
| 231 | "external/": "third_party", |
| 232 | "bazel-out/": "generated_output", |
| 233 | "cgo/": "cgo", |
| 234 | }, |
| 235 | } |
| 236 | for analyzer in DISABLED_FOR_EXTERNAL_CODE |
| 237 | } |
| 238 | |
| 239 | write_file( |
| 240 | name = "nogo_config", |
| 241 | out = "nogo_config.json", |
| 242 | content = [json.encode_indent(NOGO_CONFIG)], |
| 243 | ) |
| 244 | |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 245 | nogo( |
| 246 | name = "nogo", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame^] | 247 | config = ":nogo_config", |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 248 | visibility = ["//visibility:public"], |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame^] | 249 | deps = NOGO_PASSES, |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 250 | ) |