Merge: Useless type
authorJean Privat <jean@pryen.org>
Fri, 28 Nov 2014 21:19:31 +0000 (16:19 -0500)
committerJean Privat <jean@pryen.org>
Fri, 28 Nov 2014 21:19:31 +0000 (16:19 -0500)
Fix a wrong `useless-type` warning for attributes.
Extends the `useless-type` warning to local variables.

Pull-Request: #945

lib/opts.nit
lib/standard/ropes.nit
lib/standard/string.nit
src/modelize/modelize_property.nit
src/semantize/typing.nit

index 5e36142..5a226b9 100644 (file)
@@ -354,7 +354,7 @@ class OptionContext
 
        fun get_errors: Array[String]
        do
-               var errors: Array[String] = new Array[String]
+               var errors = new Array[String]
                errors.add_all(errors)
                for o in options do
                        for e in o.errors do
index ad09beb..12ad67e 100644 (file)
@@ -626,7 +626,7 @@ private class ReverseRopeSubstrings
 
        redef fun next do
                if pos < 0 then return
-               var curr: nullable RopeIterPiece = iter.prev
+               var curr = iter.prev
                var currit = curr.node
                while curr != null do
                        currit = curr.node
@@ -753,7 +753,7 @@ private class RopeSubstrings
                pos += str.length
                if pos > max then return
                var it = iter.prev
-               var rnod: String = it.node
+               var rnod = it.node
                loop
                        if not rnod isa Concat then
                                it.ldone = true
index 62d5ddc..e17d2cf 100644 (file)
@@ -1418,7 +1418,7 @@ abstract class Buffer
                if length == 0 then return
                var c = self[0].to_upper
                self[0] = c
-               var prev: Char = c
+               var prev = c
                for i in [1 .. length[ do
                        prev = c
                        c = self[i]
index 81b3245..440f245 100644 (file)
@@ -945,11 +945,13 @@ redef class AAttrPropdef
                        if mtype == null then return
                end
 
+               var inherited_type: nullable MType = null
                # Inherit the type from the getter (usually an abstract getter)
-               if mtype == null and mreadpropdef != null and not mreadpropdef.is_intro then
+               if mreadpropdef != null and not mreadpropdef.is_intro then
                        var msignature = mreadpropdef.mproperty.intro.msignature
                        if msignature == null then return # Error, thus skipped
-                       mtype = msignature.return_mtype
+                       inherited_type = msignature.return_mtype
+                       if mtype == null then mtype = inherited_type
                end
 
                var nexpr = self.n_expr
@@ -981,7 +983,7 @@ redef class AAttrPropdef
 
                                if mtype == null then return
                        end
-               else if ntype != null then
+               else if ntype != null and inherited_type == mtype then
                        if nexpr isa ANewExpr then
                                var xmtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
                                if xmtype == mtype then
index 3140de5..cdb8ae8 100644 (file)
@@ -668,7 +668,11 @@ redef class AVardeclExpr
                var nexpr = self.n_expr
                if nexpr != null then
                        if mtype != null then
-                               v.visit_expr_subtype(nexpr, mtype)
+                               var etype = v.visit_expr_subtype(nexpr, mtype)
+                               if etype == mtype then
+                                       assert ntype != null
+                                       v.modelbuilder.advice(ntype, "useless-type", "Warning: useless type definition for variable `{variable.name}`")
+                               end
                        else
                                mtype = v.visit_expr(nexpr)
                                if mtype == null then return # Skip error