Skip to content

Commit 314d236

Browse files
Abhijeet Vabhijeetviswa
authored andcommitted
fix staticcheck lint errors
1 parent d72dcd2 commit 314d236

35 files changed

Lines changed: 113 additions & 122 deletions

.github/workflows/checks.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,18 @@ jobs:
8888
- name: "gofmt"
8989
run: |
9090
make fmtcheck
91+
92+
golangci-lint:
93+
name: "linting"
94+
runs-on: ubuntu-latest
95+
steps:
96+
- name: "Fetch source code"
97+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
98+
- name: Install Go
99+
uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0
100+
with:
101+
go-version-file: go.mod
102+
- name: Run golangci-lint
103+
uses: golangci/golangci-lint-action@1481404843c368bc19ca9406f87d6e0fc97bdcfd # v7.0.0
104+
with:
105+
args: --enable-only=staticcheck

cmd/hcldec/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"bytes"
88
"encoding/json"
99
"fmt"
10-
"io/ioutil"
10+
"io"
1111
"os"
1212
"strings"
1313

@@ -18,7 +18,7 @@ import (
1818
"github.com/zclconf/go-cty/cty"
1919
"github.com/zclconf/go-cty/cty/function"
2020
ctyjson "github.com/zclconf/go-cty/cty/json"
21-
"golang.org/x/crypto/ssh/terminal"
21+
"golang.org/x/term"
2222
)
2323

