Class: RuboCop::Cop::Chef::Correctness::LazyInResourceGuard

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/correctness/lazy_in_resource_guard.rb

Overview

Using ‘lazy {}` within a resource guard (not_if/only_if) will cause failures and is unnecessary as resource guards are always lazily evaluated.

Examples:


### incorrect
template '/etc/foo' do
  mode '0644'
  source 'foo.erb'
  only_if { lazy { ::File.exist?('/etc/foo')} }
end

### correct
template '/etc/foo' do
  mode '0644'
  source 'foo.erb'
  only_if { ::File.exist?('/etc/foo') }
end

Constant Summary collapse

MSG =
'Using `lazy {}` within a resource guard (not_if/only_if) will cause failures and is unnecessary as resource guards are always lazily evaluated.'

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/rubocop/cop/chef/correctness/lazy_in_resource_guard.rb', line 56

def on_block(node)
  lazy_in_guard?(node) do |type, code|
    add_offense(node, severity: :refactor) do |corrector|
      corrector.replace(node, "#{type} { #{code.source} }")
    end
  end
end