Class: RuboCop::Cop::Chef::Correctness::EmptyResourceGuard
- Defined in:
- lib/rubocop/cop/chef/correctness/empty_resource_guard.rb
Overview
Resource guards (not_if/only_if) should not be empty strings as empty strings will always evaluate to true. This will cause confusion in your cookbooks as the guard will always pass.
Empty strings in Ruby are “truthy”, which means:
-
‘only_if ”` will ALWAYS execute the resource (guard always passes)
-
‘not_if ”` will NEVER execute the resource (guard always blocks)
This behavior is usually unintended and can lead to resources running when they shouldn’t or never running when they should.
Constant Summary collapse
- MSG =
'Resource guards (not_if/only_if) should not be empty strings as empty strings will always evaluate to true.'- RESTRICT_ON_SEND =
[:not_if, :only_if].freeze
Instance Method Summary collapse
Methods inherited from Base
Instance Method Details
#empty_string?(str) ⇒ Boolean
85 86 87 |
# File 'lib/rubocop/cop/chef/correctness/empty_resource_guard.rb', line 85 def empty_string?(str) str.empty? end |
#on_block(node) ⇒ Object
95 96 97 98 99 |
# File 'lib/rubocop/cop/chef/correctness/empty_resource_guard.rb', line 95 def on_block(node) empty_string_block_guard?(node) do add_offense(node, severity: :refactor) end end |
#on_send(node) ⇒ Object
89 90 91 92 93 |
# File 'lib/rubocop/cop/chef/correctness/empty_resource_guard.rb', line 89 def on_send(node) empty_string_guard?(node) do add_offense(node, severity: :refactor) end end |