From 37311883c1075edfb75a01553fd1a75088aef76a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 12 Sep 2020 13:12:06 +0300 Subject: [PATCH] tests: add optional_method_err.vv --- vlib/v/checker/tests/optional_method_err.out | 7 +++++++ vlib/v/checker/tests/optional_method_err.vv | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 vlib/v/checker/tests/optional_method_err.out create mode 100644 vlib/v/checker/tests/optional_method_err.vv diff --git a/vlib/v/checker/tests/optional_method_err.out b/vlib/v/checker/tests/optional_method_err.out new file mode 100644 index 0000000000..51fdc93ebb --- /dev/null +++ b/vlib/v/checker/tests/optional_method_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/optional_method_err.vv:17:10: error: inc_to_limit() returns an option, but you missed to add an `or {}` block to it + 15 | mut a := Abc{} + 16 | for _ in 0 .. 4 { + 17 | _ := a.inc_to_limit(2) + | ~~~~~~~~~~~~~~~ + 18 | eprintln('a: $a') + 19 | } diff --git a/vlib/v/checker/tests/optional_method_err.vv b/vlib/v/checker/tests/optional_method_err.vv new file mode 100644 index 0000000000..01cf15bc16 --- /dev/null +++ b/vlib/v/checker/tests/optional_method_err.vv @@ -0,0 +1,20 @@ +struct Abc { +mut: + x int +} + +fn (mut a Abc) inc_to_limit(max int) ?int { + if a.x >= max { + return error('x is already $max') + } + a.x++ + return a.x +} + +fn main() { + mut a := Abc{} + for _ in 0 .. 4 { + _ := a.inc_to_limit(2) + eprintln('a: $a') + } +}