Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

  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_
  3. Recently we released Kibela Web API https://prtimes.jp/main/html/rd/p/000000008.000024220.html

  4. Goal of this talk • You can: ◦ Find bugs

    with warnings. ◦ Reduce warnings from your applications. ◦ Create patches to OSS to suppress warnings.
  5. Introduce Warning

  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.
  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.
  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
  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
  10. What can we do with warning?

  11. With warning, we can: • We can find bugs with

    warning. • We can contribute to OSS easier.
  12. Find bugs • Ruby warns possible bug code. • If

    you solve a warning, a bug may be solved.
  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).
  14. Why should we fix warnings?

  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”.
  16. None
  17. Warning examples

  18. Warnings examples • Let’s look warning with real pull requests.

    ◦ Warning meaning. ◦ How to fix warning. ◦ What problems is found by the warning.
  19. Deprecated method cookpad/rrrspec#76

  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!
  21. Part of the patch - unless Dir.exists?(packaging_dir) + unless Dir.exist?(packaging_dir)

  22. Overview • File.exists? and Dir.exists? are deprecated. • It should

    be replaced with File.exist?. • It does not change any behavior.
  23. 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.
  24. 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.
  25. Method redefined whitequark/parser#378

  26. Method Redefined • def foo; end # It is ignored

    def foo; end ◦ warning: method redefined; discarding old foo Let’s look the pull request!
  27. None
  28. Patch

  29. 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.
  30. 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
  31. duplicated when clause thoughtbot/capybara-webkit#1068

  32. Duplicated when clause • case x when 1, 2, 1

    end ◦ duplicated `when' clause with line 2 is ignored
  33. None
  34. Part of the patch

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

  36. Character class has duplicated range • /[aa]/ ◦ “a” is

    duplicated. • /[\w_]/ ◦ “\w” includes “_”. • /[\s\n]/ ◦ “\s” includes new line. • /[*+-=]/ ◦ It means “*” and chars between “+” and “=”.
  37. None
  38. None
  39. Part of the patch (1)

  40. Part of the patch (2)

  41. Part of the patch (3)

  42. 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.
  43. instance variable not initialized mizzy/specinfra#685

  44. Instance variable not initialized • : def foo @foo ||=

    something end • : def foo @foo ? @foo : something end ◦ instance variable @foo not initialized
  45. None
  46. Patch

  47. Other solutions • Use ||= ◦ @foo ||= something #

    It is OK • Use defined? ◦ defined?(@foo) ? @foo : something
  48. Conclusion

  49. 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!