1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: remove unnecessary IError() casts

This commit is contained in:
Alexander Medvednikov 2022-10-28 19:08:30 +03:00
parent daa2f90023
commit c6158e4519
28 changed files with 86 additions and 88 deletions

View File

@ -15,7 +15,7 @@ pub fn read(bytes_needed int) ![]u8 {
mut buffer := []u8{len: bytes_needed} mut buffer := []u8{len: bytes_needed}
status := C.SecRandomCopyBytes(C.SecRandomRef(0), bytes_needed, buffer.data) status := C.SecRandomCopyBytes(C.SecRandomRef(0), bytes_needed, buffer.data)
if status != 0 { if status != 0 {
return IError(&ReadError{}) return &ReadError{}
} }
return buffer return buffer
} }

View File

@ -24,7 +24,7 @@ pub fn read(bytes_needed int) ![]u8 {
rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) } rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) }
if rbytes == -1 { if rbytes == -1 {
unsafe { free(buffer) } unsafe { free(buffer) }
return IError(&ReadError{}) return &ReadError{}
} }
bytes_read += rbytes bytes_read += rbytes
} }

View File

@ -27,7 +27,7 @@ pub fn read(bytes_needed int) ![]u8 {
rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) } rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) }
if rbytes == -1 { if rbytes == -1 {
unsafe { free(buffer) } unsafe { free(buffer) }
return IError(&ReadError{}) return &ReadError{}
} }
bytes_read += rbytes bytes_read += rbytes
} }

View File

@ -19,7 +19,7 @@ pub fn read(bytes_needed int) ![]u8 {
// use bcrypt_use_system_preferred_rng because we passed null as algo // use bcrypt_use_system_preferred_rng because we passed null as algo
status := C.BCryptGenRandom(0, buffer.data, bytes_needed, rand.bcrypt_use_system_preferred_rng) status := C.BCryptGenRandom(0, buffer.data, bytes_needed, rand.bcrypt_use_system_preferred_rng)
if status != rand.status_success { if status != rand.status_success {
return IError(&ReadError{}) return &ReadError{}
} }
return buffer return buffer
} }

View File

