blob: 98a15636d02d8907c34f429bf52d1e74d8cb7267 [file] [log] [blame]
Tim Windelschmidte2b28652024-04-12 16:49:16 +02001load("@bazel_skylib//rules:write_file.bzl", "write_file")
Serge Bazanski0ef96292021-05-21 15:41:32 +02002load("@io_bazel_rules_go//go:def.bzl", "nogo")
Tim Windelschmidt67f45f82025-07-29 21:45:00 +02003load("//build/analysis/staticcheck:def.bzl", ALL_STATICCHECK_ANALYZERS = "ANALYZER_NAMES", filtered_staticcheck_analyzers = "filtered_analyzers")
Tim Windelschmidta0684402025-02-25 11:32:40 +01004load(":def.bzl", "build_nogo_config", "exclude_from_checks", "exclude_from_external")
Serge Bazanski0ef96292021-05-21 15:41:32 +02005
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",
Tim Windelschmidte2b28652024-04-12 16:49:16 +020069 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/returnerrcheck",
Tim Windelschmidte2b28652024-04-12 16:49:16 +020070 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/unconvert",
71]
72
73# Combine all staticcheck analyzers with a list
74# of all globally disabled staticcheck analyzers
75# and append them to the nogo passes.
Tim Windelschmidt67f45f82025-07-29 21:45:00 +020076NOGO_PASSES += filtered_staticcheck_analyzers([
77 "ST1000", # at least one file in a package should have a package comment
78 "ST1003", # should not use ALL_CAPS in Go names; use CamelCase instead
79 "QF1006", # could lift into loop condition
80 "QF1003", # could use tagged switch
81 "QF1008", # Omit embedded fields from selector expression
Tim Windelschmidte2b28652024-04-12 16:49:16 +020082])
83
84NOGO_PASSES += [
85 # This analyzer ensures that all comment lines are <= 80 characters long
86 # in Go source. This is in line with general practices around the Go
87 # community, where code lines can be as long as needed (and is expected
88 # to be soft-reflowable by text editors), but comments are kept at a
89 # 'standard' 80 characters long, as prose within comment blocks does not
90 # soft-reflow well.
91 "@com_github_corverroos_commentwrap//:go_default_library",
Tim Windelschmidtbc6c10e2025-07-29 17:33:45 +020092 "//build/analysis/gocheckcompilerdirectives",
Tim Windelschmidte2b28652024-04-12 16:49:16 +020093 "//build/analysis/noioutil",
94 "//build/analysis/importsort",
Tim Windelschmidt99e15112025-02-05 17:38:16 +010095 "//build/analysis/gofmt",
Tim Windelschmidt6d33a432025-02-04 14:34:25 +010096 "//build/analysis/haslicense",
Tim Windelschmidte2b28652024-04-12 16:49:16 +020097]
98
Tim Windelschmidta0684402025-02-25 11:32:40 +010099NOGO_CONFIG = [
100 exclude_from_checks("dev_gvisor_gvisor", "shift", "unsafeptr"),
101 exclude_from_checks("com_github_modern_go_reflect2", "unsafeptr"),
102 exclude_from_checks("io_k8s_sigs_structured_merge_diff", "unsafeptr"),
103 exclude_from_checks("com_github_go_delve_delve", "unsafeptr"),
104 exclude_from_checks("com_github_mailru_easyjson/jlexer", "unsafeptr"),
105 exclude_from_checks("com_github_cilium_ebpf", "unsafeptr"),
106 exclude_from_checks("net_starlark_go", "unsafeptr"),
107 exclude_from_checks("org_golang_x_sys", "unsafeptr"),
108 exclude_from_checks("com_github_pingcap_tidb_parser", "unsafeptr"),
109 exclude_from_checks("com_github_dennwc_btrfs", "unsafeptr"),
110 exclude_from_checks("com_github_u_root_uio", "deepequalerrors"),
111 exclude_from_checks("com_github_sbezverk_nfproxy", "defers"),
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200112
Tim Windelschmidta0684402025-02-25 11:32:40 +0100113 # All analyzers that should be disabled for external, generated or cgo code.
114 exclude_from_external(ALL_STATICCHECK_ANALYZERS),
115 exclude_from_external([
Tim Windelschmidta0684402025-02-25 11:32:40 +0100116 "commentwrap",
Tim Windelschmidt09857822025-07-29 17:34:59 +0200117 "gocheckcompilerdirectives",
118 "noioutil",
Tim Windelschmidta0684402025-02-25 11:32:40 +0100119 "importsort",
Tim Windelschmidt09857822025-07-29 17:34:59 +0200120 "gofmt",
121 "haslicense",
122 ]),
123 exclude_from_external([
124 "exclude_files",
Tim Windelschmidta0684402025-02-25 11:32:40 +0100125 "unreachable",
126 "unusedwrite",
127 "composites",
128 "stdmethods",
129 "reflectvaluecompare",
130 "unconvert",
131 "errwrap",
132 "ruleguard",
133 "returnerrcheck",
134 "hash",
135 "errcmp",
Tim Windelschmidta0684402025-02-25 11:32:40 +0100136 "unparam",
Tim Windelschmidta0684402025-02-25 11:32:40 +0100137 "nilness",
138 "printf",
Tim Windelschmidta0684402025-02-25 11:32:40 +0100139 "copylocks",
Tim Windelschmidta0684402025-02-25 11:32:40 +0100140 "lostcancel",
141 ]),
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200142
Tim Windelschmidta0684402025-02-25 11:32:40 +0100143 # Additional custom entries.
144 {
145 "unsafeptr": {
146 "exclude_files": {
147 "sqlite3.*go": "third_party",
148 },
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200149 },
Tim Windelschmidta0684402025-02-25 11:32:40 +0100150 },
151]
Tim Windelschmidt156248b2025-01-10 00:27:45 +0100152
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200153write_file(
154 name = "nogo_config",
155 out = "nogo_config.json",
Tim Windelschmidta0684402025-02-25 11:32:40 +0100156 content = [json.encode_indent(build_nogo_config(NOGO_CONFIG))],
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200157)
158
Serge Bazanski0ef96292021-05-21 15:41:32 +0200159nogo(
160 name = "nogo",
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200161 config = ":nogo_config",
Serge Bazanski0ef96292021-05-21 15:41:32 +0200162 visibility = ["//visibility:public"],
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200163 deps = NOGO_PASSES,
Serge Bazanski0ef96292021-05-21 15:41:32 +0200164)