From 45f4c2dc29a388ef4510eb186f0240d91d8d4f95 Mon Sep 17 00:00:00 2001 From: Robert Haines Date: Sun, 9 Feb 2020 15:50:00 +0000 Subject: [PATCH] Fix Style/GuardClause cop. --- .rubocop_todo.yml | 9 --------- lib/zip/entry.rb | 17 ++++++----------- lib/zip/extra_field/generic.rb | 13 ++++++------- samples/zipfind.rb | 8 ++++---- test/test_helper.rb | 25 ++++++++++++------------- 5 files changed, 28 insertions(+), 44 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 21879bf..b35b103 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -163,15 +163,6 @@ Style/FormatStringToken: Style/FrozenStringLiteralComment: Enabled: false -# Offense count: 8 -# Configuration parameters: MinBodyLength. -Style/GuardClause: - Exclude: - - 'lib/zip/entry.rb' - - 'lib/zip/extra_field/generic.rb' - - 'samples/zipfind.rb' - - 'test/test_helper.rb' - # Offense count: 17 # Cop supports --auto-correct. Style/IfUnlessModifier: diff --git a/lib/zip/entry.rb b/lib/zip/entry.rb index d3470e1..0801ad2 100644 --- a/lib/zip/entry.rb +++ b/lib/zip/entry.rb @@ -182,12 +182,9 @@ module Zip dest_path ||= @name block ||= proc { ::Zip.on_exists_proc } - if directory? || file? || symlink? - __send__("create_#{@ftype}", dest_path, &block) - else - raise "unknown file type #{inspect}" - end + raise "unknown file type #{inspect}" unless directory? || file? || symlink? + __send__("create_#{@ftype}", dest_path, &block) self end @@ -630,12 +627,10 @@ module Zip bytes_written += buf.bytesize if bytes_written > size && !warned message = "entry '#{name}' should be #{size}B, but is larger when inflated." - if ::Zip.validate_entry_sizes - raise ::Zip::EntrySizeError, message - else - warn "WARNING: #{message}" - warned = true - end + raise ::Zip::EntrySizeError, message if ::Zip.validate_entry_sizes + + warn "WARNING: #{message}" + warned = true end end end diff --git a/lib/zip/extra_field/generic.rb b/lib/zip/extra_field/generic.rb index 0bb000b..7baf9e4 100644 --- a/lib/zip/extra_field/generic.rb +++ b/lib/zip/extra_field/generic.rb @@ -1,9 +1,9 @@ module Zip class ExtraField::Generic def self.register_map - if const_defined?(:HEADER_ID) - ::Zip::ExtraField::ID_MAP[const_get(:HEADER_ID)] = self - end + return unless const_defined?(:HEADER_ID) + + ::Zip::ExtraField::ID_MAP[const_get(:HEADER_ID)] = self end def self.name @@ -12,10 +12,9 @@ module Zip # return field [size, content] or false def initial_parse(binstr) - if !binstr - # If nil, start with empty. - return false - elsif binstr[0, 2] != self.class.const_get(:HEADER_ID) + return false unless binstr + + if binstr[0, 2] != self.class.const_get(:HEADER_ID) warn 'WARNING: weird extra field header ID. Skip parsing it.' return false end diff --git a/samples/zipfind.rb b/samples/zipfind.rb index 7a8ac45..59ca1fd 100755 --- a/samples/zipfind.rb +++ b/samples/zipfind.rb @@ -48,10 +48,10 @@ if $0 == __FILE__ end def self.check_args(args) - if args.size != 3 - usage - exit - end + return if args.size == 3 + + usage + exit end def self.usage diff --git a/test/test_helper.rb b/test/test_helper.rb index 7a0edfc..d1777bd 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -42,11 +42,10 @@ module IOizeString else raise 'Error in test method IOizeString::seek' end - if newPos < 0 || newPos >= size - raise Errno::EINVAL - else - @tell = newPos - end + + raise Errno::EINVAL if newPos < 0 || newPos >= size + + @tell = newPos end def reset @@ -107,14 +106,14 @@ module AssertEntry def self.assert_contents(filename, aString) fileContents = '' File.open(filename, 'rb') { |f| fileContents = f.read } - if fileContents != aString - if fileContents.length > 400 || aString.length > 400 - stringFile = filename + '.other' - File.open(stringFile, 'wb') { |f| f << aString } - fail("File '#{filename}' is different from contents of string stored in '#{stringFile}'") - else - assert_equal(fileContents, aString) - end + return unless fileContents != aString + + if fileContents.length > 400 || aString.length > 400 + stringFile = filename + '.other' + File.open(stringFile, 'wb') { |f| f << aString } + fail("File '#{filename}' is different from contents of string stored in '#{stringFile}'") + else + assert_equal(fileContents, aString) end end