]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Method: Keep track of return statements with nullable values
authorTimm Bäder <mail@baedert.org>
Tue, 1 Nov 2016 09:48:57 +0000 (10:48 +0100)
committerTimm Bäder <mail@baedert.org>
Tue, 1 Nov 2016 10:18:56 +0000 (11:18 +0100)
If the return type of a method is marked nullable, check that at least
one of the return statements also may return null.

vala/valamethod.vala
vala/valareturnstatement.vala

index f0f19809fecf04db190159a45a46d576fef92dd7..ec35e00b5c1b87f0b2cac19ede5aba8200946b53 100644 (file)
@@ -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<Parameter> parameters = new ArrayList<Parameter> ();
        private List<Expression> preconditions;
        private List<Expression> 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) {
index e407aec4af2acece5778c862164ed2027ecaf916..d1f349e51906eebe4df908b18e86dc789e8e9c78 100644 (file)
@@ -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;