Skip to content

Commit 71335ad

Browse files
committed
S1008: refactored lisp part a bit
1 parent d8eac1b commit 71335ad

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

simple/s1008/s1008.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,17 @@ var (
4343
(IfStmt
4444
nil
4545
cond
46-
[fret@(ReturnStmt _)]
46+
[fullret@(ReturnStmt _)]
4747
nil)
4848
`)
4949
checkIfReturnQRet = pattern.MustParse(`
50-
(Binding "fret" (ReturnStmt _))
50+
(Binding "fullret" (ReturnStmt _))
5151
`)
5252
checkReturnValue = pattern.MustParse(`
53-
(Or
54-
(ReturnStmt
55-
(List
56-
ret@(Builtin (Or "true" "false"))
57-
tail@(Any)))
58-
(ReturnStmt
59-
[ret@(Builtin (Or "true" "false"))]))
53+
(ReturnStmt
54+
(List
55+
ret@(Builtin (Or "true" "false"))
56+
tail@(Any)))
6057
`)
6158
)
6259

@@ -75,31 +72,31 @@ func run(pass *analysis.Pass) (interface{}, error) {
7572
return
7673
}
7774
}
78-
fm1, ok := code.Match(pass, checkIfReturnQIf, n1)
75+
fullm1, ok := code.Match(pass, checkIfReturnQIf, n1)
7976
if !ok {
8077
return
8178
}
82-
fm2, ok := code.Match(pass, checkIfReturnQRet, n2)
79+
fullm2, ok := code.Match(pass, checkIfReturnQRet, n2)
8380
if !ok {
8481
return
8582
}
8683

87-
if op, ok := fm1.State["cond"].(*ast.BinaryExpr); ok {
84+
if op, ok := fullm1.State["cond"].(*ast.BinaryExpr); ok {
8885
switch op.Op {
8986
case token.EQL, token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ:
9087
default:
9188
return
9289
}
9390
}
9491

95-
fret1 := fm1.State["fret"].(*ast.ReturnStmt)
96-
m1, ok := code.Match(pass, checkReturnValue, fret1)
92+
fullret1 := fullm1.State["fullret"].(*ast.ReturnStmt)
93+
m1, ok := code.Match(pass, checkReturnValue, fullret1)
9794
if !ok {
9895
return
9996
}
10097

101-
fret2 := fm2.State["fret"].(*ast.ReturnStmt)
102-
m2, ok := code.Match(pass, checkReturnValue, fret2)
98+
fullret2 := fullm2.State["fullret"].(*ast.ReturnStmt)
99+
m2, ok := code.Match(pass, checkReturnValue, fullret2)
103100
if !ok {
104101
return
105102
}
@@ -121,7 +118,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
121118
return
122119
}
123120

124-
cond := fm1.State["cond"].(ast.Expr)
121+
cond := fullm1.State["cond"].(ast.Expr)
125122
origCond := cond
126123
if ret1.Name == "false" {
127124
cond = negate(pass, cond)
@@ -146,22 +143,22 @@ func run(pass *analysis.Pass) (interface{}, error) {
146143
return nil, nil
147144
}
148145

149-
func getRetAndTail(m1 *pattern.Matcher) (*ast.Ident, []ast.Expr) {
150-
ret1 := m1.State["ret"].(*ast.Ident)
151-
var tail1 []ast.Expr
152-
if tail, ok := m1.State["tail"]; ok {
153-
tail1, _ = tail.([]ast.Expr)
146+
func getRetAndTail(m *pattern.Matcher) (*ast.Ident, []ast.Expr) {
147+
ret1 := m.State["ret"].(*ast.Ident)
148+
var tail []ast.Expr
149+
if t, ok := m.State["tail"]; ok {
150+
tail, _ = t.([]ast.Expr)
154151
}
155-
return ret1, tail1
152+
return ret1, tail
156153
}
157154

158155
func renderTailString(pass *analysis.Pass, tail []ast.Expr) string {
159-
var tail1StringBuilder strings.Builder
156+
var tailStringBuilder strings.Builder
160157
if len(tail) != 0 {
161-
tail1StringBuilder.WriteString(", ")
162-
tail1StringBuilder.WriteString(report.RenderArgs(pass, tail))
158+
tailStringBuilder.WriteString(", ")
159+
tailStringBuilder.WriteString(report.RenderArgs(pass, tail))
163160
}
164-
return tail1StringBuilder.String()
161+
return tailStringBuilder.String()
165162
}
166163

167164
func negate(pass *analysis.Pass, expr ast.Expr) ast.Expr {

0 commit comments

Comments
 (0)