Skip to content

Commit 473cd1d

Browse files
author
Lukas Neubert
authored
fmt: single line ternary return (#8605)
1 parent 118ca12 commit 473cd1d

File tree

13 files changed

+49
-79
lines changed

13 files changed

+49
-79
lines changed

cmd/tools/vdoc/html.v

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,7 @@ fn html_highlight(code string, tb &table.Table) string {
353353
} else {
354354
tok.lit
355355
}
356-
return if typ in [.unone, .name] {
357-
lit
358-
} else {
359-
'<span class="token $typ">$lit</span>'
360-
}
356+
return if typ in [.unone, .name] { lit } else { '<span class="token $typ">$lit</span>' }
361357
}
362358
mut s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{})
363359
mut tok := s.scan()

vlib/builtin/string.v

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,11 +1053,7 @@ pub fn (s string) trim_right(cutset string) string {
10531053
for pos >= 0 && s[pos] in cs_arr {
10541054
pos--
10551055
}
1056-
return if pos < 0 {
1057-
''
1058-
} else {
1059-
s[..pos + 1]
1060-
}
1056+
return if pos < 0 { '' } else { s[..pos + 1] }
10611057
}
10621058

10631059
// trim_prefix strips `str` from the start of the string.

vlib/term/term.v

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,19 @@ pub fn can_show_color_on_stderr() bool {
2929
// ok_message returns a colored string with green color.
3030
// If colors are not allowed, returns a given string.
3131
pub fn ok_message(s string) string {
32-
return if can_show_color_on_stdout() {
33-
green(' $s ')
34-
} else {
35-
s
36-
}
32+
return if can_show_color_on_stdout() { green(' $s ') } else { s }
3733
}
3834

3935
// fail_message returns a colored string with red color.
4036
// If colors are not allowed, returns a given string.
4137
pub fn fail_message(s string) string {
42-
return if can_show_color_on_stdout() {
43-
inverse(bg_white(bold(red(' $s '))))
44-
} else {
45-
s
46-
}
38+
return if can_show_color_on_stdout() { inverse(bg_white(bold(red(' $s ')))) } else { s }
4739
}
4840

4941
// warn_message returns a colored string with yellow color.
5042
// If colors are not allowed, returns a given string.
5143
pub fn warn_message(s string) string {
52-
return if can_show_color_on_stdout() {
53-
bright_yellow(' $s ')
54-
} else {
55-
s
56-
}
44+
return if can_show_color_on_stdout() { bright_yellow(' $s ') } else { s }
5745
}
5846

5947
// colorize returns a colored string by running the specified `cfn` over
@@ -107,11 +95,7 @@ pub fn header(text string, divider string) string {
10795
}
10896

10997
fn imax(x int, y int) int {
110-
return if x > y {
111-
x
112-
} else {
113-
y
114-
}
98+
return if x > y { x } else { y }
11599
}
116100

117101
fn supports_escape_sequences(fd int) bool {

vlib/v/checker/check_types.v

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,7 @@ fn (c &Checker) promote_num(left_type table.Type, right_type table.Type) table.T
183183
return type_hi
184184
} else if idx_lo >= table.i8_type_idx
185185
&& (idx_hi <= table.i64_type_idx || idx_hi == table.rune_type_idx) { // both signed
186-
return if idx_lo == table.i64_type_idx {
187-
type_lo
188-
} else {
189-
type_hi
190-
}
186+
return if idx_lo == table.i64_type_idx { type_lo } else { type_hi }
191187
} else if idx_hi - idx_lo < (table.byte_type_idx - table.i8_type_idx) {
192188
return type_lo // conversion unsigned -> signed if signed type is larger
193189
} else {

vlib/v/checker/checker.v

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,11 +1049,7 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
10491049
c.warn('`++` and `--` are statements, not expressions', infix_expr.pos)
10501050
}
10511051
*/
1052-
return if infix_expr.op.is_relational() {
1053-
table.bool_type
1054-
} else {
1055-
return_type
1056-
}
1052+
return if infix_expr.op.is_relational() { table.bool_type } else { return_type }
10571053
}
10581054

