v: vet for empty string conditions (#21529)

This commit is contained in:
Turiiya 2024-05-19 16:57:52 +02:00 committed by GitHub
parent 6752ce8914
commit f7e820cdeb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 45 additions and 48 deletions

View file

@ -19,7 +19,7 @@ pub fn check_v_commit_timestamp_before_self_rebuilding(v_timestamp u64) {
} }
pub fn validate_commit_exists(commit string) { pub fn validate_commit_exists(commit string) {
if commit.len == 0 { if commit != '' {
return return
} }
cmd := 'git cat-file -t "${commit}" ' // windows's cmd.exe does not support ' for quoting cmd := 'git cat-file -t "${commit}" ' // windows's cmd.exe does not support ' for quoting

View file

@ -536,7 +536,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
} else { } else {
dnw.write_string('${tabs(3)}<div class="title"><${head_tag}>${dn.kind} ${sym_name}${hash_link}</${head_tag}>') dnw.write_string('${tabs(3)}<div class="title"><${head_tag}>${dn.kind} ${sym_name}${hash_link}</${head_tag}>')
} }
if link.len != 0 { if link != '' {
dnw.write_string('<a class="link" rel="noreferrer" target="_blank" href="${link}">${link_svg}</a>') dnw.write_string('<a class="link" rel="noreferrer" target="_blank" href="${link}">${link_svg}</a>')
} }
dnw.write_string('</div>\n') dnw.write_string('</div>\n')

View file

@ -59,7 +59,7 @@ pub fn (cf &CFlag) format() string {
} else { } else {
value = cf.eval() value = cf.eval()
} }
if cf.name in ['-l', '-Wa', '-Wl', '-Wp'] && value.len > 0 { if cf.name in ['-l', '-Wa', '-Wl', '-Wp'] && value != '' {
return '${cf.name}${value}'.trim_space() return '${cf.name}${value}'.trim_space()
} }
// convert to absolute path // convert to absolute path

View file

