1
0
mirror of https://github.com/golang/go synced 2024-11-20 00:04:43 -07:00

gofix: be more conservative about rewrite to os.Create

Rewrite only if we understood all the flags we saw.

R=r
CC=golang-dev
https://golang.org/cl/4376046
This commit is contained in:
Russ Cox 2011-04-08 10:59:25 -04:00
parent d976314776
commit 846a368b88
2 changed files with 17 additions and 10 deletions

View File

@ -70,7 +70,6 @@ func osopen(f *ast.File) bool {
func isCreateFlag(flag ast.Expr) bool {
foundCreate := false
foundTrunc := false
foundAppend := false
// OR'ing of flags: is O_CREATE on? + or | would be fine; we just look for os.O_CREATE
// and don't worry about the actual opeator.
p := flag.Pos()
@ -80,14 +79,21 @@ func isCreateFlag(flag ast.Expr) bool {
if isBinary {
lhs = expr.Y
}
if isPkgDot(lhs, "os", "O_CREATE") {
sel, ok := lhs.(*ast.SelectorExpr)
if !ok || !isTopName(sel.X, "os") {
return false
}
switch sel.Sel.Name {
case "O_CREATE":
foundCreate = true
}
if isPkgDot(lhs, "os", "O_TRUNC") {
case "O_TRUNC":
foundTrunc = true
}
if isPkgDot(lhs, "os", "O_APPEND") {
foundAppend = true
case "O_RDONLY", "O_WRONLY", "O_RDWR":
// okay
default:
// Unexpected flag, like O_APPEND or O_EXCL.
// Be conservative and do not rewrite.
return false
}
if !isBinary {
break
@ -97,9 +103,6 @@ func isCreateFlag(flag ast.Expr) bool {
if !foundCreate {
return false
}
if foundAppend {
return false
}
if !foundTrunc {
warn(p, "rewrote os.Open with O_CREATE but not O_TRUNC to os.Create")
}

View File

@ -28,6 +28,8 @@ func f() {
os.Open(a, os.O_CREATE|os.O_TRUNC, 0664)
os.Open(a, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
os.Open(a, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
os.Open(a, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
os.Open(a, os.O_SURPRISE|os.O_CREATE, 0666)
_ = os.O_CREAT
}
`,
@ -48,6 +50,8 @@ func f() {
os.Create(a)
os.Create(a)
os.OpenFile(a, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
os.OpenFile(a, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
os.OpenFile(a, os.O_SURPRISE|os.O_CREATE, 0666)
_ = os.O_CREATE
}
`,