Merge: core: fix some "call on nullable" warnings
authorJean Privat <jean@pryen.org>
Wed, 1 Jun 2016 01:24:31 +0000 (21:24 -0400)
committerJean Privat <jean@pryen.org>
Wed, 1 Jun 2016 01:24:31 +0000 (21:24 -0400)
Too tired too work but no enough to sleep...

According to Jenkins [here](http://gresil.org/jenkins/job/CI_github/4177/warnings2Result/package.795683049/), [here](http://gresil.org/jenkins/job/CI_github/4177/warnings2Result/package.-1960060028/) and [here](http://gresil.org/jenkins/job/CI_github/4177/warnings2Result/package.1056020115/), this PR should fix the remaining warning of `core/`.

The algorithm used for the human/pillow approach was:

~~~
1. can it be fixed shamelessly with a .as(not null) ?
    yes: add that sweet .as(not null) and hope for the best
2. is there more than one occurrence of the variable involved?
    yes: factorise with a local variable, add that sweet .as(not null) and hope for the best
3. is this code to complex for my human/pillow head?
    yes: shame = false, fallback to 1.
abort "Not yet occurred case..."
~~~

There is love for @R4PaSs (core::text), @xymus (core::re) and @privat (core::*).

TL;DR No more warnings in core.

Pull-Request: #2027
Reviewed-by: Jean Privat <jean@pryen.org>

1  2 
lib/core/file.nit

diff --combined lib/core/file.nit
@@@ -49,23 -49,24 +49,24 @@@ abstract class FileStrea
        # Return null in case of error
        fun file_stat: nullable FileStat
        do
-               var stat = _file.file_stat
+               var stat = _file.as(not null).file_stat
                if stat.address_is_null then return null
                return new FileStat(stat)
        end
  
        # File descriptor of this file
-       fun fd: Int do return _file.fileno
+       fun fd: Int do return _file.as(not null).fileno
  
        redef fun close
        do
-               if _file == null then return
-               if _file.address_is_null then
+               var file = _file
+               if file == null then return
+               if file.address_is_null then
                        if last_error != null then return
                        last_error = new IOError("Cannot close unopened file")
                        return
                end
-               var i = _file.io_close
+               var i = file.io_close
                if i != 0 then
                        last_error = new IOError("Close failed due to error {sys.errno.strerror}")
                end
@@@ -83,7 -84,7 +84,7 @@@
        # * `buffer_mode_none`
        fun set_buffering_mode(buf_size, mode: Int) do
                if buf_size <= 0 then buf_size = 512
-               if _file.set_buffering_type(buf_size, mode) != 0 then
+               if _file.as(not null).set_buffering_type(buf_size, mode) != 0 then
                        last_error = new IOError("Error while changing buffering type for FileStream, returned error {sys.errno.strerror}")
                end
        end
@@@ -105,10 -106,10 +106,10 @@@ class FileReade
        #     assert l == f.read_line
        fun reopen
        do
-               if not eof and not _file.address_is_null then close
+               if not eof and not _file.as(not null).address_is_null then close
                last_error = null
-               _file = new NativeFile.io_open_read(path.to_cstring)
-               if _file.address_is_null then
+               _file = new NativeFile.io_open_read(path.as(not null).to_cstring)
+               if _file.as(not null).address_is_null then
                        last_error = new IOError("Cannot open `{path.as(not null)}`: {sys.errno.strerror}")
                        end_reached = true
                        return
  
        redef fun fill_buffer
        do
-               var nb = _file.io_read(_buffer, _buffer_capacity)
-               if last_error == null and _file.ferror then
+               var nb = _file.as(not null).io_read(_buffer, _buffer_capacity)
+               if last_error == null and _file.as(not null).ferror then
                        last_error = new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
                        end_reached = true
                end
                self.path = path
                prepare_buffer(100)
                _file = new NativeFile.io_open_read(path.to_cstring)
-               if _file.address_is_null then
+               if _file.as(not null).address_is_null then
                        last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
                        end_reached = true
                end
                self.path = ""
                prepare_buffer(1)
                _file = fd.fd_to_stream(read_only)
-               if _file.address_is_null then
+               if _file.as(not null).address_is_null then
                        last_error = new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
                        end_reached = true
                end
@@@ -223,13 -224,13 +224,13 @@@ class FileWrite
                        last_error = new IOError("Cannot write to non-writable stream")
                        return
                end
-               if _file.address_is_null then
+               if _file.as(not null).address_is_null then
                        last_error = new IOError("Writing on a null stream")
                        _is_writable = false
                        return
                end
  
-               var err = _file.write_byte(value)
+               var err = _file.as(not null).write_byte(value)
                if err != 1 then
                        # Big problem
                        last_error = new IOError("Problem writing a byte: {err}")
                        last_error = new IOError("Cannot write to non-writable stream")
                        return
                end
-               if _file.address_is_null then
+               if _file.as(not null).address_is_null then
                        last_error = new IOError("Writing on a null stream")
                        _is_writable = false
                        return
                end
-               var err = _file.io_write(native, from, len)
+               var err = _file.as(not null).io_write(native, from, len)
                if err != len then
                        # Big problem
                        last_error = new IOError("Problem in writing : {err} {len} \n")
                _file = new NativeFile.io_open_write(path.to_cstring)
                self.path = path
                _is_writable = true
-               if _file.address_is_null then
+               if _file.as(not null).address_is_null then
                        last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
                        is_writable = false
                end
                self.path = ""
                _file = fd.fd_to_stream(wipe_write)
                _is_writable = true
-                if _file.address_is_null then
+                if _file.as(not null).address_is_null then
                         last_error = new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
                        _is_writable = false
                end
@@@ -663,19 -664,6 +664,19 @@@ class Pat
                return res
        end
  
 +      # Is `self` the path to an existing directory ?
 +      #
 +      # ~~~nit
 +      # assert ".".to_path.is_dir
 +      # assert not "/etc/issue".to_path.is_dir
 +      # assert not "/should/not/exist".to_path.is_dir
 +      # ~~~
 +      fun is_dir: Bool do
 +              var st = stat
 +              if st == null then return false
 +              return st.is_dir
 +      end
 +
        # Delete a directory and all of its content
        #
        # Does not go through symbolic links and may get stuck in a cycle if there