Skip to content

Commit 33423dc

Browse files
committed
add tenv linter
1 parent 4989fc8 commit 33423dc

File tree

9 files changed

+121
-0
lines changed

9 files changed

+121
-0
lines changed

.golangci.example.yml

+4
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ linters-settings:
590590
name: true
591591
begin: true
592592

593+
tenv:
594+
# The following configurations enable checks prior to Go 1.17.
595+
f: false
596+
593597
unparam:
594598
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
595599
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ require (
6767
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
6868
github.com/shirou/gopsutil/v3 v3.21.7
6969
github.com/sirupsen/logrus v1.8.1
70+
github.com/sivchari/tenv v1.0.2
7071
github.com/sonatard/noctx v0.0.1
7172
github.com/sourcegraph/go-diff v0.6.1
7273
github.com/spf13/cobra v1.2.1

go.sum

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ type LintersSettings struct {
129129
Tagliatelle TagliatelleSettings
130130
Testpackage TestpackageSettings
131131
Thelper ThelperSettings
132+
Tenv TenvSettings
132133
Unparam UnparamSettings
133134
Unused StaticCheckSettings
134135
Varcheck VarCheckSettings
@@ -447,6 +448,10 @@ type ThelperSettings struct {
447448
} `mapstructure:"tb"`
448449
}
449450

451+
type TenvSettings struct {
452+
Force bool `mapstructure:"f"`
453+
}
454+
450455
type UnparamSettings struct {
451456
CheckExported bool `mapstructure:"check-exported"`
452457
Algo string

pkg/golinters/tenv.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package golinters
2+
3+
import (
4+
"github.com/sivchari/tenv"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
)
10+
11+
func NewTenv(settings *config.TenvSettings) *goanalysis.Linter {
12+
a := tenv.Analyzer
13+
14+
analyzers := []*analysis.Analyzer{
15+
a,
16+
}
17+
18+
var cfg map[string]map[string]interface{}
19+
if settings != nil {
20+
cfg = map[string]map[string]interface{}{
21+
a.Name: {
22+
tenv.F: settings.Force,
23+
},
24+
}
25+
}
26+
27+
return goanalysis.NewLinter(
28+
a.Name,
29+
"tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17",
30+
analyzers,
31+
cfg,
32+
).WithLoadMode(goanalysis.LoadModeSyntax)
33+
}

pkg/lint/lintersdb/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
105105
var exhaustiveStructCfg *config.ExhaustiveStructSettings
106106
var errorlintCfg *config.ErrorLintSettings
107107
var thelperCfg *config.ThelperSettings
108+
var tenvCfg *config.TenvSettings
108109
var predeclaredCfg *config.PredeclaredSettings
109110
var ifshortCfg *config.IfshortSettings
110111
var reviveCfg *config.ReviveSettings
@@ -126,6 +127,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
126127
exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct
127128
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
128129
thelperCfg = &m.cfg.LintersSettings.Thelper
130+
tenvCfg = &m.cfg.LintersSettings.Tenv
129131
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
130132
ifshortCfg = &m.cfg.LintersSettings.Ifshort
131133
reviveCfg = &m.cfg.LintersSettings.Revive
@@ -506,6 +508,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
506508
WithLoadForGoAnalysis().
507509
WithURL("https://github.com/Antonboom/errname").
508510
WithSince("v1.42.0"),
511+
linter.NewConfig(golinters.NewTenv(tenvCfg)).
512+
WithSince("v1.43.0").
513+
WithPresets(linter.PresetPerformance, linter.PresetStyle).
514+
WithLoadForGoAnalysis().
515+
WithURL("https://github.com/sivchari/tenv"),
509516

510517
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
511518
linter.NewConfig(golinters.NewNoLintLint()).

test/testdata/configs/tenv.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
tenv:
3+
f: true

test/testdata/tenv_force_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// args: -Etenv
2+
package testdata
3+
4+
import (
5+
"os"
6+
)
7+
8+
var (
9+
e = os.Setenv("a", "b") // OK
10+
)
11+
12+
func setup() {
13+
os.Setenv("a", "b") // OK
14+
err := os.Setenv("a", "b") // OK
15+
if err != nil {
16+
_ = err
17+
}
18+
}
19+
20+
func TestF() {
21+
os.Setenv("a", "b") // OK
22+
if err := os.Setenv("a", "b"); err != nil { // OK
23+
_ = err
24+
}
25+
}

test/testdata/tenv_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// args: -Etenv
2+
// config_path: testdata/configs/tenv.yml
3+
package testdata
4+
5+
import (
6+
"os"
7+
)
8+
9+
var (
10+
e = os.Setenv("a", "b") // ERROR "variable e is not using t.Setenv"
11+
)
12+
13+
func setup() {
14+
os.Setenv("a", "b") // ERROR "func setup is not using t.Setenv"
15+
err := os.Setenv("a", "b") // ERROR "func setup is not using t.Setenv"
16+
if err != nil {
17+
_ = err
18+
}
19+
}
20+
21+
func TestF() {
22+
os.Setenv("a", "b") // ERROR "func TestF is not using t.Setenv"
23+
if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using t.Setenv"
24+
_ = err
25+
}
26+
}

0 commit comments

Comments
 (0)