Skip to content

Commit

Permalink
Skip linters early if affected package is not imported
Browse files Browse the repository at this point in the history
Skip the ioreadall, slowg and timeafter linters early if the respective
packages they check for (i.e. io, slog/log, golang.org/x/exp/slog and
time) is not imported by the package that is currently analyzed. This
should reduce linter run time a bit on packages that don't import any of
the checked packages.

Signed-off-by: Tobias Klauser <[email protected]>
  • Loading branch information
tklauser committed Jan 10, 2025
1 parent 971298c commit 6318c5a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions analysisutil/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

// Package analysisutil defines helper functions used by more than one linters.
package analysisutil

import "go/types"

// ImportsPackage reports whether path is imported by pkg.
//
// Copied from
// golang.org/x/tools/go/analysis/passes/internal/analysisutil.Imports
func ImportsPackage(pkg *types.Package, path string) bool {
for _, imp := range pkg.Imports() {
if imp.Path() == path {
return true
}
}
return false
}
6 changes: 6 additions & 0 deletions ioreadall/io_readall.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"

"github.com/cilium/linters/analysisutil"
)

const (
Expand All @@ -37,6 +39,10 @@ func init() {
}

func run(pass *analysis.Pass) (interface{}, error) {
if !analysisutil.ImportsPackage(pass.Pkg, "io") {
return nil, nil // doesn't directly import io package
}

inspct, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
Expand Down
7 changes: 7 additions & 0 deletions slowg/slowg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/types/typeutil"

"github.com/cilium/linters/analysisutil"
)

// Analyzer implements an analysis function that checks for inappropriate use
Expand All @@ -26,6 +28,11 @@ var Analyzer = &analysis.Analyzer{
}

func run(pass *analysis.Pass) (any, error) {
if !analysisutil.ImportsPackage(pass.Pkg, "log/slog") &&
!analysisutil.ImportsPackage(pass.Pkg, "golang.org/x/exp/slog") {
return nil, nil // doesn't directly import slog package
}

inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("require analyzer of type *inspector.Inspector")
Expand Down
6 changes: 6 additions & 0 deletions timeafter/time_after.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"

"github.com/cilium/linters/analysisutil"
)

const (
Expand Down Expand Up @@ -46,6 +48,10 @@ func (v visitor) Visit(node ast.Node) ast.Visitor {
}

func run(pass *analysis.Pass) (interface{}, error) {
if !analysisutil.ImportsPackage(pass.Pkg, "time") {
return nil, nil // doesn't directly import time package
}

inspct, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
Expand Down

0 comments on commit 6318c5a

Please sign in to comment.