blob: 395c33f555fa20aa3420220bd7ffb477581abd80 [file] [log] [blame]
Tim Windelschmidte2b28652024-04-12 16:49:16 +02001load("@bazel_skylib//rules:write_file.bzl", "write_file")
Tim Windelschmidt156248b2025-01-10 00:27:45 +01002load("@com_github_sluongng_nogo_analyzer//staticcheck:def.bzl", ALL_STATICCHECK_ANALYZERS = "ANALYZERS", format_staticcheck_analyzers = "staticcheck_analyzers")
Serge Bazanski0ef96292021-05-21 15:41:32 +02003load("@io_bazel_rules_go//go:def.bzl", "nogo")
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.
76NOGO_PASSES += format_staticcheck_analyzers(ALL_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([
116 "exclude_files",
117 "commentwrap",
118 "importsort",
119 "unreachable",
120 "unusedwrite",
121 "composites",
122 "stdmethods",
123 "reflectvaluecompare",
124 "unconvert",
125 "errwrap",
126 "ruleguard",
127 "returnerrcheck",
128 "hash",
129 "errcmp",
130 "gofmt",
131 "unparam",
132 "haslicense",
133 "nilness",
134 "printf",
135 "gocheckcompilerdirectives",
136 "copylocks",
137 "noioutil",
138 "lostcancel",
139 ]),
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200140
Tim Windelschmidta0684402025-02-25 11:32:40 +0100141 # Additional custom entries.
142 {
143 "unsafeptr": {
144 "exclude_files": {
145 "sqlite3.*go": "third_party",
146 },
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200147 },
Tim Windelschmidta0684402025-02-25 11:32:40 +0100148 },
149]
Tim Windelschmidt156248b2025-01-10 00:27:45 +0100150
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200151write_file(
152 name = "nogo_config",
153 out = "nogo_config.json",
Tim Windelschmidta0684402025-02-25 11:32:40 +0100154 content = [json.encode_indent(build_nogo_config(NOGO_CONFIG))],
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200155)
156
Serge Bazanski0ef96292021-05-21 15:41:32 +0200157nogo(
158 name = "nogo",
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200159 config = ":nogo_config",
Serge Bazanski0ef96292021-05-21 15:41:32 +0200160 visibility = ["//visibility:public"],
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200161 deps = NOGO_PASSES,
Serge Bazanski0ef96292021-05-21 15:41:32 +0200162)