diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index bee5f8f2..50c86e74 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -334,13 +334,6 @@ Style/GlobalVars: Exclude: - 'testserver/ldapserver.rb' -# Offense count: 2 -# Configuration parameters: MinBodyLength. -Style/GuardClause: - Exclude: - - 'lib/net/ldap/connection.rb' - - 'test/test_ldap_connection.rb' - # Offense count: 161 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. diff --git a/lib/net/ber.rb b/lib/net/ber.rb index 88f8862e..eb6f04b3 100644 --- a/lib/net/ber.rb +++ b/lib/net/ber.rb @@ -327,11 +327,10 @@ def initialize args # Check the encoding of the newly created String and set the encoding # to 'UTF-8' (NOTE: we do NOT change the bytes, but only set the # encoding to 'UTF-8'). + return unless encoding == Encoding::BINARY current_encoding = encoding - if current_encoding == Encoding::BINARY - force_encoding('UTF-8') - force_encoding(current_encoding) unless valid_encoding? - end + force_encoding('UTF-8') + force_encoding(current_encoding) unless valid_encoding? end end diff --git a/lib/net/ber/ber_parser.rb b/lib/net/ber/ber_parser.rb index ee69eed8..39d3737e 100644 --- a/lib/net/ber/ber_parser.rb +++ b/lib/net/ber/ber_parser.rb @@ -172,10 +172,10 @@ def read_ber(syntax = nil) yield id, content_length if block_given? if -1 == content_length - raise Net::BER::BerError, "Indeterminite BER content length not implemented." - else - data = read(content_length) + raise Net::BER::BerError, + "Indeterminite BER content length not implemented." end + data = read(content_length) parse_ber_object(syntax, id, data) end diff --git a/lib/net/ldap.rb b/lib/net/ldap.rb index 4ba27339..bcaa579c 100644 --- a/lib/net/ldap.rb +++ b/lib/net/ldap.rb @@ -1286,11 +1286,9 @@ def use_connection(args) else begin conn = new_connection - if (result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess - yield conn - else - return result - end + result = conn.bind(args[:auth] || @auth) + return result unless result.code == Net::LDAP::ResultCodeSuccess + yield conn ensure conn.close if conn end diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index 1cbcbb67..05f676cc 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -95,17 +95,13 @@ def self.wrap_with_ssl(io, tls_options = {}, timeout=nil) conn.connect end rescue IO::WaitReadable - if IO.select([conn], nil, nil, timeout) - retry - else - raise Errno::ETIMEDOUT, "OpenSSL connection read timeout" - end + raise Errno::ETIMEDOUT, "OpenSSL connection read timeout" unless + IO.select([conn], nil, nil, timeout) + retry rescue IO::WaitWritable - if IO.select(nil, [conn], nil, timeout) - retry - else - raise Errno::ETIMEDOUT, "OpenSSL connection write timeout" - end + raise Errno::ETIMEDOUT, "OpenSSL connection write timeout" unless + IO.select(nil, [conn], nil, timeout) + retry end # Doesn't work: @@ -163,11 +159,9 @@ def setup_encryption(args, timeout=nil) raise Net::LDAP::NoStartTLSResultError, "no start_tls result" end - if pdu.result_code.zero? - @conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout) - else - raise Net::LDAP::StartTLSError, "start_tls failed: #{pdu.result_code}" - end + raise Net::LDAP::StartTLSError, + "start_tls failed: #{pdu.result_code}" unless pdu.result_code.zero? + @conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout) else raise Net::LDAP::EncMethodUnsupportedError, "unsupported encryption method #{args[:method]}" end @@ -197,12 +191,10 @@ def queued_read(message_id) # read messages until we have a match for the given message_id while pdu = read - if pdu.message_id == message_id - return pdu - else - message_queue[pdu.message_id].push pdu - next - end + return pdu if pdu.message_id == message_id + + message_queue[pdu.message_id].push pdu + next end pdu diff --git a/lib/net/ldap/dn.rb b/lib/net/ldap/dn.rb index 3037eefd..e314b80e 100644 --- a/lib/net/ldap/dn.rb +++ b/lib/net/ldap/dn.rb @@ -169,11 +169,10 @@ def each_pair end # Last pair - if [:value, :value_normal, :value_hexstring, :value_end].include? state - yield key.string.strip, value.string.rstrip - else - raise "DN badly formed" - end + raise "DN badly formed" unless + [:value, :value_normal, :value_hexstring, :value_end].include? state + + yield key.string.strip, value.string.rstrip end ## diff --git a/lib/net/ldap/entry.rb b/lib/net/ldap/entry.rb index d5068dde..10965c7c 100644 --- a/lib/net/ldap/entry.rb +++ b/lib/net/ldap/entry.rb @@ -140,11 +140,10 @@ def attribute_names # arguments to the block: a Symbol giving the name of the attribute, and a # (possibly empty) \Array of data values. def each # :yields: attribute-name, data-values-array - if block_given? - attribute_names.each do|a| - attr_name, values = a, self[a] - yield attr_name, values - end + return unless block_given? + attribute_names.each do|a| + attr_name, values = a, self[a] + yield attr_name, values end end alias_method :each_attribute, :each diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index ba6289b3..8489c377 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -16,9 +16,7 @@ def capture_stderr class FakeTCPSocket def initialize(host, port, socket_opts = {}) status, error = host.split(".") - if status == "fail" - raise Object.const_get(error) - end + raise Object.const_get(error) if status == "fail" end end