enable more linters (#1043)

* enable revive linter

* enable gochecknoinits linter

* enable unconvert linter

* enable unparam linter

* enable asciicheck linter

* enable depguard linter

* enable nakedret linter

* enable megacheck linter

* enable nolintlint linter

* enable predeclared linter

* Update go.yml

* Update go.yml
This commit is contained in:
Matthieu MOREL 2021-12-20 23:30:08 +01:00 committed by GitHub
parent d8abcce633
commit 59752fb36d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 69 additions and 54 deletions

View File

@ -2,15 +2,31 @@ run:
timeout: 5m
linters:
enable:
- asciicheck
- depguard
- errorlint
- gci
- gochecknoinits
- gofmt
- goimports
- gosec
- megacheck
- misspell
- nakedret
- nolintlint
- predeclared
- revive
- unconvert
- unparam
issues:
exclude-rules:
- linters:
- gosec
text: "Implicit memory aliasing in for loop."
path: _test\.go
- linters:
- revive
text: "unexported-return"
- linters:
- revive
text: "var-naming"

View File

@ -50,7 +50,7 @@ func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string
fileIndex := 0
firstFileLeadingContent := ""
var allDocuments *list.List = list.New()
var allDocuments = list.New()
for _, filename := range filenames {
reader, leadingContent, err := readStream(filename, fileIndex == 0 && leadingContentPreProcessing)
if err != nil {

View File

@ -56,7 +56,7 @@ func (n *CandidateNode) AsList() *list.List {
}
func (n *CandidateNode) CreateChildInMap(key *yaml.Node, node *yaml.Node) *CandidateNode {
var value interface{} = nil
var value interface{}
if key != nil {
value = key.Value
}

View File

@ -135,7 +135,7 @@ func opTokenWithPrefs(op *operationType, assignOpType *operationType, preference
func extractNumberParamter(value string) (int, error) {
parameterParser := regexp.MustCompile(`.*\(([0-9]+)\)`)
matches := parameterParser.FindStringSubmatch(value)
var indent, errParsingInt = strconv.ParseInt(matches[1], 10, 32) // nolint
var indent, errParsingInt = strconv.ParseInt(matches[1], 10, 32)
if errParsingInt != nil {
return 0, errParsingInt
}
@ -198,7 +198,7 @@ func unwrap(value string) string {
func numberValue() lex.Action {
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
var numberString = string(m.Bytes)
var number, errParsingInt = strconv.ParseInt(numberString, 10, 64) // nolint
var number, errParsingInt = strconv.ParseInt(numberString, 10, 64)
if errParsingInt != nil {
return nil, errParsingInt
}
@ -212,7 +212,7 @@ func hexValue() lex.Action {
var originalString = string(m.Bytes)
var numberString = originalString[2:]
log.Debugf("numberString: %v", numberString)
var number, errParsingInt = strconv.ParseInt(numberString, 16, 64) // nolint
var number, errParsingInt = strconv.ParseInt(numberString, 16, 64)
if errParsingInt != nil {
return nil, errParsingInt
}
@ -224,7 +224,7 @@ func hexValue() lex.Action {
func floatValue() lex.Action {
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
var numberString = string(m.Bytes)
var number, errParsingInt = strconv.ParseFloat(numberString, 64) // nolint
var number, errParsingInt = strconv.ParseFloat(numberString, 64)
if errParsingInt != nil {
return nil, errParsingInt
}

View File

@ -203,10 +203,10 @@ func recursiveNodeEqual(lhs *yaml.Node, rhs *yaml.Node) bool {
func parseInt(numberString string) (string, int64, error) {
if strings.HasPrefix(numberString, "0x") ||
strings.HasPrefix(numberString, "0X") {
num, err := strconv.ParseInt(numberString[2:], 16, 64) // nolint
num, err := strconv.ParseInt(numberString[2:], 16, 64)
return "0x%X", num, err
}
num, err := strconv.ParseInt(numberString, 10, 64) // nolint
num, err := strconv.ParseInt(numberString, 10, 64)
return "%v", num, err
}

View File

@ -220,22 +220,21 @@ func explodeNode(node *yaml.Node, context Context) error {
if hasAlias {
// this is a slow op, which is why we want to check before running it.
return reconstructAliasedMap(node, context)
} else {
// this map has no aliases, but it's kids might
for index := 0; index < len(node.Content); index = index + 2 {
keyNode := node.Content[index]
valueNode := node.Content[index+1]
err := explodeNode(keyNode, context)
if err != nil {
return err
}
err = explodeNode(valueNode, context)
if err != nil {
return err
}
}
return nil
}
// this map has no aliases, but it's kids might
for index := 0; index < len(node.Content); index = index + 2 {
keyNode := node.Content[index]
valueNode := node.Content[index+1]
err := explodeNode(keyNode, context)
if err != nil {
return err
}
err = explodeNode(valueNode, context)
if err != nil {
return err
}
}
return nil
default:
return nil
}

View File

@ -28,7 +28,7 @@ func collectObjectOperator(d *dataTreeNavigator, originalContext Context, expres
return context.SingleChildContext(candidate), nil
}
first := context.MatchingNodes.Front().Value.(*CandidateNode)
var rotated []*list.List = make([]*list.List, len(first.Node.Content))
var rotated = make([]*list.List, len(first.Node.Content))
for i := 0; i < len(first.Node.Content); i++ {
rotated[i] = list.New()
@ -61,7 +61,7 @@ func collect(d *dataTreeNavigator, context Context, remainingMatches *list.List)
candidate := remainingMatches.Remove(remainingMatches.Front()).(*CandidateNode)
splatted, err := splat(d, context.SingleChildContext(candidate),
splatted, err := splat(context.SingleChildContext(candidate),
traversePreferences{DontFollowAlias: true, IncludeMapKeys: false})
for splatEl := splatted.MatchingNodes.Front(); splatEl != nil; splatEl = splatEl.Next() {

View File

@ -14,7 +14,7 @@ func createMapOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
var path []interface{}
var document uint = 0
var document uint
sequences := list.New()
@ -42,7 +42,7 @@ func createMapOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
func sequenceFor(d *dataTreeNavigator, context Context, matchingNode *CandidateNode, expressionNode *ExpressionNode) (*CandidateNode, error) {
var path []interface{}
var document uint = 0
var document uint
var matches = list.New()
if matchingNode != nil {

View File

@ -68,7 +68,7 @@ func toEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
return context.ChildContext(results), nil
}
func parseEntry(d *dataTreeNavigator, entry *yaml.Node, position int) (*yaml.Node, *yaml.Node, error) {
func parseEntry(entry *yaml.Node, position int) (*yaml.Node, *yaml.Node, error) {
prefs := traversePreferences{DontAutoCreate: true}
candidateNode := &CandidateNode{Node: entry}
@ -92,14 +92,14 @@ func parseEntry(d *dataTreeNavigator, entry *yaml.Node, position int) (*yaml.Nod
}
func fromEntries(d *dataTreeNavigator, candidateNode *CandidateNode) (*CandidateNode, error) {
func fromEntries(candidateNode *CandidateNode) (*CandidateNode, error) {
var node = &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
var mapCandidateNode = candidateNode.CreateReplacement(node)
var contents = unwrapDoc(candidateNode.Node).Content
for index := 0; index < len(contents); index = index + 1 {
key, value, err := parseEntry(d, contents[index], index)
key, value, err := parseEntry(contents[index], index)
if err != nil {
return nil, err
}
@ -117,7 +117,7 @@ func fromEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *
switch candidateNode.Kind {
case yaml.SequenceNode:
mapResult, err := fromEntries(d, candidate)
mapResult, err := fromEntries(candidate)
if err != nil {
return Context{}, err
}
@ -143,7 +143,7 @@ func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *
for el := toEntries.MatchingNodes.Front(); el != nil; el = el.Next() {
//run expression against entries
// splat toEntries and pipe it into Rhs
splatted, err := splat(d, context.SingleChildContext(el.Value.(*CandidateNode)), traversePreferences{})
splatted, err := splat(context.SingleChildContext(el.Value.(*CandidateNode)), traversePreferences{})
if err != nil {
return Context{}, err
}

View File

@ -44,7 +44,7 @@ func hasOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
case yaml.SequenceNode:
candidateHasKey := false
if wanted.Tag == "!!int" {
var number, errParsingInt = strconv.ParseInt(wantedKey, 10, 64) // nolint
var number, errParsingInt = strconv.ParseInt(wantedKey, 10, 64)
if errParsingInt != nil {
return Context{}, errParsingInt
}

View File

@ -10,7 +10,7 @@ func mapValuesOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
candidate := el.Value.(*CandidateNode)
//run expression against entries
// splat toEntries and pipe it into Rhs
splatted, err := splat(d, context.SingleChildContext(candidate), traversePreferences{})
splatted, err := splat(context.SingleChildContext(candidate), traversePreferences{})
if err != nil {
return Context{}, err
}
@ -37,7 +37,7 @@ func mapOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
candidate := el.Value.(*CandidateNode)
//run expression against entries
// splat toEntries and pipe it into Rhs
splatted, err := splat(d, context.SingleChildContext(candidate), traversePreferences{})
splatted, err := splat(context.SingleChildContext(candidate), traversePreferences{})
if err != nil {
return Context{}, err
}

View File

@ -109,12 +109,12 @@ func mergeObjects(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs
TraversePreferences: traversePreferences{DontFollowAlias: true, IncludeMapKeys: true}}
log.Debugf("merge - preferences.DeepMergeArrays %v", preferences.DeepMergeArrays)
log.Debugf("merge - preferences.AppendArrays %v", preferences.AppendArrays)
err := recursiveDecent(d, results, context.SingleChildContext(rhs), prefs)
err := recursiveDecent(results, context.SingleChildContext(rhs), prefs)
if err != nil {
return nil, err
}
var pathIndexToStartFrom int = 0
var pathIndexToStartFrom int
if results.Front() != nil {
pathIndexToStartFrom = len(results.Front().Value.(*CandidateNode).Path)
}

View File

@ -15,7 +15,7 @@ func recursiveDescentOperator(d *dataTreeNavigator, context Context, expressionN
var results = list.New()
preferences := expressionNode.Operation.Preferences.(recursiveDescentPreferences)
err := recursiveDecent(d, results, context, preferences)
err := recursiveDecent(results, context, preferences)
if err != nil {
return Context{}, err
}
@ -23,7 +23,7 @@ func recursiveDescentOperator(d *dataTreeNavigator, context Context, expressionN
return context.ChildContext(results), nil
}
func recursiveDecent(d *dataTreeNavigator, results *list.List, context Context, preferences recursiveDescentPreferences) error {
func recursiveDecent(results *list.List, context Context, preferences recursiveDescentPreferences) error {
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
@ -35,12 +35,12 @@ func recursiveDecent(d *dataTreeNavigator, results *list.List, context Context,
if candidate.Node.Kind != yaml.AliasNode && len(candidate.Node.Content) > 0 &&
(preferences.RecurseArray || candidate.Node.Kind != yaml.SequenceNode) {
children, err := splat(d, context.SingleChildContext(candidate), preferences.TraversePreferences)
children, err := splat(context.SingleChildContext(candidate), preferences.TraversePreferences)
if err != nil {
return err
}
err = recursiveDecent(d, results, children, preferences)
err = recursiveDecent(results, children, preferences)
if err != nil {
return err
}

View File

@ -3,7 +3,7 @@ package yqlib
func splitDocumentOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- splitDocumentOperator")
var index uint = 0
var index uint
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
candidate.Document = index

View File

@ -62,7 +62,7 @@ func substituteStringOperator(d *dataTreeNavigator, context Context, expressionN
candidate := el.Value.(*CandidateNode)
node := unwrapDoc(candidate.Node)
if node.Tag != "!!str" {
return Context{}, fmt.Errorf("cannot substitute with %v, can only substitute strings. Hint: Most often you'll want to use '|=' over '=' for this operation.", node.Tag)
return Context{}, fmt.Errorf("cannot substitute with %v, can only substitute strings. Hint: Most often you'll want to use '|=' over '=' for this operation", node.Tag)
}
targetNode := substitute(node.Value, regEx, replacementText)

View File

@ -23,7 +23,7 @@ func subtractOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
return crossFunction(d, context.ReadOnlyClone(), expressionNode, subtract, false)
}
func subtractArray(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
newLhsArray := make([]*yaml.Node, 0)
for lindex := 0; lindex < len(lhs.Node.Content); lindex = lindex + 1 {
@ -60,7 +60,7 @@ func subtract(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *Ca
if rhs.Node.Kind != yaml.SequenceNode {
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Node.Tag, rhs.Path, lhsNode.Tag)
}
return subtractArray(d, context, lhs, rhs)
return subtractArray(lhs, rhs)
case yaml.ScalarNode:
if rhs.Node.Kind != yaml.ScalarNode {
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Node.Tag, rhs.Path, lhsNode.Tag)

View File

@ -17,7 +17,7 @@ type traversePreferences struct {
OptionalTraverse bool // e.g. .adf?
}
func splat(d *dataTreeNavigator, context Context, prefs traversePreferences) (Context, error) {
func splat(context Context, prefs traversePreferences) (Context, error) {
return traverseNodesWithArrayIndices(context, make([]*yaml.Node, 0), prefs)
}
@ -26,7 +26,7 @@ func traversePathOperator(d *dataTreeNavigator, context Context, expressionNode
var matches = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
newNodes, err := traverse(d, context, el.Value.(*CandidateNode), expressionNode.Operation)
newNodes, err := traverse(context, el.Value.(*CandidateNode), expressionNode.Operation)
if err != nil {
return Context{}, err
}
@ -36,7 +36,7 @@ func traversePathOperator(d *dataTreeNavigator, context Context, expressionNode
return context.ChildContext(matches), nil
}
func traverse(d *dataTreeNavigator, context Context, matchingNode *CandidateNode, operation *Operation) (*list.List, error) {
func traverse(context Context, matchingNode *CandidateNode, operation *Operation) (*list.List, error) {
log.Debug("Traversing %v", NodeToString(matchingNode))
value := matchingNode.Node
@ -66,11 +66,11 @@ func traverse(d *dataTreeNavigator, context Context, matchingNode *CandidateNode
case yaml.AliasNode:
log.Debug("its an alias!")
matchingNode.Node = matchingNode.Node.Alias
return traverse(d, context, matchingNode, operation)
return traverse(context, matchingNode, operation)
case yaml.DocumentNode:
log.Debug("digging into doc node")
return traverse(d, context, matchingNode.CreateChildInMap(nil, matchingNode.Node.Content[0]), operation)
return traverse(context, matchingNode.CreateChildInMap(nil, matchingNode.Node.Content[0]), operation)
default:
return list.New(), nil
}

View File

@ -91,7 +91,7 @@ func testScenario(t *testing.T, s *expressionScenario) {
}
func resultsToString(t *testing.T, results *list.List) []string {
var pretty []string = make([]string, 0)
var pretty = make([]string, 0)
for el := results.Front(); el != nil; el = el.Next() {
n := el.Value.(*CandidateNode)
@ -129,7 +129,7 @@ func copyFromHeader(title string, out *os.File) error {
if os.IsNotExist(err) {
return nil
}
in, err := os.Open(source) // nolint gosec
in, err := os.Open(source)
if err != nil {
return err
}

View File

@ -52,7 +52,7 @@ func (s *streamEvaluator) EvaluateNew(expression string, printer Printer, leadin
}
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer, leadingContentPreProcessing bool) error {
var totalProcessDocs uint = 0
var totalProcessDocs uint
node, err := s.treeCreator.ParseExpression(expression)
if err != nil {
return err

View File

@ -110,7 +110,7 @@ func processReadStream(reader *bufio.Reader) (io.Reader, string, error) {
func readDocuments(reader io.Reader, filename string, fileIndex int) (*list.List, error) {
decoder := yaml.NewDecoder(reader)
inputList := list.New()
var currentIndex uint = 0
var currentIndex uint
for {
var dataBucket yaml.Node