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. 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_
  2. Goal of this talk • You can: ◦ Find bugs

    with warnings. ◦ Reduce warnings from your applications. ◦ Create patches to OSS to suppress warnings.
  3. 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.
  4. 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.
  5. 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
  6. 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
  7. With warning, we can: • We can find bugs with

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

    you solve a warning, a bug may be solved.
  9. 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).
  10. 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”.
  11. Warnings examples • Let’s look warning with real pull requests.

    ◦ Warning meaning. ◦ How to fix warning. ◦ What problems is found by the warning.
  12. 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!
  13. Overview • File.exists? and Dir.exists? are deprecated. • It should

    be replaced with File.exist?. • It does not change any behavior.
  14. 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.
  15. 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.
  16. Method Redefined • def foo; end # It is ignored

    def foo; end ◦ warning: method redefined; discarding old foo Let’s look the pull request!
  17. 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.
  18. 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
  19. Duplicated when clause • case x when 1, 2, 1

    end ◦ duplicated `when' clause with line 2 is ignored
  20. Character class has duplicated range • /[aa]/ ◦ “a” is

    duplicated. • /[\w_]/ ◦ “\w” includes “_”. • /[\s\n]/ ◦ “\s” includes new line. • /[*+-=]/ ◦ It means “*” and chars between “+” and “=”.
  21. 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.
  22. Instance variable not initialized • : def foo @foo ||=

    something end • : def foo @foo ? @foo : something end ◦ instance variable @foo not initialized
  23. Other solutions • Use ||= ◦ @foo ||= something #

    It is OK • Use defined? ◦ defined?(@foo) ? @foo : something
  24. 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!