| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 1 | load("@bazel_skylib//rules:write_file.bzl", "write_file") |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 2 | load("@com_github_sluongng_nogo_analyzer//staticcheck:def.bzl", ALL_STATICCHECK_ANALYZERS = "ANALYZERS", format_staticcheck_analyzers = "staticcheck_analyzers") |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 3 | load("@io_bazel_rules_go//go:def.bzl", "nogo") |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 4 | load(":def.bzl", "build_nogo_config", "exclude_from_checks", "exclude_from_external") |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 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", |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 66 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errwrap", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 67 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/hash", |
| 68 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/nilness", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 69 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/returnerrcheck", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 70 | "@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. |
| 76 | NOGO_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 Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 82 | ]) |
| 83 | |
| 84 | NOGO_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 Windelschmidt | bc6c10e | 2025-07-29 17:33:45 +0200 | [diff] [blame^] | 92 | "//build/analysis/gocheckcompilerdirectives", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 93 | "//build/analysis/noioutil", |
| 94 | "//build/analysis/importsort", |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 95 | "//build/analysis/gofmt", |
| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 96 | "//build/analysis/haslicense", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 97 | ] |
| 98 | |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 99 | NOGO_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 Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 112 | |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 113 | # 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 Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 140 | |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 141 | # Additional custom entries. |
| 142 | { |
| 143 | "unsafeptr": { |
| 144 | "exclude_files": { |
| 145 | "sqlite3.*go": "third_party", |
| 146 | }, |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 147 | }, |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 148 | }, |
| 149 | ] |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 150 | |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 151 | write_file( |
| 152 | name = "nogo_config", |
| 153 | out = "nogo_config.json", |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 154 | content = [json.encode_indent(build_nogo_config(NOGO_CONFIG))], |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 155 | ) |
| 156 | |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 157 | nogo( |
| 158 | name = "nogo", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 159 | config = ":nogo_config", |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 160 | visibility = ["//visibility:public"], |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 161 | deps = NOGO_PASSES, |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 162 | ) |