From f7e820cdebf6349e8f6569328affa6beca0513aa Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sun, 19 May 2024 16:57:52 +0200 Subject: [PATCH] v: vet for empty string conditions (#21529) --- cmd/tools/modules/vgit/vgit.v | 2 +- cmd/tools/vdoc/html.v | 2 +- vlib/v/cflag/cflags.v | 2 +- vlib/v/checker/checker.v | 2 +- vlib/v/fmt/fmt.v | 2 +- vlib/v/gen/c/array.v | 4 +-- vlib/v/gen/c/cgen.v | 32 +++++++++---------- vlib/v/gen/c/match.v | 6 ++-- vlib/v/gen/c/past_tmp_var.v | 2 +- vlib/v/gen/c/struct.v | 2 +- vlib/v/gen/golang/golang.v | 2 +- vlib/v/gen/js/js.v | 4 +-- vlib/v/gen/js/sourcemap/source_map.v | 8 ++--- vlib/v/parser/for.v | 2 +- vlib/v/parser/parser.v | 5 +-- vlib/v/pref/pref.v | 2 +- vlib/v/tests/bench/bench_strings_similarity.v | 4 +-- vlib/v/tests/fn_multiple_returns_test.v | 2 +- vlib/v/tests/return_option_call_test.v | 2 +- vlib/v/util/errors.v | 2 +- vlib/x/json2/decoder2/decode.v | 2 +- vlib/x/json2/encoder.v | 2 +- 22 files changed, 45 insertions(+), 48 deletions(-) diff --git a/cmd/tools/modules/vgit/vgit.v b/cmd/tools/modules/vgit/vgit.v index 325254955e232d..30d114a62b066d 100644 --- a/cmd/tools/modules/vgit/vgit.v +++ b/cmd/tools/modules/vgit/vgit.v @@ -19,7 +19,7 @@ pub fn check_v_commit_timestamp_before_self_rebuilding(v_timestamp u64) { } pub fn validate_commit_exists(commit string) { - if commit.len == 0 { + if commit != '' { return } cmd := 'git cat-file -t "${commit}" ' // windows's cmd.exe does not support ' for quoting diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v index be3584d1e464c1..958948184086e9 100644 --- a/cmd/tools/vdoc/html.v +++ b/cmd/tools/vdoc/html.v @@ -536,7 +536,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, } else { dnw.write_string('${tabs(3)}
<${head_tag}>${dn.kind} ${sym_name}${hash_link}') } - if link.len != 0 { + if link != '' { dnw.write_string('${link_svg}') } dnw.write_string('
\n') diff --git a/vlib/v/cflag/cflags.v b/vlib/v/cflag/cflags.v index 3051e30a3f8195..3dff9e72fefa4f 100644 --- a/vlib/v/cflag/cflags.v +++ b/vlib/v/cflag/cflags.v @@ -59,7 +59,7 @@ pub fn (cf &CFlag) format() string { } else { 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() } // convert to absolute path diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6b9fab0c77597b..1877ab8b6d7547 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2018,7 +2018,7 @@ fn (mut c Checker) check_enum_field_integer_literal(expr ast.IntegerLiteral, is_ @[inline] fn (mut c Checker) check_loop_label(label string, pos token.Pos) { - if label.len == 0 { + if label == '' { // ignore return } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index b40f3cfc1de12e..4a0e8eb098ec6f 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -132,7 +132,7 @@ pub fn (mut f Fmt) write(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.out.writeln(s) diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 857b73ec34c9b7..79a54b7a3b2af4 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -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) elem_typ := g.typ(node.elem_type) - if var_name.len == 0 { + if var_name == '' { g.write('${ret_typ} ${past.tmp_var} =') } 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) elem_typ := g.typ(node.elem_type) - if var_name.len == 0 { + if var_name == '' { g.write('${ret_typ} ${past.tmp_var} =') } if is_default_array { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 41f4bf0abf5ff7..1439ffd0a71a22 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1658,7 +1658,7 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) { else {} } } - call_conv_attribute_suffix := if call_conv.len != 0 { + call_conv_attribute_suffix := if call_conv != '' { '__attribute__((${call_conv}))' } else { '' @@ -5127,24 +5127,24 @@ fn (mut g Gen) hash_stmt(node ast.HashStmt) { } if node.main.contains('.m') { g.definitions.writeln('') - if ct_condition.len > 0 { + if ct_condition != '' { g.definitions.writeln('#if ${ct_condition}') } // Objective C code import, include it after V types, so that e.g. `string` is // available there g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:') g.definitions.writeln(guarded_include) - if ct_condition.len > 0 { + if ct_condition != '' { g.definitions.writeln('#endif // \$if ${ct_condition}') } } else { g.includes.writeln('') - if ct_condition.len > 0 { + 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(guarded_include) - if ct_condition.len > 0 { + 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 // this does the same as '#include' for them g.definitions.writeln('') - if ct_condition.len > 0 { + if ct_condition != '' { g.definitions.writeln('#if ${ct_condition}') } // Objective C code import, include it after V types, so that e.g. `string` is // available there g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:') g.definitions.writeln(guarded_include) - if ct_condition.len > 0 { + if ct_condition != '' { g.definitions.writeln('#endif // \$if ${ct_condition}') } } else { g.preincludes.writeln('') - if ct_condition.len > 0 { + 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(guarded_include) - if ct_condition.len > 0 { + if ct_condition != '' { g.preincludes.writeln('#endif // \$if ${ct_condition}') } } } else if node.kind == 'insert' { - if ct_condition.len > 0 { + 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(node.val) - if ct_condition.len > 0 { + if ct_condition != '' { g.includes.writeln('#endif // \$if ${ct_condition}') } } else if node.kind == 'define' { - if ct_condition.len > 0 { + if ct_condition != '' { g.includes.writeln('#if ${ct_condition}') } g.includes.writeln('// defined by module `${node.mod}`') g.includes.writeln('#define ${node.main}') - if ct_condition.len > 0 { + 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}') } // Make sure to add our unpacks - if multi_unpack.len > 0 { + if multi_unpack != '' { g.insert_before_stmt(multi_unpack) } 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{} - if tsuite_begin.len > 0 { + if tsuite_begin != '' { all_tfuncs << tsuite_begin } all_tfuncs << tfuncs - if tsuite_end.len > 0 { + if tsuite_end != '' { all_tfuncs << tsuite_end } return all_tfuncs diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index 98883b6c666120..cd393e6f548909 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -100,7 +100,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { func_decl = '${def} = &${g.typ(node.return_type)};' } } - if func_decl.len > 0 { + if func_decl != '' { g.writeln(func_decl) // func, anon func declaration } else { 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) { 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) for j, branch in node.branches { 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) { 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) - use_ternary := is_expr && tmp_var.len == 0 + use_ternary := is_expr && tmp_var == '' for j, branch in node.branches { is_last := j == node.branches.len - 1 if branch.is_else || (use_ternary && is_last) { diff --git a/vlib/v/gen/c/past_tmp_var.v b/vlib/v/gen/c/past_tmp_var.v index 3a489d7d9a0037..ead48cff17dca2 100644 --- a/vlib/v/gen/c/past_tmp_var.v +++ b/vlib/v/gen/c/past_tmp_var.v @@ -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 { mut tmp_var := g.new_tmp_var() mut s := '' - if var_name.len != 0 { + if var_name != '' { tmp_var = var_name } else { s = g.go_before_last_stmt() diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 312e9e413ed9ca..4f7093dd90c213 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -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.writeln('') - if post_pragma.len > 0 { + if post_pragma != '' { g.type_definitions.writeln(post_pragma) } } diff --git a/vlib/v/gen/golang/golang.v b/vlib/v/gen/golang/golang.v index 084656e05b0ace..7a5a6160cba80f 100644 --- a/vlib/v/gen/golang/golang.v +++ b/vlib/v/gen/golang/golang.v @@ -92,7 +92,7 @@ pub fn (mut f Gen) write(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.out.writeln(s) diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 00d8e32bf219fd..01da89ef191174 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -393,11 +393,11 @@ fn (g &JsGen) get_all_test_function_names() []string { } } mut all_tfuncs := []string{} - if tsuite_begin.len > 0 { + if tsuite_begin != '' { all_tfuncs << tsuite_begin } all_tfuncs << tfuncs - if tsuite_end.len > 0 { + if tsuite_end != '' { all_tfuncs << tsuite_end } return all_tfuncs diff --git a/vlib/v/gen/js/sourcemap/source_map.v b/vlib/v/gen/js/sourcemap/source_map.v index b5f465f82c96a2..35f312082e36e5 100644 --- a/vlib/v/gen/js/sourcemap/source_map.v +++ b/vlib/v/gen/js/sourcemap/source_map.v @@ -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. 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 ""') } sources_ind := sm.sources.add(source_name) - names_ind := if name.len != 0 { + names_ind := if name != '' { NameIndexType(IndexNumber(sm.names.add(name))) } else { 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 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 ""') } sources_ind := sm.sources.add(source_name) for mapping in mapping_list { - names_ind := if mapping.name.len != 0 { + names_ind := if mapping.name != '' { NameIndexType(IndexNumber(sm.names.add(mapping.name))) } else { NameIndexType(Empty{}) diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index 860b4e8ab5bb1d..daf7d9228e237c 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -173,7 +173,7 @@ fn (mut p Parser) for_stmt() ast.Stmt { is_tmp: 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`', key_var_pos) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 066db7312a6b67..5128cb4577b28b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2712,10 +2712,7 @@ fn (mut p Parser) name_expr() ast.Expr { mod = original_name original_name = p.peek_token(4).lit } - mut name := original_name - if mod.len > 0 { - name = '${mod}.${name}' - } + name := if mod != '' { '${mod}.${original_name}' } else { original_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_type_cast := language == .c && (original_name in ['intptr_t', 'uintptr_t'] diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 4207b5afb9e298..9adacb7f169c18 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -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) 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}') os.rm(tmp_exe_file_path) or {} } diff --git a/vlib/v/tests/bench/bench_strings_similarity.v b/vlib/v/tests/bench/bench_strings_similarity.v index 13af5eb12b5cbb..8ea1a3d835ab8f 100644 --- a/vlib/v/tests/bench/bench_strings_similarity.v +++ b/vlib/v/tests/bench/bench_strings_similarity.v @@ -11,10 +11,10 @@ fn imin(x u16, y u16) u16 { // From https://gist.github.com/zeozeozeo/f785910173f3115163bffd0c5240de07 @[direct_array_access] pub fn zeozeozeo_levenshtein_distance(a string, b string) int { - if a.len == 0 { + if a == '' { return b.len } - if b.len == 0 { + if b == '' { return a.len } if a == b { diff --git a/vlib/v/tests/fn_multiple_returns_test.v b/vlib/v/tests/fn_multiple_returns_test.v index 7f6c4bb3e568bd..224b0b79280881 100644 --- a/vlib/v/tests/fn_multiple_returns_test.v +++ b/vlib/v/tests/fn_multiple_returns_test.v @@ -22,7 +22,7 @@ fn fn_mr_get_user() (string, int, []string, UserData) { fn split_to_two(s string) !(string, string) { mut tokens := s.split_nth(' ', 2) - if s.len == 0 { + if s == '' { return error('error') } if tokens.len != 2 { diff --git a/vlib/v/tests/return_option_call_test.v b/vlib/v/tests/return_option_call_test.v index d4f517cbecb78b..571a41cd672de3 100644 --- a/vlib/v/tests/return_option_call_test.v +++ b/vlib/v/tests/return_option_call_test.v @@ -1,5 +1,5 @@ fn issue(data string) ?(int, string) { - if data.len == 0 { + if data == '' { return none } return data.len, data diff --git a/vlib/v/util/errors.v b/vlib/v/util/errors.v index 3fd3fb1d036f98..54d9d10e747d1e 100644 --- a/vlib/v/util/errors.v +++ b/vlib/v/util/errors.v @@ -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 { emsg := omsg.replace('main.', '') 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)}:' } else { '' diff --git a/vlib/x/json2/decoder2/decode.v b/vlib/x/json2/decoder2/decode.v index 499cfad19d03b1..1797f822a28010 100644 --- a/vlib/x/json2/decoder2/decode.v +++ b/vlib/x/json2/decoder2/decode.v @@ -25,7 +25,7 @@ pub enum ValueKind { // check_json fn check_json(val string) ! { - if val.len == 0 { + if val == '' { return error('empty string') } } diff --git a/vlib/x/json2/encoder.v b/vlib/x/json2/encoder.v index 3e2f85fa0ed171..8c320cb54aaa67 100644 --- a/vlib/x/json2/encoder.v +++ b/vlib/x/json2/encoder.v @@ -448,7 +448,7 @@ pub fn (f Any) prettify_json_str() string { // encode_string returns the JSON spec-compliant version of the string. @[direct_array_access] fn (e &Encoder) encode_string(s string, mut buf []u8) ! { - if s.len == 0 { + if s == '' { empty := [u8(json2.quote_rune), json2.quote_rune]! unsafe { buf.push_many(&empty[0], 2) } return