Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v: vet for empty string conditions #21529

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/tools/modules/vgit/vgit.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vdoc/html.v
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
} else {
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('</div>\n')
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/cflag/cflags.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -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('{')
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 16 additions & 16 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
''
Expand Down Expand Up @@ -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}')
}
}
Expand All @@ -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}')
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/gen/c/match.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)};')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/past_tmp_var.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/golang/golang.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/js/js.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions vlib/v/gen/js/sourcemap/source_map.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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{})
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/for.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 1 addition & 4 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/tests/bench/bench_strings_similarity.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/fn_multiple_returns_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/return_option_call_test.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn issue(data string) ?(int, string) {
if data.len == 0 {
if data == '' {
return none
}
return data.len, data
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/util/errors.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
''
Expand Down
2 changes: 1 addition & 1 deletion vlib/x/json2/decoder2/decode.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum ValueKind {

// check_json
fn check_json(val string) ! {
if val.len == 0 {
if val == '' {
return error('empty string')
}
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/x/json2/encoder.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down