Skip to content

Commit 8911693

Browse files
committed
review fixes & go-critic tests improving
1 parent b112064 commit 8911693

File tree

9 files changed

+86
-63
lines changed

9 files changed

+86
-63
lines changed

pkg/golinters/gocritic.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (w *goCriticWrapper) init(settings *config.GoCriticSettings, logger logutil
100100

101101
settingsWrapper := newGoCriticSettingsWrapper(settings, logger)
102102
settingsWrapper.InferEnabledChecks()
103-
// NOTE(Antonboom): Validate must be after InferEnabledChecks, not before.
103+
// Validate must be after InferEnabledChecks, not before.
104104
// Because it uses gathered information about tags set and finally enabled checks.
105105
if err := settingsWrapper.Validate(); err != nil {
106106
logger.Fatalf("%s: invalid settings: %s", goCriticName, err)
@@ -158,11 +158,7 @@ func (w *goCriticWrapper) buildEnabledCheckers(linterCtx *gocriticlinter.Context
158158
return enabledCheckers, nil
159159
}
160160

161-
func runGocriticOnPackage(
162-
linterCtx *gocriticlinter.Context,
163-
checks []*gocriticlinter.Checker,
164-
files []*ast.File,
165-
) []result.Issue {
161+
func runGocriticOnPackage(linterCtx *gocriticlinter.Context, checks []*gocriticlinter.Checker, files []*ast.File) []result.Issue {
166162
var res []result.Issue
167163
for _, f := range files {
168164
filename := filepath.Base(linterCtx.FileSet.Position(f.Pos()).Filename)
@@ -214,7 +210,7 @@ func (w *goCriticWrapper) configureCheckerInfo(
214210
return nil
215211
}
216212

217-
// NOTE(ldez): lowercase info param keys here because golangci-lint's config parser lowercases all strings.
213+
// To lowercase info param keys here because golangci-lint's config parser lowercases all strings.
218214
infoParams := normalizeMap(info.Params)
219215
for k, p := range params {
220216
v, ok := infoParams[k]
@@ -274,12 +270,14 @@ type goCriticSettingsWrapper struct {
274270

275271
allCheckers []*gocriticlinter.CheckerInfo
276272

277-
allChecks map[string]struct{}
278-
allChecksLowerCased map[string]struct{}
279-
allChecksByTag map[string][]string
280-
allTagsSorted []string
273+
allChecks map[string]struct{}
274+
allChecksByTag map[string][]string
275+
allTagsSorted []string
276+
inferredEnabledChecks map[string]struct{}
281277

282-
inferredEnabledChecks map[string]struct{}
278+
// *LowerCased* fields are used for GoCriticSettings.SettingsPerCheck validation only.
279+
280+
allChecksLowerCased map[string]struct{}
283281
inferredEnabledLowerCasedChecks map[string]struct{}
284282
}
285283

@@ -334,7 +332,7 @@ func (s *goCriticSettingsWrapper) InferEnabledChecks() {
334332
enabledChecks[info.Name] = struct{}{}
335333
}
336334
} else if !s.DisableAll {
337-
// NOTE(Antonboom): enable-all/disable-all revokes the default settings.
335+
// enable-all/disable-all revokes the default settings.
338336
enabledChecks = make(map[string]struct{}, len(enabledByDefaultChecks))
339337
for _, check := range enabledByDefaultChecks {
340338
enabledChecks[check] = struct{}{}

test/ruleguard/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
This directory contains ruleguard files that are used in functional tests.
1+
This directory contains ruleguard files that are used in functional tests:
2+
3+
```bash
4+
T=gocritic.go make test_linters
5+
```
6+
7+
```bash
8+
T=gocritic.go make test_fix
9+
```
10+
11+
Helpful:
12+
13+
- https://go-critic.com/overview.html
14+
- https://github.com/go-critic/go-critic/blob/master/checkers/rules/rules.go

test/ruleguard/dup.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/ruleguard/preferWriteString.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build ruleguard
2+
3+
package ruleguard
4+
5+
import "github.com/quasilyte/go-ruleguard/dsl"
6+
7+
func preferWriteString(m dsl.Matcher) {
8+
m.Match(`$w.Write([]byte($s))`).
9+
Where(m["w"].Type.Implements("io.StringWriter")).
10+
Suggest("$w.WriteString($s)").
11+
Report(`$w.WriteString($s) should be preferred to the $$`)
12+
}

test/ruleguard/rangeExprCopy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/quasilyte/go-ruleguard/dsl"
77
)
88

9-
func RangeExprVal(m dsl.Matcher) {
9+
func rangeExprCopy(m dsl.Matcher) {
1010
m.Match(`for _, $_ := range $x { $*_ }`, `for _, $_ = range $x { $*_ }`).
1111
Where(m["x"].Addressable && m["x"].Type.Size >= 512).
1212
Report(`$x copy can be avoided with &$x`).

test/ruleguard/strings_simplify.go renamed to test/ruleguard/stringsSimplify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package ruleguard
44

55
import "github.com/quasilyte/go-ruleguard/dsl"
66

7-
func StringsSimplify(m dsl.Matcher) {
7+
func stringsSimplify(m dsl.Matcher) {
88
// Some issues have simple fixes that can be expressed as
99
// a replacement pattern. Rules can use Suggest() function
1010
// to add a quickfix action for such issues.

test/testdata/configs/gocritic-fix.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ linters-settings:
44
- ruleguard
55
settings:
66
ruleguard:
7-
rules: 'ruleguard/rangeExprCopy.go,ruleguard/strings_simplify.go'
8-
7+
rules: 'ruleguard/rangeExprCopy.go,ruleguard/stringsSimplify.go'

test/testdata/configs/gocritic.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
linters-settings:
22
gocritic:
3+
disabled-checks:
4+
- appendAssign
5+
- switchTrue
36
enabled-checks:
4-
- rangeValCopy
7+
- hugeParam
58
- ruleguard
6-
disabled-checks:
7-
- dupSubExpr # So as not to overlap `ruleguard`.
89
settings:
9-
rangeValCopy:
10-
sizeThreshold: 2
10+
hugeParam:
11+
sizeThreshold: 24
1112
ruleguard:
12-
debug: dupSubExpr
1313
failOn: dsl,import
14-
# comma-separated paths to ruleguard files.
14+
# Comma-separated paths to ruleguard files.
1515
# The ${configDir} is substituted by the directory containing the golangci-lint config file.
1616
# Note about the directory structure for functional tests:
1717
# The ruleguard files used in functional tests cannot be under the 'testdata' directory.
1818
# This is because they import the 'github.com/quasilyte/go-ruleguard/dsl' package,
1919
# which needs to be added to go.mod. The testdata directory is ignored by go mod.
20-
rules: '${configDir}/../../ruleguard/strings_simplify.go,${configDir}/../../ruleguard/dup.go'
20+
rules: '${configDir}/../../ruleguard/preferWriteString.go,${configDir}/../../ruleguard/stringsSimplify.go'

test/testdata/gocritic.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,69 @@ package testdata
44

55
import (
66
"flag"
7+
"io"
78
"log"
89
"strings"
910
)
1011

1112
var _ = *flag.Bool("global1", false, "") // want `flagDeref: immediate deref in \*flag.Bool\(.global1., false, ..\) is most likely an error; consider using flag\.BoolVar`
1213

1314
type size1 struct {
14-
a bool
15+
a [12]bool
1516
}
1617

1718
type size2 struct {
1819
size1
19-
b bool
20+
b [12]bool
2021
}
2122

22-
func gocriticRangeValCopySize1(ss []size1) {
23-
for _, s := range ss {
24-
log.Print(s)
23+
func gocriticAppendAssign() {
24+
var positives, negatives []int
25+
positives = append(negatives, 1)
26+
negatives = append(negatives, -1)
27+
log.Print(positives, negatives)
28+
}
29+
30+
func gocriticDupSubExpr(x bool) {
31+
if x && x { // want "dupSubExpr: suspicious identical LHS and RHS.*"
32+
log.Print("x is true")
2533
}
2634
}
2735

28-
func gocriticRangeValCopySize2(ss []size2) {
29-
for _, s := range ss { // want "rangeValCopy: each iteration copies 2 bytes.*"
30-
log.Print(s)
36+
func gocriticHugeParamSize1(ss size1) {
37+
log.Print(ss)
38+
}
39+
40+
func gocriticHugeParamSize2(ss size2) { // want "hugeParam: ss is heavy \\(24 bytes\\); consider passing it by pointer"
41+
log.Print(ss)
42+
}
43+
44+
func gocriticHugeParamSize2Ptr(ss *size2) {
45+
log.Print(*ss)
46+
}
47+
48+
func gocriticSwitchTrue() {
49+
switch true {
50+
case false:
51+
log.Print("false")
52+
default:
53+
log.Print("default")
3154
}
3255
}
3356

57+
func goCriticPreferStringWriter(w interface {
58+
io.Writer
59+
io.StringWriter
60+
}) {
61+
w.Write([]byte("test")) // want "ruleguard: w\\.WriteString\\(\"test\"\\) should be preferred.*"
62+
}
63+
3464
func gocriticStringSimplify() {
3565
s := "Most of the time, travellers worry about their luggage."
3666
s = strings.Replace(s, ",", "", -1) // want "ruleguard: this Replace call can be simplified.*"
3767
log.Print(s)
3868
}
3969

40-
func gocriticDup(x bool) {
41-
if x && x { // want "ruleguard: suspicious identical LHS and RHS.*"
42-
log.Print("x is true")
43-
}
44-
}
45-
4670
func gocriticRuleWrapperFunc() {
4771
strings.Replace("abcabc", "a", "d", -1) // want "ruleguard: this Replace call can be simplified.*"
4872
}

0 commit comments

Comments
 (0)