]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Warn when using 'this' before chain up
authorSimon Werbeck <simon.werbeck@gmail.com>
Wed, 26 Nov 2014 11:24:31 +0000 (12:24 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 19 Feb 2018 16:54:32 +0000 (17:54 +0100)
Do flow analysis on 'this' parameter in creation methods. Report a
warning if it is used before a chain up call.

https://bugzilla.gnome.org/show_bug.cgi?id=567269

vala/valaflowanalyzer.vala
vala/valamemberaccess.vala
vala/valamethodcall.vala
vala/valaobjectcreationexpression.vala

index e073c4c21c8d50c42cd913f78b5875a7b1b599ea..d2de29ca37dfc8c813116130de621186890c18c7 100644 (file)
@@ -429,7 +429,11 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                                                        Report.error (used_var.source_reference, "use of possibly unassigned local variable `%s'".printf (used_var.name));
                                                } else {
                                                        // parameter
-                                                       Report.warning (used_var.source_reference, "use of possibly unassigned parameter `%s'".printf (used_var.name));
+                                                       if (used_var.name == "this") {
+                                                               Report.warning (used_var.source_reference, "possible reference to `this' before chaining up");
+                                                       } else {
+                                                               Report.warning (used_var.source_reference, "use of possibly unassigned parameter `%s'".printf (used_var.name));
+                                                       }
                                                }
                                                continue;
                                        }
@@ -461,7 +465,11 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                                                Report.error (node.source_reference, "use of possibly unassigned local variable `%s'".printf (var_symbol.name));
                                        } else {
                                                // parameter
-                                               Report.warning (node.source_reference, "use of possibly unassigned parameter `%s'".printf (var_symbol.name));
+                                               if (var_symbol.name == "this") {
+                                                       Report.warning (node.source_reference, "possible reference to `this' before chaining up");
+                                               } else {
+                                                       Report.warning (node.source_reference, "use of possibly unassigned parameter `%s'".printf (var_symbol.name));
+                                               }
                                        }
                                        continue;
                                }
index 3bc9eb13bff95107f2f0d9ca68f062c295d71d00..4fac98adc62998e7952b64c9f2e3df5a339aac9b 100644 (file)
@@ -950,7 +950,7 @@ public class Vala.MemberAccess : Expression {
                var param = symbol_reference as Parameter;
                if (local != null) {
                        collection.add (local);
-               } else if (param != null && param.direction == ParameterDirection.OUT) {
+               } else if (param != null && (param.direction == ParameterDirection.OUT || (param.name == "this" && param.parent_symbol is CreationMethod && ((CreationMethod) param.parent_symbol).chain_up))) {
                        collection.add (param);
                }
        }
index 2b43ec842f87d9d4f7dfe16038e62c996bed72fc..5c17ef098ef5b1fdd7cace1b807074d155a548c1 100644 (file)
@@ -287,6 +287,7 @@ public class Vala.MethodCall : Expression {
 
                        var struct_creation_expression = new ObjectCreationExpression ((MemberAccess) call, source_reference);
                        struct_creation_expression.struct_creation = true;
+                       struct_creation_expression.struct_chainup = is_chainup;
                        foreach (Expression arg in get_argument_list ()) {
                                struct_creation_expression.add_argument (arg);
                        }
@@ -677,7 +678,19 @@ public class Vala.MethodCall : Expression {
        }
 
        public override void get_defined_variables (Collection<Variable> collection) {
-               call.get_defined_variables (collection);
+               if (is_chainup) {
+                       var this_parameter = call.symbol_reference as Parameter;
+                       if (this_parameter == null) {
+                               Symbol sym = (Block) parent_statement.parent_node;
+                               do {
+                                       sym = sym.parent_symbol;
+                               } while (sym is Block);
+                               this_parameter = ((Method) sym).this_parameter;
+                       }
+                       collection.add (this_parameter);
+               } else {
+                       call.get_defined_variables (collection);
+               }
 
                foreach (Expression arg in argument_list) {
                        arg.get_defined_variables (collection);
@@ -685,7 +698,9 @@ public class Vala.MethodCall : Expression {
        }
 
        public override void get_used_variables (Collection<Variable> collection) {
-               call.get_used_variables (collection);
+               if (!is_chainup) {
+                       call.get_used_variables (collection);
+               }
 
                foreach (Expression arg in argument_list) {
                        arg.get_used_variables (collection);
index 7a2d6fbf809f110d88d99bcf2288fbaf0499d773..a920673b049159cecf53adab6fd72da311b58fbd 100644 (file)
@@ -46,6 +46,7 @@ public class Vala.ObjectCreationExpression : Expression {
        public bool is_yield_expression { get; set; }
 
        public bool struct_creation { get; set; }
+       public bool struct_chainup { get; set; }
 
        private List<Expression> argument_list = new ArrayList<Expression> ();
 
@@ -535,6 +536,18 @@ public class Vala.ObjectCreationExpression : Expression {
        }
 
        public override void get_defined_variables (Collection<Variable> collection) {
+               if (struct_chainup) {
+                       var this_parameter = member_name.inner.symbol_reference as Parameter;
+                       if (this_parameter == null) {
+                               Symbol sym = (Block) parent_statement.parent_node;
+                               do {
+                                       sym = sym.parent_symbol;
+                               } while (sym is Block);
+                               this_parameter = ((Method) sym).this_parameter;
+                       }
+                       collection.add (this_parameter);
+               }
+
                foreach (Expression arg in argument_list) {
                        arg.get_defined_variables (collection);
                }