Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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_

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Introduce Warning

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

What can we do with warning?

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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).

Slide 14

Slide 14 text

Why should we fix warnings?

Slide 15

Slide 15 text

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”.

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Warning examples

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Deprecated method cookpad/rrrspec#76

Slide 20

Slide 20 text

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!

Slide 21

Slide 21 text

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

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.

Slide 26

Slide 26 text

Method redefined whitequark/parser#378

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Patch

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

duplicated when clause thoughtbot/capybara-webkit#1068

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Part of the patch

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Part of the patch (1)

Slide 41

Slide 41 text

Part of the patch (2)

Slide 42

Slide 42 text

Part of the patch (3)

Slide 43

Slide 43 text

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.

Slide 44

Slide 44 text

instance variable not initialized mizzy/specinfra#685

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Patch

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Conclusion

Slide 50

Slide 50 text

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!