From: Rico Tzschichholz Date: Sat, 24 Apr 2021 07:11:27 +0000 (+0200) Subject: vala: Report a warning for unhandled errors in destructors X-Git-Tag: 0.50.8~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b080a725dde28abb382490728f96ac5c67e57b8;p=thirdparty%2Fvala.git vala: Report a warning for unhandled errors in destructors Fixes https://gitlab.gnome.org/GNOME/vala/issues/1176 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 611b3ae9f..3f2f5dea8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -603,6 +603,7 @@ TESTS = \ errors/invalid-type-check.test \ errors/loops.vala \ errors/method-throws.vala \ + errors/unhandled.vala \ errors/bug567181.vala \ errors/bug579101.vala \ errors/bug596228.vala \ diff --git a/tests/errors/unhandled.vala b/tests/errors/unhandled.vala new file mode 100644 index 000000000..1a3768a6c --- /dev/null +++ b/tests/errors/unhandled.vala @@ -0,0 +1,45 @@ +public errordomain FooError { + FAIL +} + +public class Foo : Object { + public string bar { + get { + throw new FooError.FAIL ("property getter"); + } + set { + throw new FooError.FAIL ("property setter"); + } + } + + public Foo () { + throw new FooError.FAIL ("creation method"); + } + + construct { + throw new FooError.FAIL ("constructor"); + } + + class construct { + throw new FooError.FAIL ("class constructor"); + } + + static construct { + throw new FooError.FAIL ("static constructor"); + } + + ~Foo () { + throw new FooError.FAIL ("destructor"); + } + + class ~Foo () { + throw new FooError.FAIL ("class destructor"); + } + + public void foo () { + throw new FooError.FAIL ("method"); + } +} + +void main () { +} diff --git a/vala/valadestructor.vala b/vala/valadestructor.vala index 61797ea22..a04849673 100644 --- a/vala/valadestructor.vala +++ b/vala/valadestructor.vala @@ -77,6 +77,16 @@ public class Vala.Destructor : Subroutine { body.check (context); } + if (body != null && !body.error) { + var body_errors = new ArrayList (); + body.get_error_types (body_errors); + foreach (DataType body_error_type in body_errors) { + if (!((ErrorType) body_error_type).dynamic_error) { + Report.warning (body_error_type.source_reference, "unhandled error `%s'".printf (body_error_type.to_string())); + } + } + } + context.analyzer.current_symbol = parent_symbol; return !error;