| load("@bazel_skylib//rules:write_file.bzl", "write_file") |
| load("@com_github_sluongng_nogo_analyzer//staticcheck:def.bzl", ALL_STATICCHECK_ANALYZERS = "ANALYZERS", format_staticcheck_analyzers = "staticcheck_analyzers") |
| load("@io_bazel_rules_go//go:def.bzl", "nogo") |
| load(":def.bzl", "build_nogo_config", "exclude_from_checks", "exclude_from_external") |
| |
| # NOGO_PASSES contains all enabled analyzers that nogo should execute. |
| NOGO_PASSES = [] |
| |
| # These deps enable the analyses equivalent to running `go vet`. |
| # Passing vet = True enables only a tiny subset of these (the ones |
| # that are always correct). |
| # You can see the what `go vet` does by running `go doc cmd/vet`. |
| NOGO_PASSES += [ |
| "@org_golang_x_tools//go/analysis/passes/appends", |
| "@org_golang_x_tools//go/analysis/passes/asmdecl", |
| "@org_golang_x_tools//go/analysis/passes/assign", |
| "@org_golang_x_tools//go/analysis/passes/atomic", |
| "@org_golang_x_tools//go/analysis/passes/atomicalign", |
| "@org_golang_x_tools//go/analysis/passes/bools", |
| "@org_golang_x_tools//go/analysis/passes/buildssa", |
| "@org_golang_x_tools//go/analysis/passes/buildtag", |
| # Disable cgocall because it fails processing com_github_mattn_go_sqlite3 before exclusions are applied |
| #"@org_golang_x_tools//go/analysis/passes/cgocall", |
| "@org_golang_x_tools//go/analysis/passes/composite", |
| "@org_golang_x_tools//go/analysis/passes/copylock", |
| "@org_golang_x_tools//go/analysis/passes/deepequalerrors", |
| "@org_golang_x_tools//go/analysis/passes/defers", |
| "@org_golang_x_tools//go/analysis/passes/directive", |
| "@org_golang_x_tools//go/analysis/passes/errorsas", |
| # Disabled as there is no real benefit from it. |
| #"@org_golang_x_tools//go/analysis/passes/fieldalignment", |
| "@org_golang_x_tools//go/analysis/passes/framepointer", |
| "@org_golang_x_tools//go/analysis/passes/httpmux", |
| "@org_golang_x_tools//go/analysis/passes/httpresponse", |
| "@org_golang_x_tools//go/analysis/passes/ifaceassert", |
| # Disabled because we are using Go 1.22 |
| # https://go.dev/blog/loopvar-preview |
| #"@org_golang_x_tools//go/analysis/passes/loopclosure", |
| "@org_golang_x_tools//go/analysis/passes/lostcancel", |
| "@org_golang_x_tools//go/analysis/passes/nilfunc", |
| "@org_golang_x_tools//go/analysis/passes/nilness", |
| "@org_golang_x_tools//go/analysis/passes/printf", |
| "@org_golang_x_tools//go/analysis/passes/reflectvaluecompare", |
| # Disabled because of too many false positives |
| # "@org_golang_x_tools//go/analysis/passes/shadow", |
| "@org_golang_x_tools//go/analysis/passes/shift", |
| "@org_golang_x_tools//go/analysis/passes/sigchanyzer", |
| "@org_golang_x_tools//go/analysis/passes/slog", |
| "@org_golang_x_tools//go/analysis/passes/sortslice", |
| "@org_golang_x_tools//go/analysis/passes/stdmethods", |
| "@org_golang_x_tools//go/analysis/passes/stringintconv", |
| "@org_golang_x_tools//go/analysis/passes/structtag", |
| "@org_golang_x_tools//go/analysis/passes/testinggoroutine", |
| "@org_golang_x_tools//go/analysis/passes/tests", |
| "@org_golang_x_tools//go/analysis/passes/timeformat", |
| "@org_golang_x_tools//go/analysis/passes/unmarshal", |
| "@org_golang_x_tools//go/analysis/passes/unreachable", |
| "@org_golang_x_tools//go/analysis/passes/unsafeptr", |
| "@org_golang_x_tools//go/analysis/passes/unusedresult", |
| "@org_golang_x_tools//go/analysis/passes/unusedwrite", |
| ] |
| |
| # Append some passes provided by CockroachDB. |
| NOGO_PASSES += [ |
| "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errcmp", |
| "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errwrap", |
| "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/hash", |
| "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/nilness", |
| "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/returnerrcheck", |
| "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/unconvert", |
| ] |
| |
| # Combine all staticcheck analyzers with a list |
| # of all globally disabled staticcheck analyzers |
| # and append them to the nogo passes. |
| NOGO_PASSES += format_staticcheck_analyzers(ALL_STATICCHECK_ANALYZERS + [ |
| "-ST1000", # at least one file in a package should have a package comment |
| "-ST1003", # should not use ALL_CAPS in Go names; use CamelCase instead |
| "-QF1006", # could lift into loop condition |
| "-QF1003", # could use tagged switch |
| "-QF1008", # Omit embedded fields from selector expression |
| ]) |
| |
| NOGO_PASSES += [ |
| # This analyzer ensures that all comment lines are <= 80 characters long |
| # in Go source. This is in line with general practices around the Go |
| # community, where code lines can be as long as needed (and is expected |
| # to be soft-reflowable by text editors), but comments are kept at a |
| # 'standard' 80 characters long, as prose within comment blocks does not |
| # soft-reflow well. |
| "@com_github_corverroos_commentwrap//:go_default_library", |
| "//build/analysis/gocheckcompilerdirectives", |
| "//build/analysis/noioutil", |
| "//build/analysis/importsort", |
| "//build/analysis/gofmt", |
| "//build/analysis/haslicense", |
| ] |
| |
| NOGO_CONFIG = [ |
| exclude_from_checks("dev_gvisor_gvisor", "shift", "unsafeptr"), |
| exclude_from_checks("com_github_modern_go_reflect2", "unsafeptr"), |
| exclude_from_checks("io_k8s_sigs_structured_merge_diff", "unsafeptr"), |
| exclude_from_checks("com_github_go_delve_delve", "unsafeptr"), |
| exclude_from_checks("com_github_mailru_easyjson/jlexer", "unsafeptr"), |
| exclude_from_checks("com_github_cilium_ebpf", "unsafeptr"), |
| exclude_from_checks("net_starlark_go", "unsafeptr"), |
| exclude_from_checks("org_golang_x_sys", "unsafeptr"), |
| exclude_from_checks("com_github_pingcap_tidb_parser", "unsafeptr"), |
| exclude_from_checks("com_github_dennwc_btrfs", "unsafeptr"), |
| exclude_from_checks("com_github_u_root_uio", "deepequalerrors"), |
| exclude_from_checks("com_github_sbezverk_nfproxy", "defers"), |
| |
| # All analyzers that should be disabled for external, generated or cgo code. |
| exclude_from_external(ALL_STATICCHECK_ANALYZERS), |
| exclude_from_external([ |
| "exclude_files", |
| "commentwrap", |
| "importsort", |
| "unreachable", |
| "unusedwrite", |
| "composites", |
| "stdmethods", |
| "reflectvaluecompare", |
| "unconvert", |
| "errwrap", |
| "ruleguard", |
| "returnerrcheck", |
| "hash", |
| "errcmp", |
| "gofmt", |
| "unparam", |
| "haslicense", |
| "nilness", |
| "printf", |
| "gocheckcompilerdirectives", |
| "copylocks", |
| "noioutil", |
| "lostcancel", |
| ]), |
| |
| # Additional custom entries. |
| { |
| "unsafeptr": { |
| "exclude_files": { |
| "sqlite3.*go": "third_party", |
| }, |
| }, |
| }, |
| ] |
| |
| write_file( |
| name = "nogo_config", |
| out = "nogo_config.json", |
| content = [json.encode_indent(build_nogo_config(NOGO_CONFIG))], |
| ) |
| |
| nogo( |
| name = "nogo", |
| config = ":nogo_config", |
| visibility = ["//visibility:public"], |
| deps = NOGO_PASSES, |
| ) |