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