Skip to content

Commit 31e71e9

Browse files
authored
checker: add missing check for duplicated items on in expr (fix #22305) (#22308)
1 parent 1dfe004 commit 31e71e9

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

cmd/tools/vdoc/highlight.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn color_highlight(code string, tb &ast.Table) string {
8383
&& (next_tok.kind != .lpar || prev.kind !in [.key_fn, .rpar]) {
8484
tok_typ = .builtin
8585
} else if
86-
(next_tok.kind in [.lcbr, .rpar, .eof, .comma, .pipe, .name, .rcbr, .assign, .key_pub, .key_mut, .pipe, .comma, .comment, .lt, .lsbr]
86+
(next_tok.kind in [.lcbr, .rpar, .eof, .name, .rcbr, .assign, .key_pub, .key_mut, .pipe, .comma, .comment, .lt, .lsbr]
8787
&& next_tok.lit !in highlight_builtin_types)
8888
&& (prev.kind in [.name, .amp, .lcbr, .rsbr, .key_type, .assign, .dot, .not, .question, .rpar, .key_struct, .key_enum, .pipe, .key_interface, .comment, .ellipsis, .comma]
8989
&& prev.lit !in highlight_builtin_types)

vlib/v/checker/infix.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
241241
c.error('left operand to `${node.op}` does not match the array element type: ${err.msg()}',
242242
left_right_pos)
243243
}
244+
if mut node.right is ast.ArrayInit {
245+
c.check_duplicated_items(node.right)
246+
}
244247
} else {
245248
if mut node.right is ast.ArrayInit {
246249
for i, typ in node.right.expr_types {
@@ -953,6 +956,18 @@ fn (mut c Checker) check_div_mod_by_zero(expr ast.Expr, op_kind token.Kind) {
953956
}
954957
}
955958

959+
fn (mut c Checker) check_duplicated_items(node &ast.ArrayInit) {
960+
mut unique_items := []string{cap: node.exprs.len}
961+
for item in node.exprs {
962+
item_str := item.str()
963+
if item_str in unique_items {
964+
c.note('item `${item_str}` is duplicated in the list', item.pos())
965+
} else {
966+
unique_items << item_str
967+
}
968+
}
969+
}
970+
956971
fn (mut c Checker) check_like_operator(node &ast.InfixExpr) ast.Type {
957972
if node.left !is ast.Ident || !node.left_type.is_string() {
958973
c.error('the left operand of the `like` operator must be an identifier with a string type',

vlib/v/checker/str.v

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type {
9696
}
9797
if ((typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`, `b`])
9898
|| (typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`, `b`])
99-
|| (typ.is_int_literal()
100-
&& fmt !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`, `b`])
99+
|| (typ.is_int_literal() && fmt !in [`d`, `c`, `x`, `X`, `o`, `u`, `b`])
101100
|| (typ.is_float() && fmt !in [`E`, `F`, `G`, `e`, `f`, `g`])
102101
|| (typ.is_pointer() && fmt !in [`p`, `x`, `X`])
103102
|| (typ.is_string() && fmt !in [`s`, `S`, `r`, `R`])
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
vlib/v/checker/tests/infix_dup_in_err.vv:1:16: notice: item `2` is duplicated in the list
2+
1 | if 1 in [1, 2, 2, 2] { println('ok') }
3+
| ^
4+
2 |
5+
3 | a := `a`
6+
vlib/v/checker/tests/infix_dup_in_err.vv:4:20: notice: item `a` is duplicated in the list
7+
2 |
8+
3 | a := `a`
9+
4 | if `a` in [`c`, a, a] { println('ok') }
10+
| ^
11+
5 |
12+
6 | println('done')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if 1 in [1, 2, 2, 2] { println('ok') }
2+
3+
a := `a`
4+
if `a` in [`c`, a, a] { println('ok') }
5+
6+
println('done')

0 commit comments

Comments
 (0)