@@ -57,45 +57,46 @@ func sortslice(pass *analysis.Pass) {
57
57
i := sig .Params ().At (0 )
58
58
j := sig .Params ().At (1 )
59
59
60
- ret := lit .Body .List [0 ].(* ast.ReturnStmt )
61
- if compare , ok := ret .Results [0 ].(* ast.BinaryExpr ); ok && compare .Op == token .LSS {
62
- // isIndex reports whether e is s[v].
63
- isIndex := func (e ast.Expr , v * types.Var ) bool {
64
- index , ok := e .(* ast.IndexExpr )
65
- return ok &&
66
- equalSyntax (index .X , s ) &&
67
- is [* ast.Ident ](index .Index ) &&
68
- info .Uses [index .Index .(* ast.Ident )] == v
69
- }
70
- if isIndex (compare .X , i ) && isIndex (compare .Y , j ) {
71
- // Have: sort.Slice(s, func(i, j int) bool { return s[i] < s[j] })
60
+ if ret , ok := lit .Body .List [0 ].(* ast.ReturnStmt ); ok {
61
+ if compare , ok := ret .Results [0 ].(* ast.BinaryExpr ); ok && compare .Op == token .LSS {
62
+ // isIndex reports whether e is s[v].
63
+ isIndex := func (e ast.Expr , v * types.Var ) bool {
64
+ index , ok := e .(* ast.IndexExpr )
65
+ return ok &&
66
+ equalSyntax (index .X , s ) &&
67
+ is [* ast.Ident ](index .Index ) &&
68
+ info .Uses [index .Index .(* ast.Ident )] == v
69
+ }
70
+ if isIndex (compare .X , i ) && isIndex (compare .Y , j ) {
71
+ // Have: sort.Slice(s, func(i, j int) bool { return s[i] < s[j] })
72
72
73
- _ , prefix , importEdits := analysisinternal .AddImport (
74
- info , file , "slices" , "slices" , "Sort" , call .Pos ())
73
+ _ , prefix , importEdits := analysisinternal .AddImport (
74
+ info , file , "slices" , "slices" , "Sort" , call .Pos ())
75
75
76
- pass .Report (analysis.Diagnostic {
77
- // Highlight "sort.Slice".
78
- Pos : call .Fun .Pos (),
79
- End : call .Fun .End (),
80
- Category : "sortslice" ,
81
- Message : fmt .Sprintf ("sort.Slice can be modernized using slices.Sort" ),
82
- SuggestedFixes : []analysis.SuggestedFix {{
83
- Message : fmt .Sprintf ("Replace sort.Slice call by slices.Sort" ),
84
- TextEdits : append (importEdits , []analysis.TextEdit {
85
- {
86
- // Replace sort.Slice with slices.Sort.
87
- Pos : call .Fun .Pos (),
88
- End : call .Fun .End (),
89
- NewText : []byte (prefix + "Sort" ),
90
- },
91
- {
92
- // Eliminate FuncLit.
93
- Pos : call .Args [0 ].End (),
94
- End : call .Rparen ,
95
- },
96
- }... ),
97
- }},
98
- })
76
+ pass .Report (analysis.Diagnostic {
77
+ // Highlight "sort.Slice".
78
+ Pos : call .Fun .Pos (),
79
+ End : call .Fun .End (),
80
+ Category : "sortslice" ,
81
+ Message : fmt .Sprintf ("sort.Slice can be modernized using slices.Sort" ),
82
+ SuggestedFixes : []analysis.SuggestedFix {{
83
+ Message : fmt .Sprintf ("Replace sort.Slice call by slices.Sort" ),
84
+ TextEdits : append (importEdits , []analysis.TextEdit {
85
+ {
86
+ // Replace sort.Slice with slices.Sort.
87
+ Pos : call .Fun .Pos (),
88
+ End : call .Fun .End (),
89
+ NewText : []byte (prefix + "Sort" ),
90
+ },
91
+ {
92
+ // Eliminate FuncLit.
93
+ Pos : call .Args [0 ].End (),
94
+ End : call .Rparen ,
95
+ },
96
+ }... ),
97
+ }},
98
+ })
99
+ }
99
100
}
100
101
}
101
102
}
0 commit comments