[vala/wip/baedert/nullable: 2/4] Method: Keep track of return statements with nullable values



commit 2f299ac1df853b339e6dd63aa3aadfe50a8564d1
Author: Timm Bäder <mail baedert org>
Date:   Tue Nov 1 10:48:57 2016 +0100

    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.

 vala/valamethod.vala          |    8 ++++++++
 vala/valareturnstatement.vala |    5 +++++
 2 files changed, 13 insertions(+), 0 deletions(-)
---
diff --git a/vala/valamethod.vala b/vala/valamethod.vala
index f0f1980..ec35e00 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<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) {
diff --git a/vala/valareturnstatement.vala b/vala/valareturnstatement.vala
index e407aec..d1f349e 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;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]