Class: RuboCop::Cop::Chef::RedundantCode::OhaiAttributeToString

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

Overview

Many Ohai node attributes are already strings and don’t need to be cast to strings again

Examples:


### incorrect
node['platform'].to_s
node['platform_family'].to_s
node['platform_version'].to_s
node['fqdn'].to_s
node['hostname'].to_s
node['os'].to_s
node['name'].to_s

### correct
node['platform']
node['platform_family']
node['platform_version']
node['fqdn']
node['hostname']
node['os']
node['name']

Constant Summary collapse

MSG =
"This Ohai node attribute is already a string and doesn't need to be converted"
RESTRICT_ON_SEND =
[:to_s].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rubocop/cop/chef/redundant/ohai_attribute_to_string.rb', line 54

def on_send(node)
  attribute_to_s?(node) do |method|
    add_offense(node, severity: :refactor) do |corrector|
      corrector.replace(node, "node['#{method.value}']")
    end
  end
end