diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v
index 2bf0b572cd..42e6b23781 100644
--- a/cmd/tools/vdoc/html.v
+++ b/cmd/tools/vdoc/html.v
@@ -353,11 +353,7 @@ fn html_highlight(code string, tb &table.Table) string {
} else {
tok.lit
}
- return if typ in [.unone, .name] {
- lit
- } else {
- '$lit'
- }
+ return if typ in [.unone, .name] { lit } else { '$lit' }
}
mut s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{})
mut tok := s.scan()
diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v
index 4c19b305bd..7ed4542ab0 100644
--- a/vlib/builtin/string.v
+++ b/vlib/builtin/string.v
@@ -1053,11 +1053,7 @@ pub fn (s string) trim_right(cutset string) string {
for pos >= 0 && s[pos] in cs_arr {
pos--
}
- return if pos < 0 {
- ''
- } else {
- s[..pos + 1]
- }
+ return if pos < 0 { '' } else { s[..pos + 1] }
}
// trim_prefix strips `str` from the start of the string.
diff --git a/vlib/term/term.v b/vlib/term/term.v
index ae45794952..a62612f665 100644
--- a/vlib/term/term.v
+++ b/vlib/term/term.v
@@ -29,31 +29,19 @@ pub fn can_show_color_on_stderr() bool {
// ok_message returns a colored string with green color.
// If colors are not allowed, returns a given string.
pub fn ok_message(s string) string {
- return if can_show_color_on_stdout() {
- green(' $s ')
- } else {
- s
- }
+ return if can_show_color_on_stdout() { green(' $s ') } else { s }
}
// fail_message returns a colored string with red color.
// If colors are not allowed, returns a given string.
pub fn fail_message(s string) string {
- return if can_show_color_on_stdout() {
- inverse(bg_white(bold(red(' $s '))))
- } else {
- s
- }
+ return if can_show_color_on_stdout() { inverse(bg_white(bold(red(' $s ')))) } else { s }
}
// warn_message returns a colored string with yellow color.
// If colors are not allowed, returns a given string.
pub fn warn_message(s string) string {
- return if can_show_color_on_stdout() {
- bright_yellow(' $s ')
- } else {
- s
- }
+ return if can_show_color_on_stdout() { bright_yellow(' $s ') } else { s }
}
// colorize returns a colored string by running the specified `cfn` over
@@ -107,11 +95,7 @@ pub fn header(text string, divider string) string {
}
fn imax(x int, y int) int {
- return if x > y {
- x
- } else {
- y
- }
+ return if x > y { x } else { y }
}
fn supports_escape_sequences(fd int) bool {
diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v
index 3d0a018834..5b8551c98e 100644
--- a/vlib/v/checker/check_types.v
+++ b/vlib/v/checker/check_types.v
@@ -183,11 +183,7 @@ fn (c &Checker) promote_num(left_type table.Type, right_type table.Type) table.T
return type_hi
} else if idx_lo >= table.i8_type_idx
&& (idx_hi <= table.i64_type_idx || idx_hi == table.rune_type_idx) { // both signed
- return if idx_lo == table.i64_type_idx {
- type_lo
- } else {
- type_hi
- }
+ return if idx_lo == table.i64_type_idx { type_lo } else { type_hi }
} else if idx_hi - idx_lo < (table.byte_type_idx - table.i8_type_idx) {
return type_lo // conversion unsigned -> signed if signed type is larger
} else {
diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v
index ca5890dfa0..7c08bd13f8 100644
--- a/vlib/v/checker/checker.v
+++ b/vlib/v/checker/checker.v
@@ -1049,11 +1049,7 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
c.warn('`++` and `--` are statements, not expressions', infix_expr.pos)
}
*/
- return if infix_expr.op.is_relational() {
- table.bool_type
- } else {
- return_type
- }
+ return if infix_expr.op.is_relational() { table.bool_type } else { return_type }
}
// 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 {
// :)
// until `v.eval` is stable, I can't think of a better way to do this
different := expr.str() != cond.right.str()
- return if cond.op == .eq {
- different
- } else {
- !different
- }
+ return if cond.op == .eq { different } else { !different }
} else {
c.error('invalid `\$if` condition: ${cond.left.type_name()}1',
cond.pos)
diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v
index 5fe5935c32..e646b56ec2 100644
--- a/vlib/v/fmt/fmt.v
+++ b/vlib/v/fmt/fmt.v
@@ -537,11 +537,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
[inline]
fn abs(v int) int {
- return if v >= 0 {
- v
- } else {
- -v
- }
+ return if v >= 0 { v } else { -v }
}
const (
@@ -1636,15 +1632,15 @@ pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int, ignore_paren bool) {
}
}
-pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
- dollar := if it.is_comptime { '$' } else { '' }
- mut single_line := it.branches.len == 2 && it.has_else && branch_is_single_line(it.branches[0])
- && branch_is_single_line(it.branches[1])
- && (it.is_expr || f.is_assign || f.single_line_fields)
+pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
+ dollar := if node.is_comptime { '$' } else { '' }
+ mut single_line := node.branches.len == 2 && node.has_else
+ && branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
+ && (node.is_expr || f.is_assign || f.single_line_fields)
f.single_line_if = single_line
if_start := f.line_len
for {
- for i, branch in it.branches {
+ for i, branch in node.branches {
if i == 0 {
// first `if`
f.comments(branch.comments, {})
@@ -1658,7 +1654,7 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
}
f.write('${dollar}else ')
}
- if i < it.branches.len - 1 || !it.has_else {
+ if i < node.branches.len - 1 || !node.has_else {
f.write('${dollar}if ')
cur_pos := f.out.len
f.expr(branch.cond)
@@ -1696,9 +1692,9 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
}
f.write('}')
f.single_line_if = false
- if it.post_comments.len > 0 {
+ if node.post_comments.len > 0 {
f.writeln('')
- f.comments(it.post_comments, has_nl: false)
+ f.comments(node.post_comments, has_nl: false)
}
}
diff --git a/vlib/v/fmt/tests/if_expected.vv b/vlib/v/fmt/tests/if_expected.vv
new file mode 100644
index 0000000000..e691055cc4
--- /dev/null
+++ b/vlib/v/fmt/tests/if_expected.vv
@@ -0,0 +1,9 @@
+fn non_ternary_return() string {
+ return if some_cond {
+ 'foo'
+ } else if false {
+ 'bar'
+ } else {
+ 'baz'
+ }
+}
diff --git a/vlib/v/fmt/tests/if_input.vv b/vlib/v/fmt/tests/if_input.vv
new file mode 100644
index 0000000000..5853876df0
--- /dev/null
+++ b/vlib/v/fmt/tests/if_input.vv
@@ -0,0 +1,3 @@
+fn non_ternary_return() string {
+ return if some_cond { 'foo' } else if false { 'bar' } else { 'baz' }
+}
diff --git a/vlib/v/fmt/tests/if_ternary_keep.vv b/vlib/v/fmt/tests/if_ternary_keep.vv
index a460be5dc2..38c8b0cfcf 100644
--- a/vlib/v/fmt/tests/if_ternary_keep.vv
+++ b/vlib/v/fmt/tests/if_ternary_keep.vv
@@ -20,3 +20,15 @@ fn requires_multiple_lines() {
'other str'
}
}
+
+fn return_ternary(cond bool) int {
+ return if cond { 5 } else { 12 }
+}
+
+fn long_return_ternary() string {
+ return if false {
+ 'spam and eggs'
+ } else {
+ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
+ }
+}
diff --git a/vlib/v/gen/x64/gen.v b/vlib/v/gen/x64/gen.v
index 60cc525d95..7c54e750f3 100644
--- a/vlib/v/gen/x64/gen.v
+++ b/vlib/v/gen/x64/gen.v
@@ -249,11 +249,7 @@ fn (mut g Gen) jmp(addr int) {
}
fn abs(a i64) i64 {
- return if a < 0 {
- -a
- } else {
- a
- }
+ return if a < 0 { -a } else { a }
}
fn (mut g Gen) jle(addr i64) {
diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v
index bb7946ce92..f6a2491b94 100644
--- a/vlib/v/parser/if_match.v
+++ b/vlib/v/parser/if_match.v
@@ -15,6 +15,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
p.inside_ct_if_expr = was_inside_ct_if_expr
}
p.inside_if_expr = true
+ is_expr := p.prev_tok.kind == .key_return
mut pos := p.tok.position()
if is_comptime {
p.inside_ct_if_expr = true
@@ -152,6 +153,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
post_comments: comments
pos: pos
has_else: has_else
+ is_expr: is_expr
}
}
diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v
index 92d39df659..cfc5487cbf 100644
--- a/vlib/v/scanner/scanner.v
+++ b/vlib/v/scanner/scanner.v
@@ -1182,11 +1182,7 @@ fn (mut s Scanner) ident_char() string {
}
}
// Escapes a `'` character
- return if c == "'" {
- '\\' + c
- } else {
- c
- }
+ return if c == "'" { '\\' + c } else { c }
}
[inline]
diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v
index 32feb2c5ce..8ebc99cc8b 100644
--- a/vlib/v/util/util.v
+++ b/vlib/v/util/util.v
@@ -349,20 +349,12 @@ pub fn skip_bom(file_content string) string {
[inline]
pub fn imin(a int, b int) int {
- return if a < b {
- a
- } else {
- b
- }
+ return if a < b { a } else { b }
}
[inline]
pub fn imax(a int, b int) int {
- return if a > b {
- a
- } else {
- b
- }
+ return if a > b { a } else { b }
}
pub fn replace_op(s string) string {