Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions core/quality/adapter/src/severity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ mod tests {
);
}

#[test]
fn staticcheck_sa_is_an_error_and_style_a_warning() {
let level = |rule| resolve(TOOL_SEVERITIES, "staticcheck", rule, ToolSeverity::Info);
assert_eq!(level("SA4006"), ToolSeverity::Error);
assert_eq!(level("S1000"), ToolSeverity::Warning);
assert_eq!(level("ST1005"), ToolSeverity::Warning);
assert_eq!(level("U1000"), ToolSeverity::Warning);
assert_eq!(
resolve(
TOOL_SEVERITIES,
"staticcheck",
"QF1001",
ToolSeverity::Error
),
ToolSeverity::Info
);
}

#[test]
fn default_applies_when_no_prefix_matches() {
let table = [(
Expand Down
1 change: 1 addition & 0 deletions core/quality/adapter/src/severity_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ pub const TOOL_SEVERITIES: &[(&str, crate::severity::ToolSeverityMap)] = &[
categories: &[
("QF", crate::ToolSeverity::Info),
("S", crate::ToolSeverity::Warning),
("SA", crate::ToolSeverity::Error),
("U1000", crate::ToolSeverity::Warning),
],
},
Expand Down
12 changes: 9 additions & 3 deletions core/quality/testdata/real_result_cases.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,21 @@ REAL_RESULT_CASES = [
_go_case("govet", "lint", "deep", "//go/tests/fixtures/gotype:deep", ["govet"], ["no export data", "could not import"]),
_go_case("staticcheck", "lint", "clean", "//go/tests/fixtures/staticcheck:clean", ["staticcheck"], ["SA4017"]),
_go_case("staticcheck", "lint", "dirty", "//go/tests/fixtures/staticcheck:dirty", ["SA4017", "double", "U1000", "unusedHelper"], []),
_go_case("staticcheck", "lint", "standard_profile", "//go/tests/fixtures/staticcheck:strict_only_standard", ["staticcheck"], ["ST1003"]),
_go_case("staticcheck", "lint", "strict_profile", "//go/tests/fixtures/staticcheck:strict_only_strict", ["ST1003", "HttpServer"], []),
_go_case("staticcheck", "lint", "initialisms", "//go/tests/fixtures/staticcheck:strict_only_initialisms", ["staticcheck"], ["ST1003"]),
_go_case("staticcheck", "lint", "standard_profile", "//go/tests/fixtures/staticcheck:initialisms_standard", ["staticcheck"], ["ST1003"]),
_go_case("staticcheck", "lint", "strict_profile", "//go/tests/fixtures/staticcheck:initialisms_strict", ["staticcheck"], ["ST1003"]),
_go_case("staticcheck", "lint", "opt_in", "//go/tests/fixtures/staticcheck:initialisms_opt_in", ["ST1003", "HttpServer"], []),
_go_case("staticcheck", "lint", "initialisms", "//go/tests/fixtures/staticcheck:initialisms_custom", ["staticcheck"], ["ST1003"]),
_go_case("staticcheck", "lint", "unused_value", "//go/tests/fixtures/staticcheck:unused_value", ["SA4006", "this value of n is never used"], []),
_go_case("govet", "lint", "shadow", "//go/tests/fixtures/govet:shadowed", ["declaration of"], []),
_go_case("govet", "lint", "shadow_excluded", "//go/tests/fixtures/govet:shadowed_excluded", ["govet"], ["declaration of"]),
_go_case("govet", "lint", "shadow_off", "//go/tests/fixtures/govet:shadowed", ["govet"], ["declaration of"], config = "shared_standard"),
_go_case("govet", "lint", "forvar", "//go/tests/fixtures/govet:loopvar", ["copying variable is unneeded"], []),
_go_case("govet", "lint", "forvar_off", "//go/tests/fixtures/govet:loopvar", ["govet"], ["copying variable is unneeded"], config = "shared_standard"),
_go_case("errcheck", "lint", "blank", "//go/tests/fixtures/errcheck:blank", ["unchecked error"], []),
_go_case("errcheck", "lint", "blank_excluded", "//go/tests/fixtures/errcheck:blank_excluded", ["errcheck"], ["unchecked error"]),
_go_case("errcheck", "lint", "blank_off", "//go/tests/fixtures/errcheck:blank", ["errcheck"], ["unchecked error"], config = "shared_standard"),
_go_case("errcheck", "lint", "assert", "//go/tests/fixtures/errcheck:assert", ["unchecked error", "assert.go"], []),
_go_case("errcheck", "lint", "assert_off", "//go/tests/fixtures/errcheck:assert", ["errcheck"], ["unchecked error"], config = "shared_standard"),
_go_case("golint", "lint", "clean", "//go/tests/fixtures/golint:clean", ["errcheck", "govet", "staticcheck"], ["unchecked error", "non-constant format string", "SA4017"]),
_go_case("golint", "lint", "dirty", "//go/tests/fixtures/golint:app", ["unchecked error", "non-constant format string", "app.go", "SA4017", "tagged_on.go"], ["gen.go"]),
_go_case("govet", "lint", "override", "//go/tests/fixtures/errcheck:dirty", ["unchecked error"], [], config = "tool_overrides"),
Expand Down
9 changes: 7 additions & 2 deletions core/quality/tiers/gopls/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ fn main() {
.into_iter()
.chain(quoted_names(staticcheck_all))
{
analyses.insert(name, false);
analyses.insert(name.trim_start_matches('-').to_owned(), false);
}
// A "-NAME" entry turns a check off; gopls must say so, since its
// staticcheck switch otherwise turns on every check.
for name in quoted_names(govet)
.into_iter()
.chain(quoted_names(staticcheck))
{
analyses.insert(name, true);
match name.strip_prefix('-') {
Some(off) => analyses.insert(off.to_owned(), false),
None => analyses.insert(name, true),
};
}
println!("{{");
println!(" \"ui.diagnostic.staticcheck\": true,");
Expand Down
1 change: 1 addition & 0 deletions core/quality/tiers/gopls/gopls_standard.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"defers": true,
"directive": true,
"errorsas": true,
"forvar": false,
"framepointer": true,
"hostport": true,
"httpresponse": true,
Expand Down
17 changes: 9 additions & 8 deletions core/quality/tiers/gopls/gopls_strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,32 @@
"SA6006": true,
"SA9001": true,
"SA9002": true,
"SA9003": true,
"SA9003": false,
"SA9004": true,
"SA9005": true,
"SA9006": true,
"SA9007": true,
"SA9008": true,
"SA9009": true,
"SA9010": true,
"ST1000": true,
"ST1000": false,
"ST1001": true,
"ST1003": true,
"ST1003": false,
"ST1005": true,
"ST1006": true,
"ST1008": true,
"ST1011": true,
"ST1012": true,
"ST1013": true,
"ST1015": true,
"ST1016": true,
"ST1016": false,
"ST1017": true,
"ST1018": true,
"ST1019": true,
"ST1020": true,
"ST1021": true,
"ST1022": true,
"ST1023": true,
"ST1020": false,
"ST1021": false,
"ST1022": false,
"ST1023": false,
"U1000": true,
"appends": true,
"asmdecl": true,
Expand All @@ -163,6 +163,7 @@
"defers": true,
"directive": true,
"errorsas": true,
"forvar": true,
"framepointer": true,
"hostport": true,
"httpresponse": true,
Expand Down
1 change: 1 addition & 0 deletions core/quality/tiers/govet/strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"defers": {},
"directive": {},
"errorsas": {},
"forvar": {},
"framepointer": {},
"hostport": {},
"httpresponse": {},
Expand Down
16 changes: 8 additions & 8 deletions core/quality/tiers/staticcheck/strict.conf
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,31 @@ checks = [
"SA6006",
"SA9001",
"SA9002",
"SA9003",
"SA9004",
"SA9005",
"SA9006",
"SA9007",
"SA9008",
"SA9009",
"SA9010",
"ST1000",
"ST1001",
"ST1003",
"ST1005",
"ST1006",
"ST1008",
"ST1011",
"ST1012",
"ST1013",
"ST1015",
"ST1016",
"ST1017",
"ST1018",
"ST1019",
"ST1020",
"ST1021",
"ST1022",
"ST1023",
"U1000",
"-SA9003",
"-ST1000",
"-ST1003",
"-ST1016",
"-ST1020",
"-ST1021",
"-ST1022",
"-ST1023",
]
3 changes: 2 additions & 1 deletion core/quality/tools_registry.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5433,6 +5433,7 @@ FAMILY_TOOL_ROWS = json.decode(r"""{
"categories": {
"QF": "info",
"S": "warning",
"SA": "error",
"U1000": "warning"
},
"default": null
Expand All @@ -5453,7 +5454,7 @@ FAMILY_TOOL_ROWS = json.decode(r"""{
"windows_arm64",
"windows_x86_64"
],
"state": "known",
"state": "supported",
"tool": "staticcheck",
"transport": "batched"
}
Expand Down
2 changes: 2 additions & 0 deletions docs/cli/commands/quality.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ The `actionlint`, `ruff`, `terraform`, `hadolint`, `opa`, `regal`, `ormolu` and

`spotbugs` runs at default effort in standard and max effort in strict, and the tier's filter XML sets the confidence threshold. A bound `spotbugs_config` is used as an `-exclude` filter with `-low`, so it exposes every low-confidence finding.

`govet`, `errcheck` and `staticcheck` take their tier from `core/quality/tiers/`. Standard runs the `go vet` defaults, errcheck, and staticcheck's default checks. Strict adds the vet analyzers `forvar`, `nilness`, `shadow` and `unusedwrite`. It also makes errcheck report `_ = f()`, unchecked type assertions such as `v.(string)`, and errors from `Close` and `fmt.Fprint*`. staticcheck runs the same checks in both tiers and leaves out `SA9003`, `ST1000`, `ST1003`, `ST1016` and `ST1020` to `ST1023`. Its `SA` findings are errors; `S`, `ST` and `U1000` findings are warnings.

### JVM and .NET

| Tool | Checks | Not on |
Expand Down
2 changes: 1 addition & 1 deletion go/private/quality/go.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

load("@rules_go//go:def.bzl", "GoArchive")
load("//core/quality:family_hooks.bzl", "without")
load("//go/private:go_analysis.bzl", "GO_TOOLCHAIN_TYPE", "GoAnalysisInfo", "go_analysis_aspect", "go_lang_version")
load("//core/quality:real_core.bzl", "real_config", "real_context", "real_make_aspect", "real_make_probe_aspect")
load("//go/private:go_analysis.bzl", "GO_TOOLCHAIN_TYPE", "GoAnalysisInfo", "go_analysis_aspect", "go_lang_version")

TOOLS = {
"errcheck": {
Expand Down
2 changes: 1 addition & 1 deletion go/private/quality/go_tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ TOOLS = [
severity = severity(categories = {
"QF": "info",
"S": "warning",
"SA": "error",
"U1000": "warning",
}),
support = [
Expand All @@ -98,7 +99,6 @@ TOOLS = [
family = "go",
fix = "check_only",
platforms = ["linux_arm64", "linux_x86_64", "macos_arm64", "windows_arm64", "windows_x86_64"],
state = "known",
),
],
),
Expand Down
1 change: 1 addition & 0 deletions go/private/tools/analysis/govet/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ go_library(
"@org_golang_x_tools//go/analysis/passes/ifaceassert",
"@org_golang_x_tools//go/analysis/passes/loopclosure",
"@org_golang_x_tools//go/analysis/passes/lostcancel",
"@org_golang_x_tools//go/analysis/passes/modernize",
"@org_golang_x_tools//go/analysis/passes/nilfunc",
"@org_golang_x_tools//go/analysis/passes/nilness",
"@org_golang_x_tools//go/analysis/passes/printf",
Expand Down
3 changes: 3 additions & 0 deletions go/private/tools/analysis/govet/govet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"golang.org/x/tools/go/analysis/passes/ifaceassert"
"golang.org/x/tools/go/analysis/passes/loopclosure"
"golang.org/x/tools/go/analysis/passes/lostcancel"
"golang.org/x/tools/go/analysis/passes/modernize"
"golang.org/x/tools/go/analysis/passes/nilfunc"
"golang.org/x/tools/go/analysis/passes/nilness"
"golang.org/x/tools/go/analysis/passes/printf"
Expand Down Expand Up @@ -92,7 +93,9 @@ var defaults = []*analysis.Analyzer{
}

// strictOnly are the analyzers the strict profile adds to the defaults.
// forvar is x/tools' successor to the copyloopvar linter.
var strictOnly = []*analysis.Analyzer{
modernize.ForVarAnalyzer,
nilness.Analyzer,
shadow.Analyzer,
unusedwrite.Analyzer,
Expand Down
6 changes: 3 additions & 3 deletions go/private/tools/analysis/govet/govet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ func TestStandardRunsTheDefaults(t *testing.T) {
if !has(active, "printf") {
t.Error("printf should run")
}
for _, name := range []string{"shadow", "nilness", "unusedwrite"} {
for _, name := range []string{"forvar", "shadow", "nilness", "unusedwrite"} {
if has(active, name) {
t.Errorf("%s must be off under standard", name)
}
}
}

func TestStrictAddsShadowNilnessUnusedwrite(t *testing.T) {
func TestStrictAddsForvarShadowNilnessUnusedwrite(t *testing.T) {
if err := configure([]string{"-profile=strict"}); err != nil {
t.Fatal(err)
}
for _, name := range []string{"printf", "shadow", "nilness", "unusedwrite"} {
for _, name := range []string{"printf", "forvar", "shadow", "nilness", "unusedwrite"} {
if !has(active, name) {
t.Errorf("%s should run under strict", name)
}
Expand Down
10 changes: 8 additions & 2 deletions go/private/tools/analysis/staticcheck/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ go_test(
name = "staticcheck_test",
size = "small",
srcs = ["checks_test.go"],
data = ["//core/quality/tiers/staticcheck:standard.conf"],
data = [
"//core/quality/tiers/staticcheck:standard.conf",
"//core/quality/tiers/staticcheck:strict.conf",
],
embed = [":staticcheck"],
env = {"STANDARD_CONF": "$(rootpath //core/quality/tiers/staticcheck:standard.conf)"},
env = {
"STANDARD_CONF": "$(rootpath //core/quality/tiers/staticcheck:standard.conf)",
"STRICT_CONF": "$(rootpath //core/quality/tiers/staticcheck:strict.conf)",
},
deps = ["@org_golang_x_tools//go/analysis"],
)
17 changes: 17 additions & 0 deletions go/private/tools/analysis/staticcheck/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,20 @@ func TestDefaultMatchesStandardProfile(t *testing.T) {
}
}
}

func TestStrictProfileIsAllButTheExcluded(t *testing.T) {
profile, err := loadConfig(filepath.Join(os.Getenv("TEST_SRCDIR"), os.Getenv("TEST_WORKSPACE"), os.Getenv("STRICT_CONF")))
if err != nil {
t.Fatal(err)
}
excluded := []string{"SA9003", "ST1000", "ST1003", "ST1016", "ST1020", "ST1021", "ST1022", "ST1023"}
var want []string
for _, a := range allAnalyzers() {
if !slices.Contains(excluded, a.Analyzer.Name) {
want = append(want, a.Analyzer.Name)
}
}
if got := names2(selectAnalyzers(profile)); !reflect.DeepEqual(got, want) {
t.Errorf("strict.conf checks:\n%v\nwant every check but %v:\n%v", got, excluded, want)
}
}
7 changes: 7 additions & 0 deletions go/tests/fixtures/errcheck/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ go_library(
tags = ["manual"],
)

go_library(
name = "assert",
srcs = ["assert.go"],
importpath = "rules_dx/go/tests/fixtures/errcheck/assert",
tags = ["manual"],
)

errcheck_config(
name = "excludes",
src = "exclude.txt",
Expand Down
6 changes: 6 additions & 0 deletions go/tests/fixtures/errcheck/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package errcheck

// Name returns v as a string and panics when it is not one.
func Name(v any) string {
return v.(string)
}
7 changes: 7 additions & 0 deletions go/tests/fixtures/govet/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ go_library(
tags = ["manual"],
)

go_library(
name = "loopvar",
srcs = ["loopvar.go"],
importpath = "rules_dx/go/tests/fixtures/govet/loopvar",
tags = ["manual"],
)

govet_config(
name = "excludes",
src = "govet.json",
Expand Down
11 changes: 11 additions & 0 deletions go/tests/fixtures/govet/loopvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package govet

// Sum adds up values.
func Sum(values []int) int {
total := 0
for _, v := range values {
v := v
total += v
}
return total
}
Loading
Loading