2424
const versionStr = "0.0.1-dev"
@@ -57,8 +57,8 @@ func main() {
5757

5858
switch *diagsFormat {
5959
case "":
60-
color := terminal.IsTerminal(int(os.Stderr.Fd()))
61-
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
60+
color := term.IsTerminal(int(os.Stderr.Fd()))
61+
w, _, err := term.GetSize(int(os.Stdout.Fd()))
6262
if err != nil {
6363
w = 80
6464
}
@@ -140,7 +140,7 @@ func realmain(args []string) error {
140140
var bodies []hcl.Body
141141

142142
if len(args) == 0 {
143-
src, err := ioutil.ReadAll(os.Stdin)
143+
src, err := io.ReadAll(os.Stdin)
144144
if err != nil {
145145
return fmt.Errorf("failed to read stdin: %s", err)
146146
}

cmd/hclfmt/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
"errors"
99
"flag"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"os"
1313
"strings"
1414

1515
"github.com/hashicorp/hcl/v2"
1616
"github.com/hashicorp/hcl/v2/hclparse"
1717
"github.com/hashicorp/hcl/v2/hclwrite"
18-
"golang.org/x/crypto/ssh/terminal"
18+
"golang.org/x/term"
1919
)
2020

2121
const versionStr = "0.0.1-dev"
@@ -33,8 +33,8 @@ var checkErrs = false
3333
var changed []string
3434

3535
func init() {
36-
color := terminal.IsTerminal(int(os.Stderr.Fd()))
37-
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
36+
color := term.IsTerminal(int(os.Stderr.Fd()))
37+
w, _, err := term.GetSize(int(os.Stdout.Fd()))
3838
if err != nil {
3939
w = 80
4040
}
@@ -109,15 +109,15 @@ func processFiles() error {
109109

110110
func processFile(fn string, in *os.File) error {
111111
var err error
112-
var hasLocalChanges bool = false
112+
hasLocalChanges := false
113113
if in == nil {
114114
in, err = os.Open(fn)
115115
if err != nil {
116116
return fmt.Errorf("failed to open %s: %s", fn, err)
117117
}
118118
}
119119

120-
inSrc, err := ioutil.ReadAll(in)
120+
inSrc, err := io.ReadAll(in)
121121
if err != nil {
122122
return fmt.Errorf("failed to read %s: %s", fn, err)
123123
}
@@ -140,7 +140,7 @@ func processFile(fn string, in *os.File) error {
140140

141141
if *overwrite {
142142
if hasLocalChanges {
143-
return ioutil.WriteFile(fn, outSrc, 0644)
143+
return os.WriteFile(fn, outSrc, 0644)
144144
} else {
145145
return nil
146146
}

cmd/hclspecsuite/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os"
99
"os/exec"
1010

11-
"golang.org/x/crypto/ssh/terminal"
11+
"golang.org/x/term"
1212

1313
"github.com/hashicorp/hcl/v2"
1414
"github.com/hashicorp/hcl/v2/hclparse"
@@ -35,8 +35,8 @@ func realMain(args []string) int {
3535

3636
parser := hclparse.NewParser()
3737

38-
color := terminal.IsTerminal(int(os.Stderr.Fd()))
39-
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
38+
color := term.IsTerminal(int(os.Stderr.Fd()))
39+
w, _, err := term.GetSize(int(os.Stdout.Fd()))
4040
if err != nil {
4141
w = 80
4242
}

cmd/hclspecsuite/runner.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"bytes"
88
"encoding/json"
99
"fmt"
10-
"io/ioutil"
1110
"os"
1211
"os/exec"
1312
"path/filepath"
@@ -39,7 +38,7 @@ func (r *Runner) Run() hcl.Diagnostics {
3938
func (r *Runner) runDir(dir string) hcl.Diagnostics {
4039
var diags hcl.Diagnostics
4140

42-
infos, err := ioutil.ReadDir(dir)
41+
infos, err := os.ReadDir(dir)
4342
if err != nil {
4443
diags = append(diags, &hcl.Diagnostic{
4544
Severity: hcl.DiagError,
@@ -110,7 +109,7 @@ func (r *Runner) runTest(filename string) hcl.Diagnostics {
110109
// though it'll actually be parsed by the hcldec child process, since that
111110
// way we can produce nice diagnostic messages if hcldec fails to process
112111
// the spec file.
113-
src, err := ioutil.ReadFile(specFilename)
112+
src, err := os.ReadFile(specFilename)
114113
if err == nil {
115114
r.parser.AddFile(specFilename, &hcl.File{
116115
Bytes: src,
@@ -149,7 +148,7 @@ func (r *Runner) runTestInput(specFilename, inputFilename string, tf *TestFile)
149148
// though it'll actually be parsed by the hcldec child process, since that
150149
// way we can produce nice diagnostic messages if hcldec fails to process
151150
// the input file.
152-
src, err := ioutil.ReadFile(inputFilename)
151+
src, err := os.ReadFile(inputFilename)
153152
if err == nil {
154153
r.parser.AddFile(inputFilename, &hcl.File{
155154
Bytes: src,

cmd/hclspecsuite/test_file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (r *Runner) LoadTestFile(filename string) (*TestFile, hcl.Diagnostics) {
9292
diags = diags.Append(&hcl.Diagnostic{
9393
Severity: hcl.DiagError,
9494
Summary: "Duplicate \"traversals\" block",
95-
Detail: fmt.Sprintf("Only one traversals block is expected."),
95+
Detail: "Only one traversals block is expected.",
9696
Subject: &block.TypeRange,
9797
})
9898
continue
@@ -110,7 +110,7 @@ func (r *Runner) LoadTestFile(filename string) (*TestFile, hcl.Diagnostics) {
110110
diags = diags.Append(&hcl.Diagnostic{
111111
Severity: hcl.DiagError,
112112
Summary: "Duplicate \"diagnostics\" block",
113-
Detail: fmt.Sprintf("Only one diagnostics block is expected."),
113+
Detail: "Only one diagnostics block is expected.",
114114
Subject: &block.TypeRange,
115115
})
116116
continue

diagnostic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ func (d *Diagnostic) Error() string {
106106
// APIs that normally deal in vanilla Go errors.
107107
func (d Diagnostics) Error() string {
108108
count := len(d)
109-
switch {
110-
case count == 0:
109+
switch count {
110+
case 0:
111111
return "no diagnostics"
112-
case count == 1:
112+
case 1:
113113
return d[0].Error()
114114
default:
115115
return fmt.Sprintf("%s, and %d other diagnostic(s)", d[0].Error(), count-1)

ext/dynblock/variables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (n WalkVariablesNode) Visit(schema *hcl.BodySchema) (vars []hcl.Traversal,
8888
_, inherited = n.it.Inherited[traversal.RootName()]
8989
}
9090

91-
if !(ours || inherited) {
91+
if !ours && !inherited {
9292
vars = append(vars, traversal)
9393
}
9494
}
@@ -134,7 +134,7 @@ func (n WalkVariablesNode) Visit(schema *hcl.BodySchema) (vars []hcl.Traversal,
134134
ours := traversal.RootName() == iteratorName
135135
_, inherited := blockIt.Inherited[traversal.RootName()]
136136

137-
if !(ours || inherited) {
137+
if !ours && !inherited {
138138
vars = append(vars, traversal)
139139
}
140140
}

gohcl/decode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func decodeBodyToStruct(body hcl.Body, ctx *hcl.EvalContext, val reflect.Value)
195195
case isSlice:
196196
elemType := ty
197197
if isPtr {
198-
elemType = reflect.PtrTo(ty)
198+
elemType = reflect.PointerTo(ty)
199199
}
200200
sli := val.Field(fieldIdx)
201201
if sli.IsNil() {

gohcl/decode_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ func TestDecodeBody(t *testing.T) {
105105
}
106106

107107
nameVal, _ := wne.Name.Value(nil)
108-
if !nameVal.IsNull() {
109-
return false
110-
}
111-
112-
return true
108+
return nameVal.IsNull()
113109
},
114110
0,
115111
},
@@ -133,11 +129,8 @@ func TestDecodeBody(t *testing.T) {
133129
}
134130

135131
nameVal, _ := wne.Name.Value(nil)
136-
if !nameVal.Equals(cty.StringVal("Ermintrude")).True() {
137-
return false
138-
}
139132

140-
return true
133+
return nameVal.Equals(cty.StringVal("Ermintrude")).True()
141134
},
142135
0,
143136
},

0 commit comments

Comments
 (0)