Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip linters early if affected package is not imported #54

Merged
merged 1 commit into from
Jan 14, 2025
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
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
Loading