@ -30,7 +30,7 @@ pub fn new_writer(config WriterConfig) &Writer {
// write writes a single record // write writes a single record
pub fn (mut w Writer) write(record []string) !bool { pub fn (mut w Writer) write(record []string) !bool {
if !valid_delim(w.delimiter) { if !valid_delim(w.delimiter) {
return IError(&InvalidDelimiterError{}) return &InvalidDelimiterError{}
} }
le := if w.use_crlf { '\r\n' } else { '\n' } le := if w.use_crlf { '\r\n' } else { '\n' }
for n, field_ in record { for n, field_ in record {

View File

@ -620,29 +620,29 @@ pub fn (mut fs FlagParser) finalize() ![]string {
if !fs.allow_unknown_args { if !fs.allow_unknown_args {
for a in remaining { for a in remaining {
if (a.len >= 2 && a[..2] == '--') || (a.len == 2 && a[0] == `-`) { if (a.len >= 2 && a[..2] == '--') || (a.len == 2 && a[0] == `-`) {
return IError(&UnkownFlagError{ return &UnkownFlagError{
flag: a flag: a
}) }
} }
} }
} }
if remaining.len < fs.min_free_args && fs.min_free_args > 0 { if remaining.len < fs.min_free_args && fs.min_free_args > 0 {
return IError(&ArgsCountError{ return &ArgsCountError{
want: fs.min_free_args want: fs.min_free_args
got: remaining.len got: remaining.len
}) }
} }
if remaining.len > fs.max_free_args && fs.max_free_args > 0 { if remaining.len > fs.max_free_args && fs.max_free_args > 0 {
return IError(&ArgsCountError{ return &ArgsCountError{
want: fs.max_free_args want: fs.max_free_args
got: remaining.len got: remaining.len
}) }
} }
if remaining.len > 0 && fs.max_free_args == 0 && fs.min_free_args == 0 { if remaining.len > 0 && fs.max_free_args == 0 && fs.min_free_args == 0 {
return IError(&ArgsCountError{ return &ArgsCountError{
want: 0 want: 0
got: remaining.len got: remaining.len
}) }
} }
remaining << fs.all_after_dashdash remaining << fs.all_after_dashdash
return remaining return remaining

View File

@ -38,21 +38,21 @@ pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader {
// read fufills the Reader interface // read fufills the Reader interface
pub fn (mut r BufferedReader) read(mut buf []u8) !int { pub fn (mut r BufferedReader) read(mut buf []u8) !int {
if r.end_of_stream { if r.end_of_stream {
return IError(Eof{}) return Eof{}
} }
// read data out of the buffer if we dont have any // read data out of the buffer if we dont have any
if r.needs_fill() { if r.needs_fill() {
if !r.fill_buffer() { if !r.fill_buffer() {
// end of stream // end of stream
return IError(Eof{}) return Eof{}
} }
} }
read := copy(mut buf, r.buf[r.offset..r.len]) read := copy(mut buf, r.buf[r.offset..r.len])
if read == 0 { if read == 0 {
return IError(NotExpected{ return NotExpected{
cause: 'invalid copy of buffer' cause: 'invalid copy of buffer'
code: -1 code: -1
}) }
} }
r.offset += read r.offset += read
return read return read

View File

@ -15,7 +15,7 @@ fn (mut s StringReader) read(mut buf []u8) !int {
eprintln('>>>> StringReader.read output buf.len: $buf.len') eprintln('>>>> StringReader.read output buf.len: $buf.len')
} }
if s.place > s.text.len + 1 { if s.place > s.text.len + 1 {
return IError(io.Eof{}) return io.Eof{}
} }
mut howmany := imin(buf.len, s.text.len - s.place) mut howmany := imin(buf.len, s.text.len - s.place)
xxx := s.text[s.place..s.place + howmany].bytes() xxx := s.text[s.place..s.place + howmany].bytes()

View File

@ -14,7 +14,7 @@ pub mut:
fn (mut b Buf) read(mut buf []u8) !int { fn (mut b Buf) read(mut buf []u8) !int {
if !(b.i < b.bytes.len) { if !(b.i < b.bytes.len) {
return IError(io.Eof{}) return io.Eof{}
} }
n := copy(mut buf, b.bytes[b.i..]) n := copy(mut buf, b.bytes[b.i..])
b.i += n b.i += n

View File

@ -9,7 +9,7 @@ mut:
fn (mut b Buf) read(mut buf []u8) !int { fn (mut b Buf) read(mut buf []u8) !int {
if !(b.i < b.bytes.len) { if !(b.i < b.bytes.len) {
return IError(Eof{}) return Eof{}
} }
n := copy(mut buf, b.bytes[b.i..]) n := copy(mut buf, b.bytes[b.i..])
b.i += n b.i += n
@ -46,7 +46,7 @@ mut:
fn (mut s StringReader) read(mut buf []u8) !int { fn (mut s StringReader) read(mut buf []u8) !int {
if s.place >= s.text.len { if s.place >= s.text.len {
return IError(Eof{}) return Eof{}
} }
read := copy(mut buf, s.text[s.place..].bytes()) read := copy(mut buf, s.text[s.place..].bytes())
s.place += read s.place += read

View File

@ -139,10 +139,10 @@ fn (stmt Stmt) get_error_msg() string {
pub fn (stmt Stmt) error(code int) IError { pub fn (stmt Stmt) error(code int) IError {
msg := stmt.get_error_msg() msg := stmt.get_error_msg()
return IError(&SQLError{ return &SQLError{
msg: '$msg ($code) ($stmt.query)' msg: '$msg ($code) ($stmt.query)'
code: code code: code
}) }
} }
fn (stmt Stmt) get_field_count() u16 { fn (stmt Stmt) get_field_count() u16 {

View File

@ -645,19 +645,19 @@ pub fn (err HeaderKeyError) code() int {
fn is_valid(header string) ! { fn is_valid(header string) ! {
for _, c in header { for _, c in header {
if int(c) >= 128 || !is_token(c) { if int(c) >= 128 || !is_token(c) {
return IError(HeaderKeyError{ return HeaderKeyError{
code: 1 code: 1
header: header header: header
invalid_char: c invalid_char: c
}) }
} }
} }
if header.len == 0 { if header.len == 0 {
return IError(HeaderKeyError{ return HeaderKeyError{
code: 2 code: 2
header: header header: header
invalid_char: 0 invalid_char: 0
}) }
} }
} }

View File

@ -223,7 +223,7 @@ pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) !int {
if res > 0 { if res > 0 {
return res return res
} else if res == 0 { } else if res == 0 {
return IError(io.Eof{}) return io.Eof{}
} else { } else {
match res { match res {
C.MBEDTLS_ERR_SSL_WANT_READ { C.MBEDTLS_ERR_SSL_WANT_READ {

View File

@ -274,7 +274,7 @@ pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) !int {
if res > 0 { if res > 0 {
return res return res
} else if res == 0 { } else if res == 0 {
return IError(io.Eof{}) return io.Eof{}
} else { } else {
err_res := ssl_error(res, s.ssl)! err_res := ssl_error(res, s.ssl)!
match err_res { match err_res {

View File

@ -135,10 +135,10 @@ pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) !int {
pub fn (c TcpConn) read(mut buf []u8) !int { pub fn (c TcpConn) read(mut buf []u8) !int {
return c.read_ptr(buf.data, buf.len) or { return c.read_ptr(buf.data, buf.len) or {
return IError(io.NotExpected{ return io.NotExpected{
cause: 'unexpected error in `read_ptr` function' cause: 'unexpected error in `read_ptr` function'
code: -1 code: -1
}) }
} }
} }

View File

@ -235,20 +235,20 @@ pub fn (mut f File) reopen(path string, mode string) ! {
// read implements the Reader interface. // read implements the Reader interface.
pub fn (f &File) read(mut buf []u8) !int { pub fn (f &File) read(mut buf []u8) !int {
if buf.len == 0 { if buf.len == 0 {
return IError(Eof{}) return Eof{}
} }
// the following is needed, because on FreeBSD, C.feof is a macro: // the following is needed, because on FreeBSD, C.feof is a macro:
nbytes := int(C.fread(buf.data, 1, buf.len, &C.FILE(f.cfile))) nbytes := int(C.fread(buf.data, 1, buf.len, &C.FILE(f.cfile)))
// if no bytes were read, check for errors and end-of-file. // if no bytes were read, check for errors and end-of-file.
if nbytes <= 0 { if nbytes <= 0 {
if C.feof(&C.FILE(f.cfile)) != 0 { if C.feof(&C.FILE(f.cfile)) != 0 {
return IError(Eof{}) return Eof{}
} }
if C.ferror(&C.FILE(f.cfile)) != 0 { if C.ferror(&C.FILE(f.cfile)) != 0 {
return IError(NotExpected{ return NotExpected{
cause: 'unexpected error from fread' cause: 'unexpected error from fread'
code: -1 code: -1
}) }
} }
} }
return nbytes return nbytes
@ -421,7 +421,7 @@ fn fread(ptr voidptr, item_size int, items int, stream &C.FILE) !int {
// read. The caller will get none on their next call because there will be // read. The caller will get none on their next call because there will be
// no data available and the end-of-file will be encountered again. // no data available and the end-of-file will be encountered again.
if C.feof(stream) != 0 { if C.feof(stream) != 0 {
return IError(Eof{}) return Eof{}
} }
// If fread encountered an error, return it. Note that fread and ferror do // If fread encountered an error, return it. Note that fread and ferror do
// not tell us what the error was, so we can't return anything more specific // not tell us what the error was, so we can't return anything more specific
@ -580,11 +580,11 @@ pub fn (err SizeOfTypeIs0Error) msg() string {
} }
fn error_file_not_opened() IError { fn error_file_not_opened() IError {
return IError(&FileNotOpenedError{}) return &FileNotOpenedError{}
} }
fn error_size_of_type_0() IError { fn error_size_of_type_0() IError {
return IError(&SizeOfTypeIs0Error{}) return &SizeOfTypeIs0Error{}
} }
// read_struct reads a single struct of type `T` // read_struct reads a single struct of type `T`

View File

@ -21,10 +21,10 @@ pub struct ErrSizeOfTypeIs0 {
code int code int
} }
fn error_file_not_opened() IError { fn error_file_not_opened() IError {
return IError(&ErrFileNotOpened{}) return (&ErrFileNotOpened{})
} }
fn error_size_of_type_0() IError { fn error_size_of_type_0() IError {
return IError(&ErrSizeOfTypeIs0{}) return (&ErrSizeOfTypeIs0{})
} }
*/ */
pub fn open_file(path string, mode string, options ...int) !File { pub fn open_file(path string, mode string, options ...int) !File {

View File

@ -455,7 +455,7 @@ pub fn (err ExecutableNotFoundError) msg() string {
} }
fn error_failed_to_find_executable() IError { fn error_failed_to_find_executable() IError {
return IError(&ExecutableNotFoundError{}) return &ExecutableNotFoundError{}
} }
// find_abs_path_of_executable walks the environment PATH, just like most shell do, it returns // find_abs_path_of_executable walks the environment PATH, just like most shell do, it returns

View File

@ -87,16 +87,16 @@ fn parse_range(input string) ?Range {
fn parse_comparator_set(input string) ?ComparatorSet { fn parse_comparator_set(input string) ?ComparatorSet {
raw_comparators := input.split(semver.comparator_sep) raw_comparators := input.split(semver.comparator_sep)
if raw_comparators.len > 2 { if raw_comparators.len > 2 {
return IError(&InvalidComparatorFormatError{ return &InvalidComparatorFormatError{
msg: 'Invalid format of comparator set for input "$input"' msg: 'Invalid format of comparator set for input "$input"'
}) }
} }
mut comparators := []Comparator{} mut comparators := []Comparator{}
for raw_comp in raw_comparators { for raw_comp in raw_comparators {
c := parse_comparator(raw_comp) or { c := parse_comparator(raw_comp) or {
return IError(&InvalidComparatorFormatError{ return &InvalidComparatorFormatError{
msg: 'Invalid comparator "$raw_comp" in input "$input"' msg: 'Invalid comparator "$raw_comp" in input "$input"'
}) }
} }
comparators << c comparators << c
} }

View File

@ -40,14 +40,12 @@ pub fn (err InvalidVersionFormatError) msg() string {
// from returns a `Version` structure parsed from `input` `string`. // from returns a `Version` structure parsed from `input` `string`.
pub fn from(input string) ?Version { pub fn from(input string) ?Version {
if input.len == 0 { if input.len == 0 {
return IError(&EmptyInputError{}) return &EmptyInputError{}
} }
raw_version := parse(input) raw_version := parse(input)
version := raw_version.validate() or { version := raw_version.validate() or { return &InvalidVersionFormatError{
return IError(&InvalidVersionFormatError{ input: input
input: input } }
})
}
return version return version
} }

View File

@ -110,10 +110,10 @@ pub fn connect(path string) !DB {
db := &C.sqlite3(0) db := &C.sqlite3(0)
code := C.sqlite3_open(&char(path.str), &db) code := C.sqlite3_open(&char(path.str), &db)
if code != 0 { if code != 0 {
return IError(&SQLError{ return &SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) } msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code code: code
}) }
} }
return DB{ return DB{
conn: db conn: db
@ -129,10 +129,10 @@ pub fn (mut db DB) close() !bool {
if code == 0 { if code == 0 {
db.is_open = false db.is_open = false
} else { } else {
return IError(&SQLError{ return &SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) } msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code code: code
}) }
} }
return true // successfully closed return true // successfully closed
} }
@ -223,15 +223,15 @@ pub fn (db &DB) exec_one(query string) !Row {
unsafe { rows.free() } unsafe { rows.free() }
} }
if rows.len == 0 { if rows.len == 0 {
return IError(&SQLError{ return &SQLError{
msg: 'No rows' msg: 'No rows'
code: code code: code
}) }
} else if code != 101 { } else if code != 101 {
return IError(&SQLError{ return &SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) } msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code code: code
}) }
} }
res := rows[0] res := rows[0]
return res return res

View File

@ -37,7 +37,7 @@ fn test_decode_a() {
fn (mut b TestReader) read(mut buf []u8) !int { fn (mut b TestReader) read(mut buf []u8) !int {
if !(b.i < b.bytes.len) { if !(b.i < b.bytes.len) {
return IError(io.Eof{}) return io.Eof{}
} }
n := copy(mut buf, b.bytes[b.i..]) n := copy(mut buf, b.bytes[b.i..])
b.i += n b.i += n

View File

@ -28,10 +28,10 @@ fn (e MyError) code() int {
// An example function that returns a custom error. // An example function that returns a custom error.
fn foo() ?string { fn foo() ?string {
return IError(MyError{ return MyError{
msg: 'foo' msg: 'foo'
blah: 'world' blah: 'world'
}) }
} }
fn test_interface_embedding_smartcast() { fn test_interface_embedding_smartcast() {

View File

@ -16,9 +16,9 @@ fn foo() ?string {
} }
fn bar() !(string, int) { fn bar() !(string, int) {
a := foo() or { return IError(Err{ a := foo() or { return Err{
msg: 'error test' msg: 'error test'
}) } } }
return a, 1 return a, 1
} }

View File

@ -12,7 +12,7 @@ fn (err MyError) code() int {
} }
fn foo() int|none|IError { fn foo() int|none|IError {
return IError(MyError{}) return MyError{}
} }
fn test_string_optional_none() { fn test_string_optional_none() {

View File

@ -49,8 +49,8 @@ pub fn (mut app App) get_csrf_token() ?string {
if app.csrf_cookie_value != '' { if app.csrf_cookie_value != '' {
return app.csrf_cookie_value return app.csrf_cookie_value
} else { } else {
return IError(CsrfError{ return CsrfError{
m: 'The CSRF-Token-Value is empty. Please check if you have setted a cookie!' m: 'The CSRF-Token-Value is empty. Please check if you have setted a cookie!'
}) }
} }
} }

View File

@ -24,7 +24,7 @@ fn parse_attrs(name string, attrs []string) !([]http.Method, string) {
} }
if attr.starts_with('/') { if attr.starts_with('/') {
if path != '' { if path != '' {
return IError(http.MultiplePathAttributesError{}) return http.MultiplePathAttributesError{}
} }
path = attr path = attr
x.delete(i) x.delete(i)
@ -33,9 +33,9 @@ fn parse_attrs(name string, attrs []string) !([]http.Method, string) {
i++ i++
} }
if x.len > 0 { if x.len > 0 {
return IError(http.UnexpectedExtraAttributeError{ return http.UnexpectedExtraAttributeError{
attributes: x attributes: x
}) }
} }
if methods.len == 0 { if methods.len == 0 {
methods = [http.Method.get] methods = [http.Method.get]

View File

@ -104,11 +104,11 @@ fn (mut p Parser) next() {
fn (mut p Parser) next_with_err() ! { fn (mut p Parser) next_with_err() ! {
p.next() p.next()
if p.tok.kind == .error { if p.tok.kind == .error {
return IError(DecodeError{ return DecodeError{
line: p.tok.line line: p.tok.line
column: p.tok.full_col() column: p.tok.full_col()
message: p.tok.lit.bytestr() message: p.tok.lit.bytestr()
}) }
} }
} }
@ -145,18 +145,18 @@ fn (mut p Parser) decode() !Any {
p.next_with_err()! p.next_with_err()!
fi := p.decode_value()! fi := p.decode_value()!
if p.tok.kind != .eof { if p.tok.kind != .eof {
return IError(InvalidTokenError{ return InvalidTokenError{
token: p.tok token: p.tok
}) }
} }
return fi return fi
} }
fn (mut p Parser) decode_value() !Any { fn (mut p Parser) decode_value() !Any {
if p.n_level + 1 == 500 { if p.n_level + 1 == 500 {
return IError(DecodeError{ return DecodeError{
message: 'reached maximum nesting level of 500' message: 'reached maximum nesting level of 500'
}) }
} }
match p.tok.kind { match p.tok.kind {
.lsbr { .lsbr {
@ -200,9 +200,9 @@ fn (mut p Parser) decode_value() !Any {
return Any(str) return Any(str)
} }
else { else {
return IError(InvalidTokenError{ return InvalidTokenError{
token: p.tok token: p.tok
}) }
} }
} }
return Any(null) return Any(null)
@ -219,15 +219,15 @@ fn (mut p Parser) decode_array() !Any {
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.next_with_err()! p.next_with_err()!
if p.tok.kind == .rsbr { if p.tok.kind == .rsbr {
return IError(InvalidTokenError{ return InvalidTokenError{
token: p.tok token: p.tok
}) }
} }
} else if p.tok.kind != .rsbr { } else if p.tok.kind != .rsbr {
return IError(UnknownTokenError{ return UnknownTokenError{
token: p.tok token: p.tok
kind: .array kind: .array
}) }
} }
} }
p.next_with_err()! p.next_with_err()!
@ -241,28 +241,28 @@ fn (mut p Parser) decode_object() !Any {
p.n_level++ p.n_level++
for p.tok.kind != .rcbr { for p.tok.kind != .rcbr {
if p.tok.kind != .str_ { if p.tok.kind != .str_ {
return IError(InvalidTokenError{ return InvalidTokenError{
token: p.tok token: p.tok
expected: .str_ expected: .str_
}) }
} }
cur_key := p.tok.lit.bytestr() cur_key := p.tok.lit.bytestr()
p.next_with_err()! p.next_with_err()!
if p.tok.kind != .colon { if p.tok.kind != .colon {
return IError(InvalidTokenError{ return InvalidTokenError{
token: p.tok token: p.tok
expected: .colon expected: .colon
}) }
} }
p.next_with_err()! p.next_with_err()!
fields[cur_key] = p.decode_value()! fields[cur_key] = p.decode_value()!
if p.tok.kind != .comma && p.tok.kind != .rcbr { if p.tok.kind != .comma && p.tok.kind != .rcbr {
return IError(UnknownTokenError{ return UnknownTokenError{
token: p.tok token: p.tok
kind: .object kind: .object
}) }
} else if p.tok.kind == .comma { } else if p.tok.kind == .comma {
p.next_with_err()! p.next_with_err()!
} }