summaryrefslogtreecommitdiff
path: root/src/cmd/gofix/sortslice.go
blob: b9c108b5a1c2260f4b44c5f229490014c5591486 (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
// 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(fix{
		"sortslice",
		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
}