diff options
author | Markus Roberts <Markus@reality.com> | 2010-07-09 18:06:38 -0700 |
---|---|---|
committer | Markus Roberts <Markus@reality.com> | 2010-07-09 18:06:38 -0700 |
commit | a07bbe2e711ee22a40e147c046997c8813ae3cc8 (patch) | |
tree | d4a25fff2328eceaf6b9afe8fcbf4192813f54b2 /lib/puppet/util | |
parent | 07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b (diff) | |
download | puppet-a07bbe2e711ee22a40e147c046997c8813ae3cc8.tar.gz puppet-a07bbe2e711ee22a40e147c046997c8813ae3cc8.tar.xz puppet-a07bbe2e711ee22a40e147c046997c8813ae3cc8.zip |
Code smell: Omit needless checks on defined
* Replaced 53 occurances of
defined\?\((.+?)\) (?:and|&&) \1( |$)
with
\1\2
In code like:
unless defined? @foo and @foo and bar("baz")
"defined? @foo and @foo" can safely be replaced with "@foo":
unless @foo and bar("baz")
Because:
* Both evaluate to false/nil when @foo is not defined
* Both evaluate to @foo when @foo is defined
3 Examples:
The code:
@sync = Sync.new unless defined?(@sync) and @sync
becomes:
@sync = Sync.new unless @sync
The code:
unless defined?(@content) and @content
becomes:
unless @content
The code:
raise(ArgumentError, "Already handling indirection for #{@indirection.name}; cannot also handle #{indirection}") if defined?(@indirection) and @indirection
becomes:
raise(ArgumentError, "Already handling indirection for #{@indirection.name}; cannot also handle #{indirection}") if @indirection
* Replaced 2 occurances of
defined\?\((.+?)\) (?:and|&&) ! *\1.nil\?
with
!\1.nil?
In code like:
while defined? @foo and ! @foo.nil? ...
"defined? @foo and ! @foo.nil?" can safely be replaced with "! @foo.nil?":
while ! @foo.nil? ...
Because:
* Both evaluate to false/nil when @foo is not defined
* Both evaluate to "! @foo.nil?" when @foo is defined
2 Examples:
The code:
!!(defined?(@value) and ! @value.nil?)
becomes:
!!(!@value.nil?)
The code:
self.init unless defined?(@@state) and ! @@state.nil?
becomes:
self.init unless !@@state.nil?
Diffstat (limited to 'lib/puppet/util')
-rw-r--r-- | lib/puppet/util/cacher.rb | 4 | ||||
-rw-r--r-- | lib/puppet/util/docs.rb | 2 | ||||
-rw-r--r-- | lib/puppet/util/inline_docs.rb | 2 | ||||
-rw-r--r-- | lib/puppet/util/ldap/generator.rb | 2 | ||||
-rw-r--r-- | lib/puppet/util/ldap/manager.rb | 2 | ||||
-rw-r--r-- | lib/puppet/util/settings/file_setting.rb | 4 | ||||
-rw-r--r-- | lib/puppet/util/settings/setting.rb | 6 | ||||
-rw-r--r-- | lib/puppet/util/storage.rb | 2 |
8 files changed, 12 insertions, 12 deletions
diff --git a/lib/puppet/util/cacher.rb b/lib/puppet/util/cacher.rb index d229c40ca..8a0acc82f 100644 --- a/lib/puppet/util/cacher.rb +++ b/lib/puppet/util/cacher.rb @@ -58,7 +58,7 @@ module Puppet::Util::Cacher end def attr_ttl(name) - return nil unless defined?(@attr_ttls) and @attr_ttls + return nil unless @attr_ttls @attr_ttls[name] end @@ -122,7 +122,7 @@ module Puppet::Util::Cacher end def value_cache - @value_cache = {} unless defined?(@value_cache) and @value_cache + @value_cache = {} unless @value_cache @value_cache end end diff --git a/lib/puppet/util/docs.rb b/lib/puppet/util/docs.rb index beed4bb0b..d247ec044 100644 --- a/lib/puppet/util/docs.rb +++ b/lib/puppet/util/docs.rb @@ -22,7 +22,7 @@ module Puppet::Util::Docs self.send(m) }.join(" ") - if defined?(@doc) and @doc + if @doc @doc + extra else extra diff --git a/lib/puppet/util/inline_docs.rb b/lib/puppet/util/inline_docs.rb index 695b8e8df..b04b40a22 100644 --- a/lib/puppet/util/inline_docs.rb +++ b/lib/puppet/util/inline_docs.rb @@ -15,7 +15,7 @@ module Puppet::Util::InlineDocs attr_writer :doc def doc - @doc = "" unless defined?(@doc) and @doc + @doc = "" unless @doc @doc end diff --git a/lib/puppet/util/ldap/generator.rb b/lib/puppet/util/ldap/generator.rb index fb4915100..2320c203c 100644 --- a/lib/puppet/util/ldap/generator.rb +++ b/lib/puppet/util/ldap/generator.rb @@ -30,7 +30,7 @@ class Puppet::Util::Ldap::Generator end def source - if defined?(@source) and @source + if @source @source.to_s else nil diff --git a/lib/puppet/util/ldap/manager.rb b/lib/puppet/util/ldap/manager.rb index b1048a1a3..3e3c54562 100644 --- a/lib/puppet/util/ldap/manager.rb +++ b/lib/puppet/util/ldap/manager.rb @@ -46,7 +46,7 @@ class Puppet::Util::Ldap::Manager def connect raise ArgumentError, "You must pass a block to #connect" unless block_given? - unless defined?(@connection) and @connection + unless @connection if Puppet[:ldaptls] ssl = :tls elsif Puppet[:ldapssl] diff --git a/lib/puppet/util/settings/file_setting.rb b/lib/puppet/util/settings/file_setting.rb index 351a1ae81..ee17d7ddc 100644 --- a/lib/puppet/util/settings/file_setting.rb +++ b/lib/puppet/util/settings/file_setting.rb @@ -23,7 +23,7 @@ class Puppet::Util::Settings::FileSetting < Puppet::Util::Settings::Setting end def group - return unless defined?(@group) && @group + return unless @group @settings[:group] end @@ -36,7 +36,7 @@ class Puppet::Util::Settings::FileSetting < Puppet::Util::Settings::Setting end def owner - return unless defined?(@owner) && @owner + return unless @owner return "root" if @owner == "root" or ! use_service_user? @settings[:user] end diff --git a/lib/puppet/util/settings/setting.rb b/lib/puppet/util/settings/setting.rb index ba7e4b526..a14dcf050 100644 --- a/lib/puppet/util/settings/setting.rb +++ b/lib/puppet/util/settings/setting.rb @@ -50,11 +50,11 @@ class Puppet::Util::Settings::Setting end def iscreated? - defined?(@iscreated) && @iscreated + @iscreated end def set? - !!(defined?(@value) and ! @value.nil?) + !!(!@value.nil?) end # short name for the celement @@ -68,7 +68,7 @@ class Puppet::Util::Settings::Setting str = @desc.gsub(/^/, "# ") + "\n" # Add in a statement about the default. - str += "# The default value is '#{@default}'.\n" if defined?(@default) and @default + str += "# The default value is '#{@default}'.\n" if @default # If the value has not been overridden, then print it out commented # and unconverted, so it's clear that that's the default and how it diff --git a/lib/puppet/util/storage.rb b/lib/puppet/util/storage.rb index f821c8fd7..6927092de 100644 --- a/lib/puppet/util/storage.rb +++ b/lib/puppet/util/storage.rb @@ -47,7 +47,7 @@ class Puppet::Util::Storage Puppet.settings.use(:main) unless FileTest.directory?(Puppet[:statedir]) unless File.exists?(Puppet[:statefile]) - self.init unless defined?(@@state) and ! @@state.nil? + self.init unless !@@state.nil? return end unless File.file?(Puppet[:statefile]) |