@ -2018,7 +2018,7 @@ fn (mut c Checker) check_enum_field_integer_literal(expr ast.IntegerLiteral, is_
@[inline] @[inline]
fn (mut c Checker) check_loop_label(label string, pos token.Pos) { fn (mut c Checker) check_loop_label(label string, pos token.Pos) {
if label.len == 0 { if label == '' {
// ignore // ignore
return return
} }

View file

@ -132,7 +132,7 @@ pub fn (mut f Fmt) write(s string) {
} }
pub fn (mut f Fmt) writeln(s string) { pub fn (mut f Fmt) writeln(s string) {
if f.indent > 0 && f.empty_line && s.len > 0 { if f.indent > 0 && f.empty_line && s != '' {
f.write_indent() f.write_indent()
} }
f.out.writeln(s) f.out.writeln(s)

View file

@ -94,7 +94,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
ret_typ := g.typ(node.typ) ret_typ := g.typ(node.typ)
elem_typ := g.typ(node.elem_type) elem_typ := g.typ(node.elem_type)
if var_name.len == 0 { if var_name == '' {
g.write('${ret_typ} ${past.tmp_var} =') g.write('${ret_typ} ${past.tmp_var} =')
} }
g.write('{') g.write('{')
@ -300,7 +300,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
ret_typ := g.typ(node.typ) ret_typ := g.typ(node.typ)
elem_typ := g.typ(node.elem_type) elem_typ := g.typ(node.elem_type)
if var_name.len == 0 { if var_name == '' {
g.write('${ret_typ} ${past.tmp_var} =') g.write('${ret_typ} ${past.tmp_var} =')
} }
if is_default_array { if is_default_array {

View file

@ -1658,7 +1658,7 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) {
else {} else {}
} }
} }
call_conv_attribute_suffix := if call_conv.len != 0 { call_conv_attribute_suffix := if call_conv != '' {
'__attribute__((${call_conv}))' '__attribute__((${call_conv}))'
} else { } else {
'' ''
@ -5127,24 +5127,24 @@ fn (mut g Gen) hash_stmt(node ast.HashStmt) {
} }
if node.main.contains('.m') { if node.main.contains('.m') {
g.definitions.writeln('') g.definitions.writeln('')
if ct_condition.len > 0 { if ct_condition != '' {
g.definitions.writeln('#if ${ct_condition}') g.definitions.writeln('#if ${ct_condition}')
} }
// Objective C code import, include it after V types, so that e.g. `string` is // Objective C code import, include it after V types, so that e.g. `string` is
// available there // available there
g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:') g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
g.definitions.writeln(guarded_include) g.definitions.writeln(guarded_include)
if ct_condition.len > 0 { if ct_condition != '' {
g.definitions.writeln('#endif // \$if ${ct_condition}') g.definitions.writeln('#endif // \$if ${ct_condition}')
} }
} else { } else {
g.includes.writeln('') g.includes.writeln('')
if ct_condition.len > 0 { if ct_condition != '' {
g.includes.writeln('#if ${ct_condition}') g.includes.writeln('#if ${ct_condition}')
} }
g.includes.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:') g.includes.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
g.includes.writeln(guarded_include) g.includes.writeln(guarded_include)
if ct_condition.len > 0 { if ct_condition != '' {
g.includes.writeln('#endif // \$if ${ct_condition}') g.includes.writeln('#endif // \$if ${ct_condition}')
} }
} }
@ -5164,43 +5164,43 @@ fn (mut g Gen) hash_stmt(node ast.HashStmt) {
// Might need to support '#preinclude' for .m files as well but for the moment // Might need to support '#preinclude' for .m files as well but for the moment
// this does the same as '#include' for them // this does the same as '#include' for them
g.definitions.writeln('') g.definitions.writeln('')
if ct_condition.len > 0 { if ct_condition != '' {
g.definitions.writeln('#if ${ct_condition}') g.definitions.writeln('#if ${ct_condition}')
} }
// Objective C code import, include it after V types, so that e.g. `string` is // Objective C code import, include it after V types, so that e.g. `string` is
// available there // available there
g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:') g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
g.definitions.writeln(guarded_include) g.definitions.writeln(guarded_include)
if ct_condition.len > 0 { if ct_condition != '' {
g.definitions.writeln('#endif // \$if ${ct_condition}') g.definitions.writeln('#endif // \$if ${ct_condition}')
} }
} else { } else {
g.preincludes.writeln('') g.preincludes.writeln('')
if ct_condition.len > 0 { if ct_condition != '' {
g.preincludes.writeln('#if ${ct_condition}') g.preincludes.writeln('#if ${ct_condition}')
} }
g.preincludes.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:') g.preincludes.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
g.preincludes.writeln(guarded_include) g.preincludes.writeln(guarded_include)
if ct_condition.len > 0 { if ct_condition != '' {
g.preincludes.writeln('#endif // \$if ${ct_condition}') g.preincludes.writeln('#endif // \$if ${ct_condition}')
} }
} }
} else if node.kind == 'insert' { } else if node.kind == 'insert' {
if ct_condition.len > 0 { if ct_condition != '' {
g.includes.writeln('#if ${ct_condition}') g.includes.writeln('#if ${ct_condition}')
} }
g.includes.writeln('// inserted by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:') g.includes.writeln('// inserted by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
g.includes.writeln(node.val) g.includes.writeln(node.val)
if ct_condition.len > 0 { if ct_condition != '' {
g.includes.writeln('#endif // \$if ${ct_condition}') g.includes.writeln('#endif // \$if ${ct_condition}')
} }
} else if node.kind == 'define' { } else if node.kind == 'define' {
if ct_condition.len > 0 { if ct_condition != '' {
g.includes.writeln('#if ${ct_condition}') g.includes.writeln('#if ${ct_condition}')
} }
g.includes.writeln('// defined by module `${node.mod}`') g.includes.writeln('// defined by module `${node.mod}`')
g.includes.writeln('#define ${node.main}') g.includes.writeln('#define ${node.main}')
if ct_condition.len > 0 { if ct_condition != '' {
g.includes.writeln('#endif // \$if ${ct_condition}') g.includes.writeln('#endif // \$if ${ct_condition}')
} }
} }
@ -5504,7 +5504,7 @@ fn (mut g Gen) return_stmt(node ast.Return) {
g.write('return ${tmpvar}') g.write('return ${tmpvar}')
} }
// Make sure to add our unpacks // Make sure to add our unpacks
if multi_unpack.len > 0 { if multi_unpack != '' {
g.insert_before_stmt(multi_unpack) g.insert_before_stmt(multi_unpack)
} }
if use_tmp_var && !fn_return_is_option && !fn_return_is_result { if use_tmp_var && !fn_return_is_option && !fn_return_is_result {
@ -7219,11 +7219,11 @@ fn (g Gen) get_all_test_function_names() []string {
} }
} }
mut all_tfuncs := []string{} mut all_tfuncs := []string{}
if tsuite_begin.len > 0 { if tsuite_begin != '' {
all_tfuncs << tsuite_begin all_tfuncs << tsuite_begin
} }
all_tfuncs << tfuncs all_tfuncs << tfuncs
if tsuite_end.len > 0 { if tsuite_end != '' {
all_tfuncs << tsuite_end all_tfuncs << tsuite_end
} }
return all_tfuncs return all_tfuncs

