[vala/wip/issue/894: 76/76] vala: Automatically remove superfluous silent casts




commit 461b104cb8fdd4d5c15ce820459970c5d4a72220
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Aug 11 15:22:21 2020 +0200

    vala: Automatically remove superfluous silent casts
    
    If type of expression is target type then omit this GType cast.
    
    This will clean up existing sources which matches the expected scheme for
    type narrowing, see https://gitlab.gnome.org/GNOME/vala/issues/894

 tests/Makefile.am                 |  1 +
 tests/objects/casting-silent.vala | 28 ++++++++++++++++++++++++++++
 vala/valacastexpression.vala      |  9 +++++++++
 3 files changed, 38 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9e22e1bf3..6bf76f2ae 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -369,6 +369,7 @@ TESTS = \
        delegates/bug761360.vala \
        delegates/bug772204.test \
        delegates/bug792077.vala \
+       objects/casting-silent.vala \
        objects/chainup.vala \
        objects/class-ccode-cprefix.vala \
        objects/class_only.vala \
diff --git a/tests/objects/casting-silent.vala b/tests/objects/casting-silent.vala
new file mode 100644
index 000000000..51928cd75
--- /dev/null
+++ b/tests/objects/casting-silent.vala
@@ -0,0 +1,28 @@
+class Foo {
+}
+
+class Bar : Foo {
+}
+
+Bar get_bar () {
+       return new Bar ();
+}
+
+void main() {
+       {
+               var bar = new Foo ();
+               var foo = bar as Foo;
+
+               assert (foo == bar);
+               assert (foo == (bar as Foo));
+               assert (bar == (foo as Foo));
+       }
+       {
+               var bar = get_bar () as Foo;
+               var foo = bar as Bar;
+
+               assert (foo == bar);
+               assert (foo == (bar as Bar));
+               assert (bar == (foo as Foo));
+       }
+}
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index f86aad157..b3b90278d 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -199,6 +199,15 @@ public class Vala.CastExpression : Expression {
 
                if (is_silent_cast) {
                        value_type.nullable = true;
+
+                       // Drop superfluous casts, if the source and target type matches then this cast is 
not required
+                       if (context.profile == Profile.GOBJECT
+                           && type_reference.type_symbol is ObjectTypeSymbol && type_reference.type_symbol 
== inner.value_type.type_symbol) {
+                               Report.warning (source_reference, "Casting to `%s' is superfluous".printf 
(type_reference.to_qualified_string ()));
+                               context.analyzer.replaced_nodes.add (this);
+                               parent_node.replace_expression (this, inner);
+                               return !error;
+                       }
                }
 
                if (context.profile == Profile.GOBJECT


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