10591055
// returns name and position of variable that needs write lock
@@ -4964,11 +4960,7 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool {
49644960
// :)
49654961
// until `v.eval` is stable, I can't think of a better way to do this
49664962
different := expr.str() != cond.right.str()
4967-
return if cond.op == .eq {
4968-
different
4969-
} else {
4970-
!different
4971-
}
4963+
return if cond.op == .eq { different } else { !different }
49724964
} else {
49734965
c.error('invalid `\$if` condition: ${cond.left.type_name()}1',
49744966
cond.pos)

vlib/v/fmt/fmt.v

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
537537

538538
[inline]
539539
fn abs(v int) int {
540-
return if v >= 0 {
541-
v
542-
} else {
543-
-v
544-
}
540+
return if v >= 0 { v } else { -v }
545541
}
546542

547543
const (
@@ -1636,15 +1632,15 @@ pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int, ignore_paren bool) {
16361632
}
16371633
}
16381634

1639-
pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
1640-
dollar := if it.is_comptime { '$' } else { '' }
1641-
mut single_line := it.branches.len == 2 && it.has_else && branch_is_single_line(it.branches[0])
1642-
&& branch_is_single_line(it.branches[1])
1643-
&& (it.is_expr || f.is_assign || f.single_line_fields)
1635+
pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
1636+
dollar := if node.is_comptime { '$' } else { '' }
1637+
mut single_line := node.branches.len == 2 && node.has_else
1638+
&& branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
1639+
&& (node.is_expr || f.is_assign || f.single_line_fields)
16441640
f.single_line_if = single_line
16451641
if_start := f.line_len
16461642
for {
1647-
for i, branch in it.branches {
1643+
for i, branch in node.branches {
16481644
if i == 0 {
16491645
// first `if`
16501646
f.comments(branch.comments, {})
@@ -1658,7 +1654,7 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
16581654
}
16591655
f.write('${dollar}else ')
16601656
}
1661-
if i < it.branches.len - 1 || !it.has_else {
1657+
if i < node.branches.len - 1 || !node.has_else {
16621658
f.write('${dollar}if ')
16631659
cur_pos := f.out.len
16641660
f.expr(branch.cond)
@@ -1696,9 +1692,9 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
16961692
}
16971693
f.write('}')
16981694
f.single_line_if = false
1699-
if it.post_comments.len > 0 {
1695+
if node.post_comments.len > 0 {
17001696
f.writeln('')
1701-
f.comments(it.post_comments, has_nl: false)
1697+
f.comments(node.post_comments, has_nl: false)
17021698
}
17031699
}
17041700

vlib/v/fmt/tests/if_expected.vv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn non_ternary_return() string {
2+
return if some_cond {
3+
'foo'
4+
} else if false {
5+
'bar'
6+
} else {
7+
'baz'
8+
}
9+
}

vlib/v/fmt/tests/if_input.vv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn non_ternary_return() string {
2+
return if some_cond { 'foo' } else if false { 'bar' } else { 'baz' }
3+
}

vlib/v/fmt/tests/if_ternary_keep.vv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn requires_multiple_lines() {
2020
'other str'
2121
}
2222
}
23+
24+
fn return_ternary(cond bool) int {
25+
return if cond { 5 } else { 12 }
26+
}
27+
28+
fn long_return_ternary() string {
29+
return if false {
30+
'spam and eggs'
31+
} else {
32+
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
33+
}
34+
}

vlib/v/gen/x64/gen.v

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,7 @@ fn (mut g Gen) jmp(addr int) {
249249
}
250250

251251
fn abs(a i64) i64 {
252-
return if a < 0 {
253-
-a
254-
} else {
255-
a
256-
}
252+
return if a < 0 { -a } else { a }
257253
}
258254

259255
fn (mut g Gen) jle(addr i64) {

0 commit comments

Comments
 (0)