pkg/terminal: use reflect.Value.IsValid to check a zero Value (#3450)
I searched the source code of Go, and found no usages of
"== (reflect.Value{})" and "!= (reflect.Value{})". I think
it's more idiomatic to use "IsValid" to check a zero Value.
This commit is contained in:
parent
7db57df266
commit
b5c9edccff
@ -134,13 +134,13 @@ func Print(out io.Writer, path string, reader io.Reader, startLine, endLine, arr
|
||||
|
||||
tokposval := nval.FieldByName("TokPos")
|
||||
tokval := nval.FieldByName("Tok")
|
||||
if tokposval != (reflect.Value{}) && tokval != (reflect.Value{}) {
|
||||
if tokposval.IsValid() && tokval.IsValid() {
|
||||
emit(tokval.Interface().(token.Token), tokposval.Interface().(token.Pos), token.NoPos)
|
||||
}
|
||||
|
||||
for _, kwname := range []string{"Case", "Begin", "Defer", "For", "Func", "Go", "Interface", "Map", "Return", "Select", "Struct", "Switch"} {
|
||||
kwposval := nval.FieldByName(kwname)
|
||||
if kwposval != (reflect.Value{}) {
|
||||
if kwposval.IsValid() {
|
||||
kwpos, ok := kwposval.Interface().(token.Pos)
|
||||
if ok && kwpos != token.NoPos {
|
||||
emit(token.ILLEGAL, kwpos, token.NoPos)
|
||||
|
||||
@ -181,7 +181,7 @@ func (v structAsStarlarkValue) Attr(name string) (starlark.Value, error) {
|
||||
return r, err
|
||||
}
|
||||
r := v.v.FieldByName(name)
|
||||
if r == (reflect.Value{}) {
|
||||
if !r.IsValid() {
|
||||
return starlark.None, fmt.Errorf("no field named %q in %T", name, v.v.Interface())
|
||||
}
|
||||
return v.env.interfaceToStarlarkValue(r.Interface()), nil
|
||||
@ -681,7 +681,7 @@ func unmarshalStarlarkValueIntl(val starlark.Value, dst reflect.Value, path stri
|
||||
}
|
||||
fieldName := string(k.(starlark.String))
|
||||
dstfield := dst.FieldByName(fieldName)
|
||||
if dstfield == (reflect.Value{}) {
|
||||
if !dstfield.IsValid() {
|
||||
return converr(fmt.Sprintf("unknown field %s", fieldName))
|
||||
}
|
||||
valfield, _, _ := val.Get(starlark.String(fieldName))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user