From c6158e4519b8b495f9b46d4e1916f3927df3bb22 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 28 Oct 2022 19:08:30 +0300 Subject: [PATCH] all: remove unnecessary IError() casts --- vlib/crypto/rand/rand_darwin.c.v | 2 +- vlib/crypto/rand/rand_linux.c.v | 2 +- vlib/crypto/rand/rand_solaris.c.v | 2 +- vlib/crypto/rand/rand_windows.c.v | 2 +- vlib/encoding/csv/writer.v | 2 +- vlib/flag/flag.v | 16 ++++----- vlib/io/buffered_reader.v | 8 ++--- vlib/io/custom_string_reading_test.v | 2 +- vlib/io/io_test.v | 2 +- vlib/io/reader_test.v | 4 +-- vlib/mysql/stmt.c.v | 4 +-- vlib/net/http/header.v | 8 ++--- vlib/net/mbedtls/ssl_connection.v | 2 +- vlib/net/openssl/ssl_connection.v | 2 +- vlib/net/tcp.v | 4 +-- vlib/os/file.c.v | 14 ++++---- vlib/os/file.js.v | 4 +-- vlib/os/os.v | 2 +- vlib/semver/range.v | 8 ++--- vlib/semver/semver.v | 10 +++--- vlib/sqlite/sqlite.v | 16 ++++----- vlib/v/gen/js/sourcemap/vlq/vlq_decode_test.v | 2 +- .../interface_embedding_smartcast_test.v | 4 +-- vlib/v/tests/results_multi_return_test.v | 4 +-- vlib/v/tests/string_optional_none_test.v | 2 +- vlib/vweb/csrf/create_cookie.v | 4 +-- vlib/vweb/parse.v | 6 ++-- vlib/x/json2/decoder.v | 36 +++++++++---------- 28 files changed, 86 insertions(+), 88 deletions(-) diff --git a/vlib/crypto/rand/rand_darwin.c.v b/vlib/crypto/rand/rand_darwin.c.v index 7737ff2d4b..bea901edc2 100644 --- a/vlib/crypto/rand/rand_darwin.c.v +++ b/vlib/crypto/rand/rand_darwin.c.v @@ -15,7 +15,7 @@ pub fn read(bytes_needed int) ![]u8 { mut buffer := []u8{len: bytes_needed} status := C.SecRandomCopyBytes(C.SecRandomRef(0), bytes_needed, buffer.data) if status != 0 { - return IError(&ReadError{}) + return &ReadError{} } return buffer } diff --git a/vlib/crypto/rand/rand_linux.c.v b/vlib/crypto/rand/rand_linux.c.v index 910e10d3fa..fafacd6094 100644 --- a/vlib/crypto/rand/rand_linux.c.v +++ b/vlib/crypto/rand/rand_linux.c.v @@ -24,7 +24,7 @@ pub fn read(bytes_needed int) ![]u8 { rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) } if rbytes == -1 { unsafe { free(buffer) } - return IError(&ReadError{}) + return &ReadError{} } bytes_read += rbytes } diff --git a/vlib/crypto/rand/rand_solaris.c.v b/vlib/crypto/rand/rand_solaris.c.v index 1c33dceced..31bf6bf991 100644 --- a/vlib/crypto/rand/rand_solaris.c.v +++ b/vlib/crypto/rand/rand_solaris.c.v @@ -27,7 +27,7 @@ pub fn read(bytes_needed int) ![]u8 { rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) } if rbytes == -1 { unsafe { free(buffer) } - return IError(&ReadError{}) + return &ReadError{} } bytes_read += rbytes } diff --git a/vlib/crypto/rand/rand_windows.c.v b/vlib/crypto/rand/rand_windows.c.v index c1e653f17f..dde0b48c9c 100644 --- a/vlib/crypto/rand/rand_windows.c.v +++ b/vlib/crypto/rand/rand_windows.c.v @@ -19,7 +19,7 @@ pub fn read(bytes_needed int) ![]u8 { // 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) if status != rand.status_success { - return IError(&ReadError{}) + return &ReadError{} } return buffer } diff --git a/vlib/encoding/csv/writer.v b/vlib/encoding/csv/writer.v index f400e78af2..f1f6b87c5e 100644 --- a/vlib/encoding/csv/writer.v +++ b/vlib/encoding/csv/writer.v @@ -30,7 +30,7 @@ pub fn new_writer(config WriterConfig) &Writer { // write writes a single record pub fn (mut w Writer) write(record []string) !bool { if !valid_delim(w.delimiter) { - return IError(&InvalidDelimiterError{}) + return &InvalidDelimiterError{} } le := if w.use_crlf { '\r\n' } else { '\n' } for n, field_ in record { diff --git a/vlib/flag/flag.v b/vlib/flag/flag.v index 2468416459..967d43c529 100644 --- a/vlib/flag/flag.v +++ b/vlib/flag/flag.v @@ -620,29 +620,29 @@ pub fn (mut fs FlagParser) finalize() ![]string { if !fs.allow_unknown_args { for a in remaining { if (a.len >= 2 && a[..2] == '--') || (a.len == 2 && a[0] == `-`) { - return IError(&UnkownFlagError{ + return &UnkownFlagError{ flag: a - }) + } } } } if remaining.len < fs.min_free_args && fs.min_free_args > 0 { - return IError(&ArgsCountError{ + return &ArgsCountError{ want: fs.min_free_args got: remaining.len - }) + } } if remaining.len > fs.max_free_args && fs.max_free_args > 0 { - return IError(&ArgsCountError{ + return &ArgsCountError{ want: fs.max_free_args got: remaining.len - }) + } } if remaining.len > 0 && fs.max_free_args == 0 && fs.min_free_args == 0 { - return IError(&ArgsCountError{ + return &ArgsCountError{ want: 0 got: remaining.len - }) + } } remaining << fs.all_after_dashdash return remaining diff --git a/vlib/io/buffered_reader.v b/vlib/io/buffered_reader.v index 4f9b1bcf81..c92302de76 100644 --- a/vlib/io/buffered_reader.v +++ b/vlib/io/buffered_reader.v @@ -38,21 +38,21 @@ pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader { // read fufills the Reader interface pub fn (mut r BufferedReader) read(mut buf []u8) !int { if r.end_of_stream { - return IError(Eof{}) + return Eof{} } // read data out of the buffer if we dont have any if r.needs_fill() { if !r.fill_buffer() { // end of stream - return IError(Eof{}) + return Eof{} } } read := copy(mut buf, r.buf[r.offset..r.len]) if read == 0 { - return IError(NotExpected{ + return NotExpected{ cause: 'invalid copy of buffer' code: -1 - }) + } } r.offset += read return read diff --git a/vlib/io/custom_string_reading_test.v b/vlib/io/custom_string_reading_test.v index f0b2f901bd..983980c3e1 100644 --- a/vlib/io/custom_string_reading_test.v +++ b/vlib/io/custom_string_reading_test.v @@ -15,7 +15,7 @@ fn (mut s StringReader) read(mut buf []u8) !int { eprintln('>>>> StringReader.read output buf.len: $buf.len') } if s.place > s.text.len + 1 { - return IError(io.Eof{}) + return io.Eof{} } mut howmany := imin(buf.len, s.text.len - s.place) xxx := s.text[s.place..s.place + howmany].bytes() diff --git a/vlib/io/io_test.v b/vlib/io/io_test.v index 396e359314..8e9f35aa92 100644 --- a/vlib/io/io_test.v +++ b/vlib/io/io_test.v @@ -14,7 +14,7 @@ pub mut: fn (mut b Buf) read(mut buf []u8) !int { if !(b.i < b.bytes.len) { - return IError(io.Eof{}) + return io.Eof{} } n := copy(mut buf, b.bytes[b.i..]) b.i += n diff --git a/vlib/io/reader_test.v b/vlib/io/reader_test.v index 3e7f72f64b..adfeb62f3f 100644 --- a/vlib/io/reader_test.v +++ b/vlib/io/reader_test.v @@ -9,7 +9,7 @@ mut: fn (mut b Buf) read(mut buf []u8) !int { if !(b.i < b.bytes.len) { - return IError(Eof{}) + return Eof{} } n := copy(mut buf, b.bytes[b.i..]) b.i += n @@ -46,7 +46,7 @@ mut: fn (mut s StringReader) read(mut buf []u8) !int { if s.place >= s.text.len { - return IError(Eof{}) + return Eof{} } read := copy(mut buf, s.text[s.place..].bytes()) s.place += read diff --git a/vlib/mysql/stmt.c.v b/vlib/mysql/stmt.c.v index 56b7b4e245..487ac9ea43 100644 --- a/vlib/mysql/stmt.c.v +++ b/vlib/mysql/stmt.c.v @@ -139,10 +139,10 @@ fn (stmt Stmt) get_error_msg() string { pub fn (stmt Stmt) error(code int) IError { msg := stmt.get_error_msg() - return IError(&SQLError{ + return &SQLError{ msg: '$msg ($code) ($stmt.query)' code: code - }) + } } fn (stmt Stmt) get_field_count() u16 { diff --git a/vlib/net/http/header.v b/vlib/net/http/header.v index 05ac323271..db296e1d6c 100644 --- a/vlib/net/http/header.v +++ b/vlib/net/http/header.v @@ -645,19 +645,19 @@ pub fn (err HeaderKeyError) code() int { fn is_valid(header string) ! { for _, c in header { if int(c) >= 128 || !is_token(c) { - return IError(HeaderKeyError{ + return HeaderKeyError{ code: 1 header: header invalid_char: c - }) + } } } if header.len == 0 { - return IError(HeaderKeyError{ + return HeaderKeyError{ code: 2 header: header invalid_char: 0 - }) + } } } diff --git a/vlib/net/mbedtls/ssl_connection.v b/vlib/net/mbedtls/ssl_connection.v index 41ea28bdd8..8fa50bfe71 100644 --- a/vlib/net/mbedtls/ssl_connection.v +++ b/vlib/net/mbedtls/ssl_connection.v @@ -223,7 +223,7 @@ pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) !int { if res > 0 { return res } else if res == 0 { - return IError(io.Eof{}) + return io.Eof{} } else { match res { C.MBEDTLS_ERR_SSL_WANT_READ { diff --git a/vlib/net/openssl/ssl_connection.v b/vlib/net/openssl/ssl_connection.v index 97230de7c5..d38709cb68 100644 --- a/vlib/net/openssl/ssl_connection.v +++ b/vlib/net/openssl/ssl_connection.v @@ -274,7 +274,7 @@ pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) !int { if res > 0 { return res } else if res == 0 { - return IError(io.Eof{}) + return io.Eof{} } else { err_res := ssl_error(res, s.ssl)! match err_res { diff --git a/vlib/net/tcp.v b/vlib/net/tcp.v index 3899269e07..cb27dd319c 100644 --- a/vlib/net/tcp.v +++ b/vlib/net/tcp.v @@ -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 { return c.read_ptr(buf.data, buf.len) or { - return IError(io.NotExpected{ + return io.NotExpected{ cause: 'unexpected error in `read_ptr` function' code: -1 - }) + } } } diff --git a/vlib/os/file.c.v b/vlib/os/file.c.v index 4714633aef..8d234c8445 100644 --- a/vlib/os/file.c.v +++ b/vlib/os/file.c.v @@ -235,20 +235,20 @@ pub fn (mut f File) reopen(path string, mode string) ! { // read implements the Reader interface. pub fn (f &File) read(mut buf []u8) !int { if buf.len == 0 { - return IError(Eof{}) + return Eof{} } // 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))) // if no bytes were read, check for errors and end-of-file. if nbytes <= 0 { if C.feof(&C.FILE(f.cfile)) != 0 { - return IError(Eof{}) + return Eof{} } if C.ferror(&C.FILE(f.cfile)) != 0 { - return IError(NotExpected{ + return NotExpected{ cause: 'unexpected error from fread' code: -1 - }) + } } } 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 // no data available and the end-of-file will be encountered again. if C.feof(stream) != 0 { - return IError(Eof{}) + return Eof{} } // 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 @@ -580,11 +580,11 @@ pub fn (err SizeOfTypeIs0Error) msg() string { } fn error_file_not_opened() IError { - return IError(&FileNotOpenedError{}) + return &FileNotOpenedError{} } fn error_size_of_type_0() IError { - return IError(&SizeOfTypeIs0Error{}) + return &SizeOfTypeIs0Error{} } // read_struct reads a single struct of type `T` diff --git a/vlib/os/file.js.v b/vlib/os/file.js.v index cf67f5679f..4ebd6d7549 100644 --- a/vlib/os/file.js.v +++ b/vlib/os/file.js.v @@ -21,10 +21,10 @@ pub struct ErrSizeOfTypeIs0 { code int } fn error_file_not_opened() IError { - return IError(&ErrFileNotOpened{}) + return (&ErrFileNotOpened{}) } fn error_size_of_type_0() IError { - return IError(&ErrSizeOfTypeIs0{}) + return (&ErrSizeOfTypeIs0{}) } */ pub fn open_file(path string, mode string, options ...int) !File { diff --git a/vlib/os/os.v b/vlib/os/os.v index c8ef218c8b..6bac8af978 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -455,7 +455,7 @@ pub fn (err ExecutableNotFoundError) msg() string { } 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 diff --git a/vlib/semver/range.v b/vlib/semver/range.v index 2f77bb7327..81174fea7b 100644 --- a/vlib/semver/range.v +++ b/vlib/semver/range.v @@ -87,16 +87,16 @@ fn parse_range(input string) ?Range { fn parse_comparator_set(input string) ?ComparatorSet { raw_comparators := input.split(semver.comparator_sep) if raw_comparators.len > 2 { - return IError(&InvalidComparatorFormatError{ + return &InvalidComparatorFormatError{ msg: 'Invalid format of comparator set for input "$input"' - }) + } } mut comparators := []Comparator{} for raw_comp in raw_comparators { c := parse_comparator(raw_comp) or { - return IError(&InvalidComparatorFormatError{ + return &InvalidComparatorFormatError{ msg: 'Invalid comparator "$raw_comp" in input "$input"' - }) + } } comparators << c } diff --git a/vlib/semver/semver.v b/vlib/semver/semver.v index 57089b05b2..48754df73d 100644 --- a/vlib/semver/semver.v +++ b/vlib/semver/semver.v @@ -40,14 +40,12 @@ pub fn (err InvalidVersionFormatError) msg() string { // from returns a `Version` structure parsed from `input` `string`. pub fn from(input string) ?Version { if input.len == 0 { - return IError(&EmptyInputError{}) + return &EmptyInputError{} } raw_version := parse(input) - version := raw_version.validate() or { - return IError(&InvalidVersionFormatError{ - input: input - }) - } + version := raw_version.validate() or { return &InvalidVersionFormatError{ + input: input + } } return version } diff --git a/vlib/sqlite/sqlite.v b/vlib/sqlite/sqlite.v index fcb5332d82..ed308844cf 100644 --- a/vlib/sqlite/sqlite.v +++ b/vlib/sqlite/sqlite.v @@ -110,10 +110,10 @@ pub fn connect(path string) !DB { db := &C.sqlite3(0) code := C.sqlite3_open(&char(path.str), &db) if code != 0 { - return IError(&SQLError{ + return &SQLError{ msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) } code: code - }) + } } return DB{ conn: db @@ -129,10 +129,10 @@ pub fn (mut db DB) close() !bool { if code == 0 { db.is_open = false } else { - return IError(&SQLError{ + return &SQLError{ msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) } code: code - }) + } } return true // successfully closed } @@ -223,15 +223,15 @@ pub fn (db &DB) exec_one(query string) !Row { unsafe { rows.free() } } if rows.len == 0 { - return IError(&SQLError{ + return &SQLError{ msg: 'No rows' code: code - }) + } } else if code != 101 { - return IError(&SQLError{ + return &SQLError{ msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) } code: code - }) + } } res := rows[0] return res diff --git a/vlib/v/gen/js/sourcemap/vlq/vlq_decode_test.v b/vlib/v/gen/js/sourcemap/vlq/vlq_decode_test.v index 981d19ba8a..365918d6f1 100644 --- a/vlib/v/gen/js/sourcemap/vlq/vlq_decode_test.v +++ b/vlib/v/gen/js/sourcemap/vlq/vlq_decode_test.v @@ -37,7 +37,7 @@ fn test_decode_a() { fn (mut b TestReader) read(mut buf []u8) !int { if !(b.i < b.bytes.len) { - return IError(io.Eof{}) + return io.Eof{} } n := copy(mut buf, b.bytes[b.i..]) b.i += n diff --git a/vlib/v/tests/interface_embedding_smartcast_test.v b/vlib/v/tests/interface_embedding_smartcast_test.v index 5b1d87b79c..537aff4fc1 100644 --- a/vlib/v/tests/interface_embedding_smartcast_test.v +++ b/vlib/v/tests/interface_embedding_smartcast_test.v @@ -28,10 +28,10 @@ fn (e MyError) code() int { // An example function that returns a custom error. fn foo() ?string { - return IError(MyError{ + return MyError{ msg: 'foo' blah: 'world' - }) + } } fn test_interface_embedding_smartcast() { diff --git a/vlib/v/tests/results_multi_return_test.v b/vlib/v/tests/results_multi_return_test.v index f8b5a2ed06..64096f5eb5 100644 --- a/vlib/v/tests/results_multi_return_test.v +++ b/vlib/v/tests/results_multi_return_test.v @@ -16,9 +16,9 @@ fn foo() ?string { } fn bar() !(string, int) { - a := foo() or { return IError(Err{ + a := foo() or { return Err{ msg: 'error test' - }) } + } } return a, 1 } diff --git a/vlib/v/tests/string_optional_none_test.v b/vlib/v/tests/string_optional_none_test.v index b381ff1254..e785fc42c1 100644 --- a/vlib/v/tests/string_optional_none_test.v +++ b/vlib/v/tests/string_optional_none_test.v @@ -12,7 +12,7 @@ fn (err MyError) code() int { } fn foo() int|none|IError { - return IError(MyError{}) + return MyError{} } fn test_string_optional_none() { diff --git a/vlib/vweb/csrf/create_cookie.v b/vlib/vweb/csrf/create_cookie.v index 5584fe0b49..4b9e806d40 100644 --- a/vlib/vweb/csrf/create_cookie.v +++ b/vlib/vweb/csrf/create_cookie.v @@ -49,8 +49,8 @@ pub fn (mut app App) get_csrf_token() ?string { if app.csrf_cookie_value != '' { return app.csrf_cookie_value } else { - return IError(CsrfError{ + return CsrfError{ m: 'The CSRF-Token-Value is empty. Please check if you have setted a cookie!' - }) + } } } diff --git a/vlib/vweb/parse.v b/vlib/vweb/parse.v index aa9662ab17..3e6e43ef75 100644 --- a/vlib/vweb/parse.v +++ b/vlib/vweb/parse.v @@ -24,7 +24,7 @@ fn parse_attrs(name string, attrs []string) !([]http.Method, string) { } if attr.starts_with('/') { if path != '' { - return IError(http.MultiplePathAttributesError{}) + return http.MultiplePathAttributesError{} } path = attr x.delete(i) @@ -33,9 +33,9 @@ fn parse_attrs(name string, attrs []string) !([]http.Method, string) { i++ } if x.len > 0 { - return IError(http.UnexpectedExtraAttributeError{ + return http.UnexpectedExtraAttributeError{ attributes: x - }) + } } if methods.len == 0 { methods = [http.Method.get] diff --git a/vlib/x/json2/decoder.v b/vlib/x/json2/decoder.v index 472cdb9175..233d621ba5 100644 --- a/vlib/x/json2/decoder.v +++ b/vlib/x/json2/decoder.v @@ -104,11 +104,11 @@ fn (mut p Parser) next() { fn (mut p Parser) next_with_err() ! { p.next() if p.tok.kind == .error { - return IError(DecodeError{ + return DecodeError{ line: p.tok.line column: p.tok.full_col() message: p.tok.lit.bytestr() - }) + } } } @@ -145,18 +145,18 @@ fn (mut p Parser) decode() !Any { p.next_with_err()! fi := p.decode_value()! if p.tok.kind != .eof { - return IError(InvalidTokenError{ + return InvalidTokenError{ token: p.tok - }) + } } return fi } fn (mut p Parser) decode_value() !Any { if p.n_level + 1 == 500 { - return IError(DecodeError{ + return DecodeError{ message: 'reached maximum nesting level of 500' - }) + } } match p.tok.kind { .lsbr { @@ -200,9 +200,9 @@ fn (mut p Parser) decode_value() !Any { return Any(str) } else { - return IError(InvalidTokenError{ + return InvalidTokenError{ token: p.tok - }) + } } } return Any(null) @@ -219,15 +219,15 @@ fn (mut p Parser) decode_array() !Any { if p.tok.kind == .comma { p.next_with_err()! if p.tok.kind == .rsbr { - return IError(InvalidTokenError{ + return InvalidTokenError{ token: p.tok - }) + } } } else if p.tok.kind != .rsbr { - return IError(UnknownTokenError{ + return UnknownTokenError{ token: p.tok kind: .array - }) + } } } p.next_with_err()! @@ -241,28 +241,28 @@ fn (mut p Parser) decode_object() !Any { p.n_level++ for p.tok.kind != .rcbr { if p.tok.kind != .str_ { - return IError(InvalidTokenError{ + return InvalidTokenError{ token: p.tok expected: .str_ - }) + } } cur_key := p.tok.lit.bytestr() p.next_with_err()! if p.tok.kind != .colon { - return IError(InvalidTokenError{ + return InvalidTokenError{ token: p.tok expected: .colon - }) + } } p.next_with_err()! fields[cur_key] = p.decode_value()! if p.tok.kind != .comma && p.tok.kind != .rcbr { - return IError(UnknownTokenError{ + return UnknownTokenError{ token: p.tok kind: .object - }) + } } else if p.tok.kind == .comma { p.next_with_err()! }