This cop looks for trailing blank lines and a final newline in the source code.
# `final_blank_line` looks for one blank line followed by a new line# at the end of files. # badclass Foo; end# EOF # badclass Foo; end # EOF # goodclass Foo; end # EOF
# `final_newline` looks for one newline at the end of files. # badclass Foo; end # EOF # badclass Foo; end # EOF # goodclass Foo; end# EOF
This cop transforms usages of a method call safeguarded by a non nil
check for the variable whose method is being called to
safe navigation (&.
). If there is a method chain, all of the methods
in the chain need to be checked for safety, and all of the methods will
need to be changed to use safe navigation. We have limited the cop to
not register an offense for method chains that exceed 2 methods.
Configuration option: ConvertCodeThatCanStartToReturnNil
The default for this is false
. When configured to true
, this will
check for code in the format !foo.nil? && foo.bar
. As it is written,
the return of this code is limited to false
and whatever the return
of the method is. If this is converted to safe navigation,
foo&.bar
can start returning nil
as well as what the method
returns.
# badfoo.bar if foofoo.bar.baz if foofoo.bar(param1, param2) if foofoo.bar { |e| e.something } if foofoo.bar(param) { |e| e.something } if foo foo.bar if !foo.nil?foo.bar unless !foofoo.bar unless foo.nil? foo && foo.barfoo && foo.bar.bazfoo && foo.bar(param1, param2)foo && foo.bar { |e| e.something }foo && foo.bar(param) { |e| e.something } # goodfoo&.barfoo&.bar&.bazfoo&.bar(param1, param2)foo&.bar { |e| e.something }foo&.bar(param) { |e| e.something }foo && foo.bar.baz.qux # method chain with more than 2 methodsfoo && foo.nil? # method that `nil` responds to # Method calls that do not use `.`foo && foo < barfoo < bar if foo # This could start returning `nil` as well as the return of the methodfoo.nil? || foo.bar!foo || foo.bar # Methods that are used on assignment, arithmetic operation or# comparison should not be converted to use safe navigationfoo.baz = bar if foofoo.baz + bar if foofoo.bar > 2 if foo