From: Timm Bäder Date: Tue, 1 Nov 2016 09:48:57 +0000 (+0100) Subject: Method: Keep track of return statements with nullable values X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f299ac1df853b339e6dd63aa3aadfe50a8564d1;p=thirdparty%2Fvala.git Method: Keep track of return statements with nullable values If the return type of a method is marked nullable, check that at least one of the return statements also may return null. --- diff --git a/vala/valamethod.vala b/vala/valamethod.vala index f0f19809f..ec35e00b5 100644 --- a/vala/valamethod.vala +++ b/vala/valamethod.vala @@ -185,6 +185,8 @@ public class Vala.Method : Subroutine, Callable { public int yield_count { get; set; } + public bool has_nullable_return_statement { get; set; default = false;} + private List parameters = new ArrayList (); private List preconditions; private List postconditions; @@ -759,6 +761,12 @@ public class Vala.Method : Subroutine, Callable { if (body != null) { body.check (context); + if (!this.overrides && this.base_interface_method == null && + this.return_type.nullable && !this.has_nullable_return_statement) { + // The return type of the method is marked nullable but none + // of the return statements return a nullable type + Report.warning (this.source_reference, "Return type of %s is marked nullable but none of its return statements return a nullable value".printf (this.get_full_name ())); + } } if (context.analyzer.current_struct != null) { diff --git a/vala/valareturnstatement.vala b/vala/valareturnstatement.vala index e407aec4a..d1f349e51 100644 --- a/vala/valareturnstatement.vala +++ b/vala/valareturnstatement.vala @@ -138,6 +138,11 @@ public class Vala.ReturnStatement : CodeNode, Statement { Report.warning (source_reference, "`null' incompatible with return type `%s`".printf (context.analyzer.current_return_type.to_string ())); } + if (return_expression.value_type.nullable && + context.analyzer.current_method != null) { + context.analyzer.current_method.has_nullable_return_statement = true; + } + add_error_types (return_expression.get_error_types ()); return !error;