Skip to content

Commit

Permalink
[checks] Add govet
Browse files Browse the repository at this point in the history
Most of go vet's checks overlap with other linters we already
implemented support for, but the `copylocks` check in particular is not
enforced by the existing linters, so we should run that along with any
future analyzers we discover are not enforced by other lintesr.

Change-Id: Ie85f8085721b5d1006740d36a0c9bff7bba167b5
Reviewed-on: https://fuchsia-review.googlesource.com/c/shac-project/shac/+/925054
Fuchsia-Auto-Submit: Oliver Newman <olivernewman@google.com>
Reviewed-by: Anthony Fandrianto <atyfto@google.com>
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
  • Loading branch information
orn688 authored and CQ Bot committed Oct 2, 2023
1 parent 257dfa5 commit 1d1ca72
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
59 changes: 59 additions & 0 deletions checks/go.star
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,62 @@ def no_fork_without_lock(ctx):
col = int(match.groups[3]),
message = finding["message"],
)

def govet(
ctx,
# `go vet` has a lot of overlap with other linters, only include
# analyzers that aren't enforced by the other linters.
analyzers = [
"copylocks",
]):
"""Checks that exec.Command Start() and Run() aren't called directly.
Instead, callers should use the `execsupport` package, which provides appropriate
locks to make sure forks are safe.
Args:
ctx: A ctx instance.
analyzers: Names of analyzers to run (run `go tool vet help` to see all
options).
"""
output = ctx.os.exec(
[
"go",
"vet",
"-json",
] +
["-" + a for a in analyzers] +
["./..."],
env = go_env(),
).wait().stderr

# output is of the form:
# # pkg1
# {}
# # pkg2
# {
# ...
# }
findings_by_package = {}
current_package_lines = []
lines = output.splitlines()
for i, line in enumerate(lines):
if not line.startswith("# "):
current_package_lines.append(line)
if i + 1 < len(lines):
continue
if current_package_lines:
findings_by_package.update(json.decode("\n".join(current_package_lines)))
current_package_lines = []

for pkg_findings in findings_by_package.values():
for check_findings in pkg_findings.values():
for finding in check_findings:
match = ctx.re.match(r"^%s/(.+):(\d+):(\d+)$" % ctx.scm.root, finding["posn"])
ctx.emit.finding(
level = "error",
filepath = match.groups[1],
line = int(match.groups[2]),
col = int(match.groups[3]),
message = finding["message"],
)
3 changes: 2 additions & 1 deletion shac.star
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This file will evolve as new shac functionality is being added.

load("//checks/buildifier.star", "buildifier")
load("//checks/check_doc.star", "check_docs")
load("//checks/go.star", "gofmt", "gosec", "ineffassign", "no_fork_without_lock", "shadow", "staticcheck")
load("//checks/go.star", "gofmt", "gosec", "govet", "ineffassign", "no_fork_without_lock", "shadow", "staticcheck")
load("//checks/licenses.star", "check_license_headers")

def suggest_version_bump(ctx):
Expand Down Expand Up @@ -74,6 +74,7 @@ shac.register_check(check_docs)
shac.register_check(check_license_headers)
shac.register_check(gofmt)
shac.register_check(gosec)
shac.register_check(govet)
shac.register_check(ineffassign)
shac.register_check(new_todos)
shac.register_check(no_fork_without_lock)
Expand Down

0 comments on commit 1d1ca72

Please sign in to comment.