checker: use ++ / -- instead of += 1 / -= 1

pull/5385/head
Ruofan XU 2020-06-15 01:05:05 +08:00 committed by GitHub
parent a3a91f54a9
commit 90279a7108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 4 deletions

View File

@ -110,8 +110,8 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
out /= ten_pow_table_64[out_len - n_digit ] out /= ten_pow_table_64[out_len - n_digit ]
//println("out1:[$out] ${d.m / ten_pow_table_64[out_len - n_digit ]}") //println("out1:[$out] ${d.m / ten_pow_table_64[out_len - n_digit ]}")
if d.m / ten_pow_table_64[out_len - n_digit ] < out { if d.m / ten_pow_table_64[out_len - n_digit ] < out {
d_exp += 1 d_exp++
n_digit += 1 n_digit++
} }
//println("cmp: ${d.m/ten_pow_table_64[out_len - n_digit ]} ${out/ten_pow_table_64[out_len - n_digit ]}") //println("cmp: ${d.m/ten_pow_table_64[out_len - n_digit ]} ${out/ten_pow_table_64[out_len - n_digit ]}")

View File

@ -709,6 +709,9 @@ fn (mut c Checker) assign_expr(mut assign_expr ast.AssignExpr) {
} else if !right.is_number() && right_type != table.string_type && !right.is_pointer() { } else if !right.is_number() && right_type != table.string_type && !right.is_pointer() {
c.error('operator += not defined on right operand type `$right.name`', assign_expr.val.position()) c.error('operator += not defined on right operand type `$right.name`', assign_expr.val.position())
} }
if assign_expr.val is ast.IntegerLiteral && assign_expr.val.str().int() == 1 {
c.error('use `++` instead of `+= 1`', assign_expr.pos)
}
} }
.minus_assign { .minus_assign {
if !left.is_number() && !left.is_pointer() { if !left.is_number() && !left.is_pointer() {
@ -716,6 +719,9 @@ fn (mut c Checker) assign_expr(mut assign_expr ast.AssignExpr) {
} else if !right.is_number() && !right.is_pointer() { } else if !right.is_number() && !right.is_pointer() {
c.error('operator -= not defined on right operand type `$right.name`', assign_expr.val.position()) c.error('operator -= not defined on right operand type `$right.name`', assign_expr.val.position())
} }
if assign_expr.val is ast.IntegerLiteral && assign_expr.val.str().int() == 1 {
c.error('use `--` instead of `-= 1`', assign_expr.pos)
}
} }
.mult_assign, .div_assign { .mult_assign, .div_assign {
if !left.is_number() { if !left.is_number() {

View File

@ -55,7 +55,7 @@ fn check_path(vexe, dir, voptions, result_extension string, tests []string) int
println('found:') println('found:')
println(found) println(found)
println('============\n') println('============\n')
nb_fail += 1 nb_fail++
} else { } else {
println(term.green('OK')) println(term.green('OK'))
os.rm(program) os.rm(program)

View File

@ -0,0 +1,13 @@
vlib/v/checker/tests/plus_or_minus_assign_one_err.v:3:6: error: use `++` instead of `+= 1`
1 | fn main() {
2 | mut foo := 10
3 | foo += 1
| ~~
4 | foo -= 1
5 | }
vlib/v/checker/tests/plus_or_minus_assign_one_err.v:4:6: error: use `--` instead of `-= 1`
2 | mut foo := 10
3 | foo += 1
4 | foo -= 1
| ~~
5 | }

View File

@ -0,0 +1,5 @@
fn main() {
mut foo := 10
foo += 1
foo -= 1
}

View File

@ -167,7 +167,7 @@ fn test_reassignment() {
assert x2 == 777 assert x2 == 777
x2 = 100 x2 = 100
assert x2 == 100 assert x2 == 100
x2 += 1 x2++
assert x2 == 101 assert x2 == 101
// //
mut x3 := 0 mut x3 := 0