From d7ccbba2c90aa0c82e75c5c20ad997bafacf3990 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 7 Dec 2019 15:31:56 +0300 Subject: [PATCH] do not allow casting a type to itself --- .github/workflows/ci.yml | 2 +- vlib/builtin/builtin_nix.v | 5 +++-- vlib/builtin/utf8.v | 8 ++++---- vlib/compiler/gen_c.v | 4 ++++ vlib/strconv/atoi.v | 10 +++++----- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 61d9a81403..11b65f216e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,7 +41,7 @@ jobs: run: | brew services start postgresql sleep 3 - psql -c 'select rolname from pg_roles' + psql -c -d postgres 'select rolname from pg_roles' psql -U postgres -c 'create database customerdb;' psql -d customerdb -f examples/database/pg/mydb.sql - name: Test v->c diff --git a/vlib/builtin/builtin_nix.v b/vlib/builtin/builtin_nix.v index 0555f7cbe4..9bbd164748 100644 --- a/vlib/builtin/builtin_nix.v +++ b/vlib/builtin/builtin_nix.v @@ -53,7 +53,8 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool { nr_ptrs := C.backtrace(*voidptr(buffer), 100) nr_actual_frames := nr_ptrs-skipframes mut sframes := []string - csymbols := *byteptr(C.backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)) + csymbols := C.backtrace_symbols(*voidptr(&buffer[skipframes]), + nr_actual_frames) for i in 0..nr_actual_frames { sframes << tos2(csymbols[i]) } for sframe in sframes { executable := sframe.all_before('(') @@ -72,7 +73,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool { output += tos(buf, vstrlen(buf)) } output = output.trim_space()+':' - if 0 != int(C.pclose(f)) { + if 0 != C.pclose(f) { println(sframe) continue } if output in ['??:0:','??:?:'] { output = '' } diff --git a/vlib/builtin/utf8.v b/vlib/builtin/utf8.v index 38c2482f31..e57e9f75e7 100644 --- a/vlib/builtin/utf8.v +++ b/vlib/builtin/utf8.v @@ -155,7 +155,7 @@ fn utf8_len(c byte) int { // Reads an utf8 character from standard input pub fn utf8_getchar() int { - c := int(C.getchar()) + c := C.getchar() len := utf8_len(~c) if c < 0 { return 0 @@ -164,12 +164,12 @@ pub fn utf8_getchar() int { } else if len == 1 { return -1 } else { - mut uc := int(c & ((1 << (7 - len)) - 1)) + mut uc := c & ((1 << (7 - len)) - 1) for i := 0; i + 1 < len; i++ { - c2 := int(C.getchar()) + c2 := C.getchar() if c2 != -1 && (c2 >> 6) == 2 { uc <<= 6 - uc |= int((c2 & 63)) + uc |= (c2 & 63) } else if c2 == -1 { return 0 } else { diff --git a/vlib/compiler/gen_c.v b/vlib/compiler/gen_c.v index 48abfdf004..5baaf52489 100644 --- a/vlib/compiler/gen_c.v +++ b/vlib/compiler/gen_c.v @@ -527,6 +527,10 @@ fn (p mut Parser) cast(typ string) { p.check(.lpar) p.expected_type = typ expr_typ := p.bool_expression() + // Do not allow `int(my_int)` + if expr_typ == typ { + p.warn('casting `$typ` to `$expr_typ` is not needed') + } // `face := FT_Face(cobj)` => `FT_Face face = *((FT_Face*)cobj);` casting_voidptr_to_value := expr_typ == 'void*' && typ != 'int' && typ != 'byteptr' && !typ.ends_with('*') diff --git a/vlib/strconv/atoi.v b/vlib/strconv/atoi.v index 7950a95df4..b1861249de 100644 --- a/vlib/strconv/atoi.v +++ b/vlib/strconv/atoi.v @@ -61,7 +61,7 @@ pub fn common_parse_uint(s string, _base int, _bit_size int, error_on_non_digit } if bit_size == 0 { - bit_size = int(int_size) + bit_size = int_size } else if bit_size < 0 || bit_size > 64 { // return error('parse_uint: bitsize error $s - $bit_size') return u64(0) @@ -69,11 +69,11 @@ pub fn common_parse_uint(s string, _base int, _bit_size int, error_on_non_digit // Cutoff is the smallest number such that cutoff*base > maxUint64. // Use compile-time constants for common cases. - cutoff := u64(max_u64/u64(base)) + u64(1) + cutoff := max_u64/u64(base) + u64(1) max_val := if bit_size == 64 { max_u64 } else { - u64(u64(1)<= cutoff { // return error('parse_int: range error $s0') return i64(cutoff - u64(1))