$30 off During Our Annual Pro Sale. View Details »

Suppress Warning

pocke
September 15, 2019

Suppress Warning

pocke

September 15, 2019
Tweet

More Decks by pocke

Other Decks in Programming

Transcript

  1. Suppress Warnings
    15th, Sep. 2019
    大阪Ruby会議02

    View Slide

  2. pp self
    ● Masataka “Pocke” Kuwabara
    ● I’m from https://ruby-jp.github.io!
    ● Works for Bit Journey, Inc. / Kibela
    ● https://github.com/pocke
    ● https://twitter.com/p_ck_

    View Slide

  3. Recently we released Kibela Web API
    https://prtimes.jp/main/html/rd/p/000000008.000024220.html

    View Slide

  4. Goal of this talk
    ● You can:
    ○ Find bugs with warnings.
    ○ Reduce warnings from your applications.
    ○ Create patches to OSS to suppress warnings.

    View Slide

  5. Introduce
    Warning

    View Slide

  6. What is the warning?
    ● Ruby warns code, if
    ○ It may have a bug.
    ○ It is deprecated.
    ● For example:
    ○ Useless variable: x = 1; p y
    ○ Deprecated method: File.exists?
    ■ Use File.exist? instead.

    View Slide

  7. Warning levels
    ● Ruby has 3 levels for warning.
    ● Level 0: All warnings are disabled.
    ● Level 1: Only important warnings are enabled
    (default).
    ● Level 2: All warnings are enabled.
    ○ Almost warnings are enabled only with Level 2.

    View Slide

  8. How to enable all warnings
    ● `-w` option enables all warnings.
    ○ The level 2
    ● $ ruby -e 42
    ○ Print nothing
    ● $ ruby -w -e 42
    ○ -e:1: warning: possibly useless use of a
    literal in void context

    View Slide

  9. Enable warnings without CLI option
    ● We can also use RUBYOPT environment
    variable.
    ● RUBYOPT is parsed as Ruby’s command line
    option.
    ● RUBYOPT=-w bin/rails s
    ● $ echo ‘export RUBYOPT=”-w”’ >>
    ~/.bashrc

    View Slide

  10. What can we do
    with warning?

    View Slide

  11. With warning, we can:
    ● We can find bugs with warning.
    ● We can contribute to OSS easier.

    View Slide

  12. Find bugs
    ● Ruby warns possible bug code.
    ● If you solve a warning, a bug may be solved.

    View Slide

  13. Contribute to OSS easier
    ● Warning is easy to fix, in many cases.
    ● Because
    ○ The goal is clear.
    ○ The patch will be small.
    ● But some warnings is difficult to fix. e.g.
    ○ Uninitialized instance variable
    ○ The code has bug(s).

    View Slide

  14. Why should we
    fix warnings?

    View Slide

  15. Why should we fix warnings?
    ● We can find bugs with warnings.
    ● But if application has too many warnings, it is
    too difficult.
    ● Similar with “Broken Windows Theory”.

    View Slide

  16. View Slide

  17. Warning
    examples

    View Slide

  18. Warnings examples
    ● Let’s look warning with real pull requests.
    ○ Warning meaning.
    ○ How to fix warning.
    ○ What problems is found by the warning.

    View Slide

  19. Deprecated
    method
    cookpad/rrrspec#76

    View Slide

  20. Deprecated method
    ● File.exists?(path)
    ○ File.exists? is a deprecated name, use File.exist? instead
    ● Dir.exists?(path)
    ○ Dir.exists? is a deprecated name, use Dir.exist? instead
    Let’s look the pull request!

    View Slide


  21. View Slide

  22. Part of the patch
    - unless Dir.exists?(packaging_dir)
    + unless Dir.exist?(packaging_dir)

    View Slide

  23. Overview
    ● File.exists? and Dir.exists? are
    deprecated.
    ● It should be replaced with File.exist?.
    ● It does not change any behavior.

    View Slide

  24. It is one of the easiest warning!
    ● Good for first step of pull request!
    ● Because
    ○ Fixing approach is obvious.
    ○ It does not change any behavior.
    ● But it has only small impact.

    View Slide

  25. Other deprecated methods
    ● Kernel.#open for URI. (In Ruby 2.7.0-dev)
    ○ Use URI.open.
    ■ File.open, IO.open, and IO.popen are also avairable.
    ● URI.escape
    ○ Use ERB::Util.#url_encode, CGI.escape, or
    URI.encode_www_form_component
    ○ It depends on the requirement.

    View Slide

  26. Method
    redefined
    whitequark/parser#378

    View Slide

  27. Method Redefined
    ● def foo; end # It is ignored
    def foo; end
    ○ warning: method redefined; discarding old foo
    Let’s look the pull request!

    View Slide

  28. View Slide

  29. Patch

    View Slide

  30. Overview
    ● Two methods ware accidently redefined in
    minitest test file.
    ● A method has the same content, so I removed
    it.
    ● But the other one has different method body.

    View Slide

  31. In another case
    ● If you really want to redefine method,
    use remove_method.
    ○ remove_method :foo
    def foo; end
    ○ e.g. https://github.com/ruby/irb/pull/18

    View Slide

  32. duplicated
    when clause
    thoughtbot/capybara-webkit#1068

    View Slide

  33. Duplicated when clause
    ● case x
    when 1, 2, 1
    end
    ○ duplicated `when' clause with line 2 is ignored

    View Slide

  34. View Slide

  35. Part of the patch

    View Slide

  36. Character
    class has
    duplicated
    range
    rouge-ruby/rouge#1197

    View Slide

  37. Character class has duplicated range
    ● /[aa]/
    ○ “a” is duplicated.
    ● /[\w_]/
    ○ “\w” includes “_”.
    ● /[\s\n]/
    ○ “\s” includes new line.
    ● /[*+-=]/
    ○ It means “*” and chars between “+” and “=”.

    View Slide

  38. View Slide

  39. View Slide

  40. Part of the patch (1)

    View Slide

  41. Part of the patch (2)

    View Slide

  42. Part of the patch (3)

    View Slide

  43. Overview
    ● Ruby also warns Regexp.
    ● In this pull request, I found and fixed bugs.
    ○ → It is useful warning to find bug.
    ● I often find the same warning from validations
    in app/models/user.rb.

    View Slide

  44. instance
    variable not
    initialized
    mizzy/specinfra#685

    View Slide

  45. Instance variable not initialized
    ● : def foo
    @foo ||= something
    end
    ● : def foo
    @foo ? @foo : something
    end
    ○ instance variable @foo not initialized

    View Slide

  46. View Slide

  47. Patch

    View Slide

  48. Other solutions
    ● Use ||=
    ○ @foo ||= something # It is OK
    ● Use defined?
    ○ defined?(@foo) ? @foo : something

    View Slide

  49. Conclusion

    View Slide

  50. Conclusion
    ● We looked warnings examples.
    ○ Meanings, how to fix warnings, etc
    ● With using warnings, we can
    ○ Find bugs.
    ○ Contribute to OSS easier.
    ● Let’s try enabling warnings in your application!
    Thank you for listening!

    View Slide