Skip to content

gocritic: invalid ruleguard rules do not cause a warning or error #4421

@autarch

Description

@autarch

Welcome

Description of the problem

If a ruleguard rule is invalid (but syntactically correct), it seems like golangci-lint does not error out. Here's an example rule:

func SomeRule(m dsl.Matcher) {
	m.Match("$foo").
		Where(m["foo"].Text.Matches("x")).
		Where(m["foo"].Text.Matches("y")).
		Report("x and y")
}

If this is run against ruleguard directly, it will complain: ruleguard: load rules: parse rules file: irconv error: ./gorules/rule.go:7: Where() can't be repeated

However, if run via golangci-lint run, there is no error and the process exits 0.

Note that this is not the same as #1565, which is about syntax errors in the rules. In this case, the rules are valid Go but the use of the dsl API is not valid.

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.56.2 built with go1.22.0 from 58a724a0 on 2024-02-15T18:01:51Z

Configuration

linters:
  disable-all: true
  enable:
    - gocritic

  gocritic:
    enabled-checks:
      - ruleguard
    settings:
      ruleguard:
        rules: "${configDir}/gorules/*.go"

Go environment

$ go version && go env
go version go1.20.10 linux/amd64
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/autarch/.cache/go-build"
GOENV="/home/autarch/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/autarch/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/autarch/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.10"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/autarch/tmp/rg/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build548721329=/tmp/go-build -gno-record-gcc-switches"

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /home/autarch/tmp/rg /home/autarch/tmp /home/autarch /home /] 
INFO [config_reader] Used config file .golangci.yml 
INFO [lintersdb] Active 1 linters: [gocritic]     
INFO [loader] Go packages loading at mode 575 (imports|name|types_sizes|compiled_files|deps|exports_file|files) took 103.777308ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 106.514µs 
INFO [linters_context/goanalysis] analyzers took 5.572402ms with top 10 stages: gocritic: 5.567661ms, typecheck: 4.741µs 
INFO [runner] processing took 2.921µs with stages: max_from_linter: 570ns, nolint: 347ns, max_same_issues: 307ns, cgo: 239ns, identifier_marker: 237ns, skip_dirs: 163ns, autogenerated_exclude: 163ns, skip_files: 120ns, exclude-rules: 106ns, source_code: 105ns, fixer: 105ns, path_prettifier: 103ns, filename_unadjuster: 102ns, path_shortener: 36ns, severity-rules: 33ns, exclude: 32ns, diff: 31ns, sort_results: 31ns, max_per_file_from_linter: 31ns, uniq_by_line: 30ns, path_prefixer: 30ns 
INFO [runner] linters took 177.609003ms with stages: goanalysis_metalinter: 177.583661ms 
INFO File cache stats: 0 entries of total size 0B 
INFO Memory: 4 samples, avg is 47.3MB, max is 66.1MB 
INFO Execution took 285.113086ms                  

A minimal reproducible example or link to a public repository

https://github.com/autarch/golangci-lint-ruleguard-issue

Validation

  • Yes, I've included all information above (version, config, etc.).

Metadata

Metadata

Assignees

No one assigned

    Labels

    dependenciesRelates to an upstream dependencyquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions