summaryrefslogtreecommitdiff
path: root/src/cmd/fix/math.go
blob: 2ec837eb00bfddc40cf1b5356f440e99656471b6 (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
// 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(mathFix)
}

var mathFix = fix{
	"math",
	"2011-09-29",
	math,
	`Remove the leading F from math functions such as Fabs.

http://codereview.appspot.com/5158043
`,
}

var mathRenames = []struct{ in, out string }{
	{"Fabs", "Abs"},
	{"Fdim", "Dim"},
	{"Fmax", "Max"},
	{"Fmin", "Min"},
	{"Fmod", "Mod"},
}

func math(f *ast.File) bool {
	if !imports(f, "math") {
		return false
	}

	fixed := false

	walk(f, func(n interface{}) {
		// Rename functions.
		if expr, ok := n.(ast.Expr); ok {
			for _, s := range mathRenames {
				if isPkgDot(expr, "math", s.in) {
					expr.(*ast.SelectorExpr).Sel.Name = s.out
					fixed = true
					return
				}
			}
		}
	})
	return fixed
}