summaryrefslogtreecommitdiff
path: root/src/cmd/fix/sortslice.go
blob: 89267b847e3fe68b98885c289171d3ceb51e0dff (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
// Copyright 2011 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"go/ast"
)

func init() {
	register(sortsliceFix)
}

var sortsliceFix = fix{
	"sortslice",
	"2011-06-26",
	sortslice,
	`Adapt code from sort.[Float64|Int|String]Array to  sort.[Float64|Int|String]Slice.
		
http://codereview.appspot.com/4602054
http://codereview.appspot.com/4639041
`,
}

func sortslice(f *ast.File) (fixed bool) {
	if !imports(f, "sort") {
		return
	}

	walk(f, func(n interface{}) {
		s, ok := n.(*ast.SelectorExpr)
		if !ok || !isTopName(s.X, "sort") {
			return
		}

		switch s.Sel.String() {
		case "Float64Array":
			s.Sel.Name = "Float64Slice"
		case "IntArray":
			s.Sel.Name = "IntSlice"
		case "StringArray":
			s.Sel.Name = "StringSlice"
		default:
			return
		}

		fixed = true
	})

	return
}