fmt: write empty or blocks the same as empty fn bodies, empty structs, etc (#9136)

pull/9168/head
Lukas Neubert 2021-03-06 20:04:51 +01:00 committed by GitHub
parent 7a9d9f1e78
commit fdcfe397d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 48 additions and 38 deletions

View File

@ -29,7 +29,7 @@ const another_point = Point{0.25, 2.25, 6.25}
const extended_point = Extended_Point{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0} const extended_point = Extended_Point{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}
fn testsuite_begin() { fn testsuite_begin() {
os.rmdir_all(tfolder) or { } os.rmdir_all(tfolder) or {}
assert !os.is_dir(tfolder) assert !os.is_dir(tfolder)
os.mkdir_all(tfolder) or { panic(err) } os.mkdir_all(tfolder) or { panic(err) }
os.chdir(tfolder) os.chdir(tfolder)

View File

@ -9,7 +9,7 @@ const (
fn testsuite_begin() { fn testsuite_begin() {
eprintln('testsuite_begin, tfolder = $tfolder') eprintln('testsuite_begin, tfolder = $tfolder')
os.rmdir_all(tfolder) or { } os.rmdir_all(tfolder) or {}
assert !os.is_dir(tfolder) assert !os.is_dir(tfolder)
os.mkdir_all(tfolder) or { panic(err) } os.mkdir_all(tfolder) or { panic(err) }
os.chdir(tfolder) os.chdir(tfolder)
@ -36,7 +36,7 @@ fn test_inode_file_owner_permission() {
mut file := os.open_file(filename, 'w', 0o600) or { return } mut file := os.open_file(filename, 'w', 0o600) or { return }
file.close() file.close()
mode := os.inode(filename) mode := os.inode(filename)
os.rm(filename) or { } os.rm(filename) or {}
assert mode.owner.read assert mode.owner.read
assert mode.owner.write assert mode.owner.write
assert !mode.owner.execute assert !mode.owner.execute

View File

@ -13,7 +13,7 @@ const args_at_start = os.args.clone()
fn testsuite_begin() { fn testsuite_begin() {
eprintln('testsuite_begin, tfolder = $tfolder') eprintln('testsuite_begin, tfolder = $tfolder')
os.rmdir_all(tfolder) or { } os.rmdir_all(tfolder) or {}
assert !os.is_dir(tfolder) assert !os.is_dir(tfolder)
os.mkdir_all(tfolder) or { panic(err) } os.mkdir_all(tfolder) or { panic(err) }
os.chdir(tfolder) os.chdir(tfolder)
@ -25,7 +25,7 @@ fn testsuite_begin() {
fn testsuite_end() { fn testsuite_end() {
os.chdir(os.wd_at_startup) os.chdir(os.wd_at_startup)
os.rmdir_all(tfolder) or { } os.rmdir_all(tfolder) or {}
assert !os.is_dir(tfolder) assert !os.is_dir(tfolder)
// eprintln('testsuite_end , tfolder = $tfolder removed.') // eprintln('testsuite_end , tfolder = $tfolder removed.')
} }
@ -280,7 +280,7 @@ fn test_tmpdir() {
assert t.len > 0 assert t.len > 0
assert os.is_dir(t) assert os.is_dir(t)
tfile := t + os.path_separator + 'tmpfile.txt' tfile := t + os.path_separator + 'tmpfile.txt'
os.rm(tfile) or { } // just in case os.rm(tfile) or {} // just in case
tfile_content := 'this is a temporary file' tfile_content := 'this is a temporary file'
os.write_file(tfile, tfile_content) or { panic(err) } os.write_file(tfile, tfile_content) or { panic(err) }
tfile_content_read := os.read_file(tfile) or { panic(err) } tfile_content_read := os.read_file(tfile) or { panic(err) }
@ -305,8 +305,8 @@ fn test_make_symlink_check_is_link_and_remove_symlink() {
} }
folder := 'tfolder' folder := 'tfolder'
symlink := 'tsymlink' symlink := 'tsymlink'
os.rm(symlink) or { } os.rm(symlink) or {}
os.rm(folder) or { } os.rm(folder) or {}
os.mkdir(folder) or { panic(err) } os.mkdir(folder) or { panic(err) }
folder_contents := os.ls(folder) or { panic(err) } folder_contents := os.ls(folder) or { panic(err) }
assert folder_contents.len == 0 assert folder_contents.len == 0
@ -538,6 +538,6 @@ fn test_posix_set_bit() {
} }
mode = u32(s.st_mode) & 0o7777 mode = u32(s.st_mode) & 0o7777
assert mode == 0o0755 assert mode == 0o0755
rm(fpath) or { } rm(fpath) or {}
} }
} }

View File

@ -10,7 +10,7 @@ const test_os_process = os.join_path(os.temp_dir(), 'v', 'test_os_process.exe')
const test_os_process_source = os.join_path(vroot, 'cmd/tools/test_os_process.v') const test_os_process_source = os.join_path(vroot, 'cmd/tools/test_os_process.v')
fn testsuite_begin() { fn testsuite_begin() {
os.rm(test_os_process) or { } os.rm(test_os_process) or {}
assert os.system('$vexe -o $test_os_process $test_os_process_source') == 0 assert os.system('$vexe -o $test_os_process $test_os_process_source') == 0
} }

View File

@ -455,7 +455,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {
} }
} }
utyp := c.unwrap_generic(struct_init.typ) utyp := c.unwrap_generic(struct_init.typ)
c.ensure_type_exists(utyp, struct_init.pos) or { } c.ensure_type_exists(utyp, struct_init.pos) or {}
type_sym := c.table.get_type_symbol(utyp) type_sym := c.table.get_type_symbol(utyp)
if type_sym.kind == .sum_type && struct_init.fields.len == 1 { if type_sym.kind == .sum_type && struct_init.fields.len == 1 {
sexpr := struct_init.fields[0].expr.str() sexpr := struct_init.fields[0].expr.str()
@ -1846,7 +1846,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
c.error('cannot call a function that does not have a body', call_expr.pos) c.error('cannot call a function that does not have a body', call_expr.pos)
} }
for generic_type in call_expr.generic_types { for generic_type in call_expr.generic_types {
c.ensure_type_exists(generic_type, call_expr.generic_list_pos) or { } c.ensure_type_exists(generic_type, call_expr.generic_list_pos) or {}
} }
if f.generic_names.len > 0 && f.return_type.has_flag(.generic) { if f.generic_names.len > 0 && f.return_type.has_flag(.generic) {
rts := c.table.get_type_symbol(f.return_type) rts := c.table.get_type_symbol(f.return_type)
@ -1855,7 +1855,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
gts := c.table.get_type_symbol(call_expr.generic_types[0]) gts := c.table.get_type_symbol(call_expr.generic_types[0])
nrt := '$rts.name<$gts.name>' nrt := '$rts.name<$gts.name>'
idx := c.table.type_idxs[nrt] idx := c.table.type_idxs[nrt]
c.ensure_type_exists(idx, call_expr.pos) or { } c.ensure_type_exists(idx, call_expr.pos) or {}
call_expr.return_type = table.new_type(idx).derive(f.return_type) call_expr.return_type = table.new_type(idx).derive(f.return_type)
} }
} }
@ -2965,7 +2965,7 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type {
c.error('cannot initalize sum type array without default value', array_init.elem_type_pos) c.error('cannot initalize sum type array without default value', array_init.elem_type_pos)
} }
} }
c.ensure_type_exists(array_init.elem_type, array_init.elem_type_pos) or { } c.ensure_type_exists(array_init.elem_type, array_init.elem_type_pos) or {}
return array_init.typ return array_init.typ
} }
// a = [] // a = []
@ -3618,7 +3618,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
expr_type_sym := c.table.get_type_symbol(node.expr_type) expr_type_sym := c.table.get_type_symbol(node.expr_type)
type_sym := c.table.get_type_symbol(node.typ) type_sym := c.table.get_type_symbol(node.typ)
if expr_type_sym.kind == .sum_type { if expr_type_sym.kind == .sum_type {
c.ensure_type_exists(node.typ, node.pos) or { } c.ensure_type_exists(node.typ, node.pos) or {}
if !c.table.sumtype_has_variant(node.expr_type, node.typ) { if !c.table.sumtype_has_variant(node.expr_type, node.typ) {
c.error('cannot cast `$expr_type_sym.name` to `$type_sym.name`', node.pos) c.error('cannot cast `$expr_type_sym.name` to `$type_sym.name`', node.pos)
// c.error('only $info.variants can be casted to `$typ`', node.pos) // c.error('only $info.variants can be casted to `$typ`', node.pos)
@ -3863,7 +3863,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type {
from_type_sym := c.table.get_type_symbol(node.expr_type) from_type_sym := c.table.get_type_symbol(node.expr_type)
to_type_sym := c.table.get_type_symbol(node.typ) // type to be used as cast to_type_sym := c.table.get_type_symbol(node.typ) // type to be used as cast
if to_type_sym.language != .c { if to_type_sym.language != .c {
c.ensure_type_exists(node.typ, node.pos) or { } c.ensure_type_exists(node.typ, node.pos) or {}
} }
expr_is_ptr := node.expr_type.is_ptr() || node.expr_type.idx() in table.pointer_type_idxs expr_is_ptr := node.expr_type.is_ptr() || node.expr_type.idx() in table.pointer_type_idxs
if expr_is_ptr && to_type_sym.kind == .string && !node.in_prexpr { if expr_is_ptr && to_type_sym.kind == .string && !node.in_prexpr {
@ -5503,8 +5503,8 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type {
// `x := map[string]string` - set in parser // `x := map[string]string` - set in parser
if node.typ != 0 { if node.typ != 0 {
info := c.table.get_type_symbol(node.typ).map_info() info := c.table.get_type_symbol(node.typ).map_info()
c.ensure_type_exists(info.key_type, node.pos) or { } c.ensure_type_exists(info.key_type, node.pos) or {}
c.ensure_type_exists(info.value_type, node.pos) or { } c.ensure_type_exists(info.value_type, node.pos) or {}
node.key_type = info.key_type node.key_type = info.key_type
node.value_type = info.value_type node.value_type = info.value_type
return node.typ return node.typ

View File

@ -850,7 +850,11 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
.absent {} .absent {}
.block { .block {
if node.stmts.len == 0 { if node.stmts.len == 0 {
f.write(' or { }') f.write(' or {')
if node.pos.line_nr != node.pos.last_line {
f.writeln('')
}
f.write('}')
return return
} else if node.stmts.len == 1 && stmt_is_single_line(node.stmts[0]) { } else if node.stmts.len == 1 && stmt_is_single_line(node.stmts[0]) {
// the control stmts (return/break/continue...) print a newline inside them, // the control stmts (return/break/continue...) print a newline inside them,

View File

@ -1,3 +1,9 @@
fn main() {
empty_or_block() or {}
empty_or_block() or {
}
}
fn fn_with_or() int { fn fn_with_or() int {
fn_with_optional() or { return 10 } fn_with_optional() or { return 10 }
return 20 return 20

View File

@ -127,7 +127,7 @@ fn protected_load_lib(mut r live.LiveReloadInfo, new_lib_path string) {
elog(r, '> load_lib OK, new live_lib: $r.live_lib') elog(r, '> load_lib OK, new live_lib: $r.live_lib')
// removing the .so file from the filesystem after dlopen-ing // removing the .so file from the filesystem after dlopen-ing
// it is safe, since it will still be mapped in memory // it is safe, since it will still be mapped in memory
os.rm(new_lib_path) or { } os.rm(new_lib_path) or {}
} }
// NB: r.reloader() is executed in a new, independent thread // NB: r.reloader() is executed in a new, independent thread

View File

@ -75,7 +75,7 @@ fn testsuite_begin() {
for f in [tmp_file, source_file, output_file, res_original_file, res_changed_file, res_another_file, for f in [tmp_file, source_file, output_file, res_original_file, res_changed_file, res_another_file,
res_stop_file, res_stop_file,
] { ] {
os.rm(f) or { } os.rm(f) or {}
} }
atomic_write_source(live_program_source) atomic_write_source(live_program_source)
} }
@ -105,7 +105,7 @@ fn testsuite_end() {
assert histogram['CHANGED'] + histogram['ANOTHER'] > 0 assert histogram['CHANGED'] + histogram['ANOTHER'] > 0
// assert histogram['END'] > 0 // assert histogram['END'] > 0
for tfile in cleanup_files { for tfile in cleanup_files {
os.rm(tfile) or { } os.rm(tfile) or {}
} }
} }

View File

@ -490,7 +490,7 @@ pub fn parse_args(known_external_commands []string, args []string) (&Preferences
// //
if output_option.len != 0 { if output_option.len != 0 {
res.vrun_elog('remove tmp exe file: $tmp_exe_file_path') res.vrun_elog('remove tmp exe file: $tmp_exe_file_path')
os.rm(tmp_exe_file_path) or { } os.rm(tmp_exe_file_path) or {}
} }
res.vrun_elog('remove tmp v file: $tmp_v_file_path') res.vrun_elog('remove tmp v file: $tmp_v_file_path')
os.rm(tmp_v_file_path) or { panic(err) } os.rm(tmp_v_file_path) or { panic(err) }

View File

@ -54,7 +54,7 @@ fn test_parse_invalid_cflags() {
} }
fn parse_valid_flag(mut t table.Table, flag string) { fn parse_valid_flag(mut t table.Table, flag string) {
t.parse_cflag(flag, module_name, cdefines) or { } t.parse_cflag(flag, module_name, cdefines) or {}
} }
fn assert_parse_invalid_flag(mut t table.Table, flag string) { fn assert_parse_invalid_flag(mut t table.Table, flag string) {

View File

@ -17,7 +17,7 @@ fn check_cache_entry_fpath_invariants(x string, extension string) {
fn testsuite_begin() { fn testsuite_begin() {
os.setenv('VCACHE', vcache_folder, true) os.setenv('VCACHE', vcache_folder, true)
// eprintln('testsuite_begin, vcache_folder = $vcache_folder') // eprintln('testsuite_begin, vcache_folder = $vcache_folder')
os.rmdir_all(vcache_folder) or { } os.rmdir_all(vcache_folder) or {}
vcache.new_cache_manager([]) vcache.new_cache_manager([])
assert os.is_dir(vcache_folder) assert os.is_dir(vcache_folder)
} }
@ -96,6 +96,6 @@ fn test_readme_exists_and_is_readable() {
fn testsuite_end() { fn testsuite_end() {
os.chdir(os.wd_at_startup) os.chdir(os.wd_at_startup)
os.rmdir_all(vcache_folder) or { } os.rmdir_all(vcache_folder) or {}
assert !os.is_dir(vcache_folder) assert !os.is_dir(vcache_folder)
} }

View File

@ -35,7 +35,7 @@ fn parse_request(mut reader io.BufferedReader) ?http.Request {
n := length.int() n := length.int()
if n > 0 { if n > 0 {
body = []byte{len: n} body = []byte{len: n}
reader.read(mut body) or { } reader.read(mut body) or {}
} }
} }

View File

@ -20,7 +20,7 @@ const (
fn testsuite_begin() { fn testsuite_begin() {
os.chdir(vroot) os.chdir(vroot)
if os.exists(serverexe) { if os.exists(serverexe) {
os.rm(serverexe) or { } os.rm(serverexe) or {}
} }
} }
@ -260,7 +260,7 @@ fn simple_tcp_client(config SimpleTcpClientConfig) ?string {
client.set_read_timeout(tcp_r_timeout) client.set_read_timeout(tcp_r_timeout)
client.set_write_timeout(tcp_w_timeout) client.set_write_timeout(tcp_w_timeout)
defer { defer {
client.close() or { } client.close() or {}
} }
message := 'GET $config.path HTTP/1.1 message := 'GET $config.path HTTP/1.1
Host: $config.host Host: $config.host

View File

@ -169,7 +169,7 @@ pub fn (mut ctx Context) server_error(ecode int) Result {
if ctx.done { if ctx.done {
return Result{} return Result{}
} }
send_string(mut ctx.conn, vweb.http_500) or { } send_string(mut ctx.conn, vweb.http_500) or {}
return Result{} return Result{}
} }
@ -191,7 +191,7 @@ pub fn (mut ctx Context) not_found() Result {
return Result{} return Result{}
} }
ctx.done = true ctx.done = true
send_string(mut ctx.conn, vweb.http_404) or { } send_string(mut ctx.conn, vweb.http_404) or {}
return Result{} return Result{}
} }
@ -301,7 +301,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
conn.set_read_timeout(30 * time.second) conn.set_read_timeout(30 * time.second)
conn.set_write_timeout(30 * time.second) conn.set_write_timeout(30 * time.second)
defer { defer {
conn.close() or { } conn.close() or {}
} }
mut reader := io.new_buffered_reader(reader: io.make_reader(conn)) mut reader := io.new_buffered_reader(reader: io.make_reader(conn))
page_gen_start := time.ticks() page_gen_start := time.ticks()
@ -321,7 +321,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
if 'multipart/form-data' in req.lheaders['content-type'].split('; ') { if 'multipart/form-data' in req.lheaders['content-type'].split('; ') {
boundary := req.lheaders['content-type'].split('; ').filter(it.starts_with('boundary=')) boundary := req.lheaders['content-type'].split('; ').filter(it.starts_with('boundary='))
if boundary.len != 1 { if boundary.len != 1 {
send_string(mut conn, vweb.http_400) or { } send_string(mut conn, vweb.http_400) or {}
return return
} }
form, files := parse_multipart_form(req.data, boundary[0][9..]) form, files := parse_multipart_form(req.data, boundary[0][9..])
@ -400,7 +400,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
} }
} }
// site not found // site not found
send_string(mut conn, vweb.http_404) or { } send_string(mut conn, vweb.http_404) or {}
} }
fn route_matches(url_words []string, route_words []string) ?[]string { fn route_matches(url_words []string, route_words []string) ?[]string {
@ -493,7 +493,7 @@ fn serve_static<T>(mut app T, url urllib.URL) bool {
return false return false
} }
data := os.read_file(static_file) or { data := os.read_file(static_file) or {
send_string(mut app.conn, vweb.http_404) or { } send_string(mut app.conn, vweb.http_404) or {}
return true return true
} }
app.send_response_to_client(mime_type, data) app.send_response_to_client(mime_type, data)

View File

@ -111,7 +111,7 @@ pub fn (mut ws Client) listen() ? {
defer { defer {
ws.logger.info('Quit client listener, server($ws.is_server)...') ws.logger.info('Quit client listener, server($ws.is_server)...')
if ws.state == .open { if ws.state == .open {
ws.close(1000, 'closed by client') or { } ws.close(1000, 'closed by client') or {}
} }
} }
for ws.state == .open { for ws.state == .open {
@ -321,7 +321,7 @@ pub fn (mut ws Client) close(code int, message string) ? {
return error(err_msg) return error(err_msg)
} }
defer { defer {
ws.shutdown_socket() or { } ws.shutdown_socket() or {}
ws.reset_state() ws.reset_state()
} }
ws.set_state(.closing) ws.set_state(.closing)

View File

@ -48,7 +48,7 @@ fn start_server(listen_port int) ? {
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? { s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
// not used // not used
}) })
s.listen() or { } s.listen() or {}
} }
// ws_test tests connect to the websocket server from websocket client // ws_test tests connect to the websocket server from websocket client