View file

@ -100,7 +100,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
func_decl = '${def} = &${g.typ(node.return_type)};' func_decl = '${def} = &${g.typ(node.return_type)};'
} }
} }
if func_decl.len > 0 { if func_decl != '' {
g.writeln(func_decl) // func, anon func declaration g.writeln(func_decl) // func, anon func declaration
} else { } else {
g.writeln('${g.typ(node.return_type)} ${tmp_var} = ${g.type_default(node.return_type)};') g.writeln('${g.typ(node.return_type)} ${tmp_var} = ${g.type_default(node.return_type)};')
@ -169,7 +169,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var string, tmp_var string) { fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var string, tmp_var string) {
dot_or_ptr := g.dot_or_ptr(node.cond_type) dot_or_ptr := g.dot_or_ptr(node.cond_type)
use_ternary := is_expr && tmp_var.len == 0 use_ternary := is_expr && tmp_var == ''
cond_sym := g.table.sym(node.cond_type) cond_sym := g.table.sym(node.cond_type)
for j, branch in node.branches { for j, branch in node.branches {
mut sumtype_index := 0 mut sumtype_index := 0
@ -418,7 +418,7 @@ fn (mut g Gen) should_check_low_bound_in_range_expr(expr ast.RangeExpr, node_con
fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var string, tmp_var string) { fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var string, tmp_var string) {
node_cond_type_unsigned := node.cond_type in [ast.u16_type, ast.u32_type, ast.u64_type] node_cond_type_unsigned := node.cond_type in [ast.u16_type, ast.u32_type, ast.u64_type]
type_sym := g.table.final_sym(node.cond_type) type_sym := g.table.final_sym(node.cond_type)
use_ternary := is_expr && tmp_var.len == 0 use_ternary := is_expr && tmp_var == ''
for j, branch in node.branches { for j, branch in node.branches {
is_last := j == node.branches.len - 1 is_last := j == node.branches.len - 1
if branch.is_else || (use_ternary && is_last) { if branch.is_else || (use_ternary && is_last) {

View file

@ -24,7 +24,7 @@ fn (mut g Gen) past_tmp_var_new() PastTmpVar {
fn (mut g Gen) past_tmp_var_from_var_name(var_name string) PastTmpVar { fn (mut g Gen) past_tmp_var_from_var_name(var_name string) PastTmpVar {
mut tmp_var := g.new_tmp_var() mut tmp_var := g.new_tmp_var()
mut s := '' mut s := ''
if var_name.len != 0 { if var_name != '' {
tmp_var = var_name tmp_var = var_name
} else { } else {
s = g.go_before_last_stmt() s = g.go_before_last_stmt()

View file

@ -627,7 +627,7 @@ fn (mut g Gen) struct_decl(s ast.Struct, name string, is_anon bool) {
g.type_definitions.write_string(';') g.type_definitions.write_string(';')
} }
g.type_definitions.writeln('') g.type_definitions.writeln('')
if post_pragma.len > 0 { if post_pragma != '' {
g.type_definitions.writeln(post_pragma) g.type_definitions.writeln(post_pragma)
} }
} }

View file

@ -92,7 +92,7 @@ pub fn (mut f Gen) write(s string) {
} }
pub fn (mut f Gen) writeln(s string) { pub fn (mut f Gen) writeln(s string) {
if f.indent > 0 && f.empty_line && s.len > 0 { if f.indent > 0 && f.empty_line && s != '' {
f.write_indent() f.write_indent()
} }
f.out.writeln(s) f.out.writeln(s)

View file

@ -393,11 +393,11 @@ fn (g &JsGen) get_all_test_function_names() []string {
} }
} }
mut all_tfuncs := []string{} mut all_tfuncs := []string{}
if tsuite_begin.len > 0 { if tsuite_begin != '' {
all_tfuncs << tsuite_begin all_tfuncs << tsuite_begin
} }
all_tfuncs << tfuncs all_tfuncs << tfuncs
if tsuite_end.len > 0 { if tsuite_end != '' {
all_tfuncs << tsuite_end all_tfuncs << tsuite_end
} }
return all_tfuncs return all_tfuncs

View file

@ -37,13 +37,13 @@ pub fn new_sourcemap(file string, source_root string, sources_content_inline boo
// Add a single mapping from original source line and column to the generated source's line and column for this source map being created. // Add a single mapping from original source line and column to the generated source's line and column for this source map being created.
pub fn (mut sm SourceMap) add_mapping(source_name string, source_position SourcePositionType, gen_line u32, gen_column u32, name string) { pub fn (mut sm SourceMap) add_mapping(source_name string, source_position SourcePositionType, gen_line u32, gen_column u32, name string) {
if source_name.len == 0 { if source_name == '' {
panic('add_mapping, source_name should not be ""') panic('add_mapping, source_name should not be ""')
} }
sources_ind := sm.sources.add(source_name) sources_ind := sm.sources.add(source_name)
names_ind := if name.len != 0 { names_ind := if name != '' {
NameIndexType(IndexNumber(sm.names.add(name))) NameIndexType(IndexNumber(sm.names.add(name)))
} else { } else {
NameIndexType(Empty{}) NameIndexType(Empty{})
@ -53,14 +53,14 @@ pub fn (mut sm SourceMap) add_mapping(source_name string, source_position Source
// Add multiple mappings from the same source // Add multiple mappings from the same source
pub fn (mut sm SourceMap) add_mapping_list(source_name string, mapping_list []MappingInput) ! { pub fn (mut sm SourceMap) add_mapping_list(source_name string, mapping_list []MappingInput) ! {
if source_name.len == 0 { if source_name == '' {
panic('add_mapping_list, source_name should not be ""') panic('add_mapping_list, source_name should not be ""')
} }
sources_ind := sm.sources.add(source_name) sources_ind := sm.sources.add(source_name)
for mapping in mapping_list { for mapping in mapping_list {
names_ind := if mapping.name.len != 0 { names_ind := if mapping.name != '' {
NameIndexType(IndexNumber(sm.names.add(mapping.name))) NameIndexType(IndexNumber(sm.names.add(mapping.name)))
} else { } else {
NameIndexType(Empty{}) NameIndexType(Empty{})

View file

@ -173,7 +173,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
is_tmp: true is_tmp: true
is_stack_obj: true is_stack_obj: true
}) })
if key_var_name.len > 0 { if key_var_name != '' {
return p.error_with_pos('cannot declare index variable with range `for`', return p.error_with_pos('cannot declare index variable with range `for`',
key_var_pos) key_var_pos)
} }

View file

@ -2712,10 +2712,7 @@ fn (mut p Parser) name_expr() ast.Expr {
mod = original_name mod = original_name
original_name = p.peek_token(4).lit original_name = p.peek_token(4).lit
} }
mut name := original_name name := if mod != '' { '${mod}.${original_name}' } else { original_name }
if mod.len > 0 {
name = '${mod}.${name}'
}
name_w_mod := p.prepend_mod(name) name_w_mod := p.prepend_mod(name)
is_c_pointer_cast := language == .c && prev_tok_kind == .amp // `&C.abc(x)` is *always* a cast is_c_pointer_cast := language == .c && prev_tok_kind == .amp // `&C.abc(x)` is *always* a cast
is_c_type_cast := language == .c && (original_name in ['intptr_t', 'uintptr_t'] is_c_type_cast := language == .c && (original_name in ['intptr_t', 'uintptr_t']

View file

@ -286,7 +286,7 @@ fn run_code_in_tmp_vfile_and_exit(args []string, mut res Preferences, option_nam
tmp_result := os.system(tmp_cmd) tmp_result := os.system(tmp_cmd)
res.vrun_elog('exit code: ${tmp_result}') res.vrun_elog('exit code: ${tmp_result}')
// //
if output_option.len != 0 { if output_option != '' {
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 {}
} }

View file

@ -11,10 +11,10 @@ fn imin(x u16, y u16) u16 {
// From https://gist.github.com/zeozeozeo/f785910173f3115163bffd0c5240de07 // From https://gist.github.com/zeozeozeo/f785910173f3115163bffd0c5240de07
@[direct_array_access] @[direct_array_access]
pub fn zeozeozeo_levenshtein_distance(a string, b string) int { pub fn zeozeozeo_levenshtein_distance(a string, b string) int {
if a.len == 0 { if a == '' {
return b.len return b.len
} }
if b.len == 0 { if b == '' {
return a.len return a.len
} }
if a == b { if a == b {

View file

@ -22,7 +22,7 @@ fn fn_mr_get_user() (string, int, []string, UserData) {
fn split_to_two(s string) !(string, string) { fn split_to_two(s string) !(string, string) {
mut tokens := s.split_nth(' ', 2) mut tokens := s.split_nth(' ', 2)
if s.len == 0 { if s == '' {
return error('error') return error('error')
} }
if tokens.len != 2 { if tokens.len != 2 {

View file

@ -1,5 +1,5 @@
fn issue(data string) ?(int, string) { fn issue(data string) ?(int, string) {
if data.len == 0 { if data == '' {
return none return none
} }
return data.len, data return data.len, data

View file

@ -108,7 +108,7 @@ pub fn path_styled_for_error_messages(path string) string {
pub fn formatted_error(kind string, omsg string, filepath string, pos token.Pos) string { pub fn formatted_error(kind string, omsg string, filepath string, pos token.Pos) string {
emsg := omsg.replace('main.', '') emsg := omsg.replace('main.', '')
path := path_styled_for_error_messages(filepath) path := path_styled_for_error_messages(filepath)
position := if filepath.len > 0 { position := if filepath != '' {
'${path}:${pos.line_nr + 1}:${mu.max(1, pos.col + 1)}:' '${path}:${pos.line_nr + 1}:${mu.max(1, pos.col + 1)}:'
} else { } else {
'' ''

View file

@ -25,7 +25,7 @@ pub enum ValueKind {
// check_json // check_json
fn check_json(val string) ! { fn check_json(val string) ! {
if val.len == 0 { if val == '' {
return error('empty string') return error('empty string')
} }
} }

View file

@ -448,7 +448,7 @@ pub fn (f Any) prettify_json_str() string {
// encode_string returns the JSON spec-compliant version of the string. // encode_string returns the JSON spec-compliant version of the string.
@[direct_array_access] @[direct_array_access]
fn (e &Encoder) encode_string(s string, mut buf []u8) ! { fn (e &Encoder) encode_string(s string, mut buf []u8) ! {
if s.len == 0 { if s == '' {
empty := [u8(json2.quote_rune), json2.quote_rune]! empty := [u8(json2.quote_rune), json2.quote_rune]!
unsafe { buf.push_many(&empty[0], 2) } unsafe { buf.push_many(&empty[0], 2) }
return return