Commit 846a368b authored by Russ Cox's avatar Russ Cox

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
parent d9763147
......@@ -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") {
foundCreate = true
sel, ok := lhs.(*ast.SelectorExpr)
if !ok || !isTopName(sel.X, "os") {
return false
}
if isPkgDot(lhs, "os", "O_TRUNC") {
switch sel.Sel.Name {
case "O_CREATE":
foundCreate = true
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")
}
......
......@@ -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
}
`,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment