From 59752fb36d57d6d0a1029c71033cb3fa52783a00 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 20 Dec 2021 23:30:08 +0100 Subject: [PATCH] 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 --- .golangci.yml | 16 ++++++++++++++ pkg/yqlib/all_at_once_evaluator.go | 2 +- pkg/yqlib/candidate_node.go | 2 +- pkg/yqlib/expression_tokeniser.go | 8 +++---- pkg/yqlib/lib.go | 4 ++-- pkg/yqlib/operator_anchors_aliases.go | 29 ++++++++++++------------- pkg/yqlib/operator_collect_object.go | 4 ++-- pkg/yqlib/operator_create_map.go | 4 ++-- pkg/yqlib/operator_entries.go | 10 ++++----- pkg/yqlib/operator_has.go | 2 +- pkg/yqlib/operator_map.go | 4 ++-- pkg/yqlib/operator_multiply.go | 4 ++-- pkg/yqlib/operator_recursive_descent.go | 8 +++---- pkg/yqlib/operator_split_document.go | 2 +- pkg/yqlib/operator_strings.go | 2 +- pkg/yqlib/operator_subtract.go | 4 ++-- pkg/yqlib/operator_traverse_path.go | 10 ++++----- pkg/yqlib/operators_test.go | 4 ++-- pkg/yqlib/stream_evaluator.go | 2 +- pkg/yqlib/utils.go | 2 +- 20 files changed, 69 insertions(+), 54 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 7a9f27b7..982ce949 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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" diff --git a/pkg/yqlib/all_at_once_evaluator.go b/pkg/yqlib/all_at_once_evaluator.go index 9c79a035..d23c56c6 100644 --- a/pkg/yqlib/all_at_once_evaluator.go +++ b/pkg/yqlib/all_at_once_evaluator.go @@ -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 { diff --git a/pkg/yqlib/candidate_node.go b/pkg/yqlib/candidate_node.go index 428bbeca..2bfbc586 100644 --- a/pkg/yqlib/candidate_node.go +++ b/pkg/yqlib/candidate_node.go @@ -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 } diff --git a/pkg/yqlib/expression_tokeniser.go b/pkg/yqlib/expression_tokeniser.go index 8c7d1634..0fb05642 100644 --- a/pkg/yqlib/expression_tokeniser.go +++ b/pkg/yqlib/expression_tokeniser.go @@ -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 } diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 2cb2f968..42cf2a9c 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -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 } diff --git a/pkg/yqlib/operator_anchors_aliases.go b/pkg/yqlib/operator_anchors_aliases.go index c9ee4ca0..6e7ef987 100644 --- a/pkg/yqlib/operator_anchors_aliases.go +++ b/pkg/yqlib/operator_anchors_aliases.go @@ -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 } diff --git a/pkg/yqlib/operator_collect_object.go b/pkg/yqlib/operator_collect_object.go index 9cc6792d..59417a62 100644 --- a/pkg/yqlib/operator_collect_object.go +++ b/pkg/yqlib/operator_collect_object.go @@ -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() { diff --git a/pkg/yqlib/operator_create_map.go b/pkg/yqlib/operator_create_map.go index cac23356..d5ca9954 100644 --- a/pkg/yqlib/operator_create_map.go +++ b/pkg/yqlib/operator_create_map.go @@ -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 { diff --git a/pkg/yqlib/operator_entries.go b/pkg/yqlib/operator_entries.go index a0377a74..d3d8bc3d 100644 --- a/pkg/yqlib/operator_entries.go +++ b/pkg/yqlib/operator_entries.go @@ -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 } diff --git a/pkg/yqlib/operator_has.go b/pkg/yqlib/operator_has.go index bce557a0..a8a68d55 100644 --- a/pkg/yqlib/operator_has.go +++ b/pkg/yqlib/operator_has.go @@ -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 } diff --git a/pkg/yqlib/operator_map.go b/pkg/yqlib/operator_map.go index a28b652a..e3b8ec99 100644 --- a/pkg/yqlib/operator_map.go +++ b/pkg/yqlib/operator_map.go @@ -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 } diff --git a/pkg/yqlib/operator_multiply.go b/pkg/yqlib/operator_multiply.go index ff0410e9..d22e2612 100644 --- a/pkg/yqlib/operator_multiply.go +++ b/pkg/yqlib/operator_multiply.go @@ -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) } diff --git a/pkg/yqlib/operator_recursive_descent.go b/pkg/yqlib/operator_recursive_descent.go index 71095540..bb583840 100644 --- a/pkg/yqlib/operator_recursive_descent.go +++ b/pkg/yqlib/operator_recursive_descent.go @@ -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 } diff --git a/pkg/yqlib/operator_split_document.go b/pkg/yqlib/operator_split_document.go index 6cabcd23..eba24964 100644 --- a/pkg/yqlib/operator_split_document.go +++ b/pkg/yqlib/operator_split_document.go @@ -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 diff --git a/pkg/yqlib/operator_strings.go b/pkg/yqlib/operator_strings.go index 59e18e48..cfcd3a17 100644 --- a/pkg/yqlib/operator_strings.go +++ b/pkg/yqlib/operator_strings.go @@ -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) diff --git a/pkg/yqlib/operator_subtract.go b/pkg/yqlib/operator_subtract.go index 51d68cbf..4c267e8c 100644 --- a/pkg/yqlib/operator_subtract.go +++ b/pkg/yqlib/operator_subtract.go @@ -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) diff --git a/pkg/yqlib/operator_traverse_path.go b/pkg/yqlib/operator_traverse_path.go index 69671591..c8df17d6 100644 --- a/pkg/yqlib/operator_traverse_path.go +++ b/pkg/yqlib/operator_traverse_path.go @@ -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 } diff --git a/pkg/yqlib/operators_test.go b/pkg/yqlib/operators_test.go index 197e1755..b99d3865 100644 --- a/pkg/yqlib/operators_test.go +++ b/pkg/yqlib/operators_test.go @@ -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 } diff --git a/pkg/yqlib/stream_evaluator.go b/pkg/yqlib/stream_evaluator.go index f49c6745..ddb777fb 100644 --- a/pkg/yqlib/stream_evaluator.go +++ b/pkg/yqlib/stream_evaluator.go @@ -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 diff --git a/pkg/yqlib/utils.go b/pkg/yqlib/utils.go index d5d53071..f8291a40 100644 --- a/pkg/yqlib/utils.go +++ b/pkg/yqlib/utils.go @@ -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