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
add exhaustive linter #1166
add exhaustive linter #1166
Changes from 5 commits
403564f
16dac05
870488b
cfb270e
1de0224
082eb00
cf8f8f0
a292967
4683864
f83ef76
7ec55f0
f14ddae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package golinters | ||
|
||
import ( | ||
"github.com/nishanths/exhaustive" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewExhaustive(settings *config.ExhaustiveSettings) *goanalysis.Linter { | ||
a := exhaustive.Analyzer | ||
|
||
var cfg map[string]map[string]interface{} | ||
if settings != nil { | ||
cfg = map[string]map[string]interface{}{ | ||
a.Name: { | ||
exhaustive.DefaultSignifiesExhaustiveFlag: settings.DefaultSignifiesExhaustive, | ||
}, | ||
} | ||
} | ||
|
||
return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, cfg). | ||
WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -935,7 +935,8 @@ func sizeOfReflectValueTreeBytes(rv reflect.Value, visitedPtrs map[uintptr]struc | |
return rv.Len() | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, | ||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, | ||
reflect.Uintptr, reflect.Bool, reflect.Float32, reflect.Float64, reflect.UnsafePointer: | ||
reflect.Uintptr, reflect.Bool, reflect.Float32, reflect.Float64, | ||
reflect.Complex64, reflect.Complex128, reflect.Func, reflect.UnsafePointer: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These missing cases
were reported by the exhaustive linter when I temporarily enabled it for a run. |
||
return int(rv.Type().Size()) | ||
case reflect.Invalid: | ||
return 0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//args: -Eexhaustive | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should another test case including enabling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that would help. I'll add one. |
||
package testdata | ||
|
||
type Direction int | ||
|
||
const ( | ||
North Direction = iota | ||
East | ||
South | ||
West | ||
) | ||
|
||
func processDirection(d Direction) { | ||
switch d { // ERROR "missing cases in switch of type Direction: East, West" | ||
case North, South: | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not enable it? I think dogfooding linters is important
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I can enable it.
I did not enable it originally because I was unsure if users would find the default behavior of the analyzer to be too strict. (The default behavior of the analyzer is to point out missing enum members in the switch even if a
default
case is present. That might actually be useful though — otherwise we wouldn't have discovered the missingreflect.Complex64
etc. cases above.)