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

checker: do not allow extra () in if conditions

This commit is contained in:
Alexander Medvednikov 2020-03-27 14:57:19 +01:00
parent 1a751208ca
commit 473d9fef55
11 changed files with 33 additions and 23 deletions

View File

@ -1306,7 +1306,7 @@ pub fn (s string) strip_margin(del ...byte) string {
mut ret := malloc(s.len + 1)
mut count := 0
for i := 0; i < s.len; i++ {
if (s[i] in [`\n`, `\r`]) {
if s[i] in [`\n`, `\r`] {
ret[count] = s[i]
count++
// CRLF

View File

@ -16,14 +16,14 @@ pub fn utf32_to_str(code u32) string {
buffer[0] = icode
return tos(buffer, 1)
}
if (icode <= 2047/* 0x7FF */) {
if icode <= 2047/* 0x7FF */ {
buffer[0] = 192/*0xC0*/ | (icode>>6)/* 110xxxxx */
buffer[1] = 128/*0x80*/ | (icode & 63/*0x3F*/)/* 10xxxxxx */
return tos(buffer, 2)
}
if (icode <= 65535/* 0xFFFF */) {
if icode <= 65535/* 0xFFFF */ {
buffer[0] = 224/*0xE0*/ | (icode>>12)/* 1110xxxx */
buffer[1] = 128/*0x80*/ | ((icode>>6) & 63/*0x3F*/)/* 10xxxxxx */
@ -32,7 +32,7 @@ pub fn utf32_to_str(code u32) string {
return tos(buffer, 3)
}
if (icode <= 1114111/* 0x10FFFF */) {
if icode <= 1114111/* 0x10FFFF */ {
buffer[0] = 240/*0xF0*/ | (icode>>18)/* 11110xxx */
buffer[1] = 128/*0x80*/ | ((icode>>12) & 63/*0x3F*/)/* 10xxxxxx */
@ -54,14 +54,14 @@ pub fn utf32_to_str_no_malloc(code u32, buf voidptr) string {
buffer[0] = icode
return tos(buffer, 1)
}
if (icode <= 2047/* 0x7FF */) {
if icode <= 2047/* 0x7FF */ {
buffer[0] = 192/*0xC0*/ | (icode>>6)/* 110xxxxx */
buffer[1] = 128/*0x80*/ | (icode & 63/*0x3F*/)/* 10xxxxxx */
return tos(buffer, 2)
}
if (icode <= 65535/* 0xFFFF */) {
if icode <= 65535/* 0xFFFF */ {
buffer[0] = 224/*0xE0*/ | (icode>>12)/* 1110xxxx */
buffer[1] = 128/*0x80*/ | ((icode>>6) & 63/*0x3F*/)/* 10xxxxxx */
@ -70,7 +70,7 @@ pub fn utf32_to_str_no_malloc(code u32, buf voidptr) string {
return tos(buffer, 3)
}
if (icode <= 1114111/* 0x10FFFF */) {
if icode <= 1114111/* 0x10FFFF */ {
buffer[0] = 240/*0xF0*/ | (icode>>18)/* 11110xxx */
buffer[1] = 128/*0x80*/ | ((icode>>12) & 63/*0x3F*/)/* 10xxxxxx */
@ -153,21 +153,21 @@ pub fn string_from_wide2(_wstr &u16, len int) string {
fn utf8_len(c byte) int {
mut b := 0
mut x := c
if ((x & 240) != 0) {
if (x & 240) != 0 {
// 0xF0
x >>= 4
}
else {
b += 4
}
if ((x & 12) != 0) {
if (x & 12) != 0 {
// 0x0C
x >>= 2
}
else {
b += 2
}
if ((x & 2) == 0) {
if (x & 2) == 0 {
// 0x02
b++
}

View File

@ -64,10 +64,10 @@ fn (p mut Parser) gen_fn_decl(f Fn, typ, str_args string) {
// blank identifer assignment `_ = 111`
fn (p mut Parser) gen_blank_identifier_assign() {
assign_error_tok_idx := p.token_idx
//assign_error_tok_idx := p.token_idx
p.check_name()
p.check_space(.assign)
is_indexer := p.peek() == .lsbr
//is_indexer := p.peek() == .lsbr
is_fn_call,next_expr := p.is_expr_fn_call(p.token_idx)
pos := p.cgen.add_placeholder()
expr_tok := p.cur_tok_index()
@ -77,9 +77,9 @@ fn (p mut Parser) gen_blank_identifier_assign() {
p.error_with_token_index('${next_expr}() $err_used_as_value', expr_tok)
}
p.is_var_decl = false
if !is_indexer && !is_fn_call {
p.error_with_token_index('assigning `$next_expr` to `_` is redundant', assign_error_tok_idx)
}
//if !is_indexer && !is_fn_call {
//p.error_with_token_index('assigning `$next_expr` to `_` is redundant', assign_error_tok_idx)
//}
// handle or
if p.tok == .key_orelse {
p.gen_handle_option_or_else(typ, '', pos)

View File

@ -9,10 +9,10 @@ fn (v &V) generate_hotcode_reloading_compiler_flags() []string {
mut a := []string
if v.pref.is_live || v.pref.is_so {
// See 'man dlopen', and test running a GUI program compiled with -live
if (v.pref.os == .linux || os.user_os() == 'linux') {
if v.pref.os == .linux || os.user_os() == 'linux' {
a << '-rdynamic'
}
if (v.pref.os == .mac || os.user_os() == 'mac') {
if v.pref.os == .mac || os.user_os() == 'mac' {
a << '-flat_namespace'
}
}

View File

@ -388,7 +388,7 @@ fn (p &Parser) known_fn_in_mod(name string) bool {
existing_fn := p.table.find_fn(name) or {
return false
}
if (existing_fn.mod == p.mod || existing_fn.mod == 'builtin') {
if existing_fn.mod == p.mod || existing_fn.mod == 'builtin' {
return true
}
return false

View File

@ -699,7 +699,7 @@ pub fn get_lines() []string {
mut inputstr := []string
for {
line = get_line()
if (line.len <= 0) {
if line.len <= 0 {
break
}
line = line.trim_space()

View File

@ -334,7 +334,7 @@ fn parser(s string) (int,PrepNumber) {
result = parser_pzero
}
}
else if (pn.exponent > 309) {
else if pn.exponent > 309 {
if pn.negative {
result = parser_minf
}

View File

@ -172,7 +172,7 @@ pub fn day_of_week(y, m, d int) int {
// https://stackoverflow.com/a/6385934
t := [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
mut sy := y
if (m < 3) {
if m < 3 {
sy = sy - 1
}
return (sy + sy / 4 - sy / 100 + sy / 400 + t[m - 1] + d - 1) % 7 + 1

View File

@ -961,6 +961,12 @@ pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
}
node.typ = table.void_type
for i, branch in node.branches {
match branch.cond {
ast.ParExpr{
c.error('unnecessary `()` in an if condition. use `if expr {` instead of `if (expr) {`.', node.pos)
}
else {}
}
typ := c.expr(branch.cond)
if i < node.branches.len - 1 || !node.has_else {
typ_sym := c.table.get_type_symbol(typ)

View File

@ -785,7 +785,7 @@ fn (g mut Gen) fn_args(args []table.Arg, is_variadic bool) {
is_varg := i == args.len - 1 && is_variadic
if is_varg {
varg_type_str := int(arg.typ).str()
if (!varg_type_str in g.variadic_args) {
if !(varg_type_str in g.variadic_args) {
g.variadic_args[varg_type_str] = 0
}
arg_type_name = 'varg_' + g.typ(arg.typ).replace('*', '_ptr')

View File

@ -29,7 +29,7 @@ fn (p mut Parser) comp_if() ast.CompIf {
if ((!is_not && os != p.pref.os) || (is_not && os == p.pref.os)) && !p.pref.output_cross_c {
skip_os = true
p.check(.lcbr)
p.warn('SKIPPING $val os=$os p.pref.os=$p.pref.os')
//p.warn('skipping $if $val os=$os p.pref.os=$p.pref.os')
mut stack := 1
for {
if p.tok.kind == .key_return {
@ -74,6 +74,10 @@ fn (p mut Parser) comp_if() ast.CompIf {
return node
}
const (
todo_delete_me = pref.OS.linux // TODO import warning bug
)
fn os_from_string(os string) pref.OS {
match os {
'linux' {