summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mgechev/revive/rule/modifies-value-receiver.go
blob: 4fe22ddf3fef66b406a51ee6f528215fd33d9c51 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package rule

import (
	"go/ast"
	"strings"

	"github.com/mgechev/revive/lint"
)

// ModifiesValRecRule lints assignments to value method-receivers.
type ModifiesValRecRule struct{}

// Apply applies the rule to given file.
func (r *ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
	var failures []lint.Failure

	onFailure := func(failure lint.Failure) {
		failures = append(failures, failure)
	}

	w := lintModifiesValRecRule{file: file, onFailure: onFailure}
	file.Pkg.TypeCheck()
	ast.Walk(w, file.AST)

	return failures
}

// Name returns the rule name.
func (r *ModifiesValRecRule) Name() string {
	return "modifies-value-receiver"
}

type lintModifiesValRecRule struct {
	file      *lint.File
	onFailure func(lint.Failure)
}

func (w lintModifiesValRecRule) Visit(node ast.Node) ast.Visitor {
	switch n := node.(type) {
	case *ast.FuncDecl:
		if n.Recv == nil {
			return nil // skip, not a method
		}

		receiver := n.Recv.List[0]
		if _, ok := receiver.Type.(*ast.StarExpr); ok {
			return nil // skip, method with pointer receiver
		}

		if w.skipType(receiver.Type) {
			return nil // skip, receiver is a map or array
		}

		if len(receiver.Names) < 1 {
			return nil // skip, anonymous receiver
		}

		receiverName := receiver.Names[0].Name
		if receiverName == "_" {
			return nil // skip, anonymous receiver
		}

		fselect := func(n ast.Node) bool {
			// look for assignments with the receiver in the right hand
			asgmt, ok := n.(*ast.AssignStmt)
			if !ok {
				return false
			}

			for _, exp := range asgmt.Lhs {
				switch e := exp.(type) {
				case *ast.IndexExpr: // receiver...[] = ...
					continue
				case *ast.StarExpr: // *receiver = ...
					continue
				case *ast.SelectorExpr: // receiver.field = ...
					name := w.getNameFromExpr(e.X)
					if name == "" || name != receiverName {
						continue
					}

					if w.skipType(ast.Expr(e.Sel)) {
						continue
					}

				case *ast.Ident: // receiver := ...
					if e.Name != receiverName {
						continue
					}
				default:
					continue
				}

				return true
			}

			return false
		}

		assignmentsToReceiver := pick(n.Body, fselect, nil)

		for _, assignment := range assignmentsToReceiver {
			w.onFailure(lint.Failure{
				Node:       assignment,
				Confidence: 1,
				Failure:    "suspicious assignment to a by-value method receiver",
			})
		}
	}

	return w
}

func (w lintModifiesValRecRule) skipType(t ast.Expr) bool {
	rt := w.file.Pkg.TypeOf(t)
	if rt == nil {
		return false
	}

	rt = rt.Underlying()
	rtName := rt.String()

	// skip when receiver is a map or array
	return strings.HasPrefix(rtName, "[]") || strings.HasPrefix(rtName, "map[")
}

func (lintModifiesValRecRule) getNameFromExpr(ie ast.Expr) string {
	ident, ok := ie.(*ast.Ident)
	if !ok {
		return ""
	}

	return ident.Name
}