Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unavailable_code.go « gitaly « golangci-lint « tools - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9546b1871a6e5986c14cd620b0b87367f91f058f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main

import (
	"go/ast"

	"golang.org/x/tools/go/analysis"
)

const unavailableCodeAnalyzerName = "unavailable_code"

type unavailableCodeAnalyzerSettings struct {
	IncludedFunctions []string `mapstructure:"included-functions"`
}

// newErrorWrapAnalyzer warns if Unavailable status code is used. Unavailable status code is reserved to signal server's
// unavailability. It should be used by some specific components. gRPC handlers should typically avoid this type of
// error.
// For more information:
// https://gitlab.com/gitlab-org/gitaly/-/blob/master/STYLE.md?plain=0#unavailable-code
func newUnavailableCodeAnalyzer(settings *unavailableCodeAnalyzerSettings) *analysis.Analyzer {
	return &analysis.Analyzer{
		Name: unavailableCodeAnalyzerName,
		Doc:  `discourage the usage of Unavailable status code`,
		Run:  runUnavailableCodeAnalyzer(settings.IncludedFunctions),
	}
}

func runUnavailableCodeAnalyzer(rules []string) func(*analysis.Pass) (interface{}, error) {
	return func(pass *analysis.Pass) (interface{}, error) {
		matcher := NewMatcher(pass)
		for _, file := range pass.Files {
			ast.Inspect(file, func(n ast.Node) bool {
				if call, ok := n.(*ast.CallExpr); ok {
					if matcher.MatchFunction(call, rules) {
						pass.Report(analysis.Diagnostic{
							Pos:            call.Pos(),
							End:            call.End(),
							Message:        "please avoid using the Unavailable status code: https://gitlab.com/gitlab-org/gitaly/-/blob/master/STYLE.md?plain=0#unavailable-code",
							SuggestedFixes: nil,
						})
					}
				}
				return true
			})
		}
		return nil, nil
	}
}