Skip to content

Commit 144d956

Browse files
committed
(PUP-11993) Style/UnlessElse
This commit enables the Style/UnlessElse cop and fixes 13 autocorrectable offenses.
1 parent 40332a3 commit 144d956

File tree

9 files changed

+36
-48
lines changed

9 files changed

+36
-48
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -762,18 +762,6 @@ Style/TrailingUnderscoreVariable:
762762
Style/TrivialAccessors:
763763
Enabled: false
764764

765-
# This cop supports safe auto-correction (--auto-correct).
766-
Style/UnlessElse:
767-
Exclude:
768-
- 'lib/puppet/application/lookup.rb'
769-
- 'lib/puppet/pops/evaluator/evaluator_impl.rb'
770-
- 'lib/puppet/pops/types/type_mismatch_describer.rb'
771-
- 'lib/puppet/provider/package/aix.rb'
772-
- 'lib/puppet/provider/package/yum.rb'
773-
- 'lib/puppet/ssl/openssl_loader.rb'
774-
- 'lib/puppet/type/exec.rb'
775-
- 'lib/puppet/util/command_line/trollop.rb'
776-
777765
# This cop supports safe auto-correction (--auto-correct).
778766
Style/UnpackFirst:
779767
Exclude:

lib/puppet/application/lookup.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ def generate_scope
369369
end
370370
end
371371

372-
unless node.is_a?(Puppet::Node) # to allow unit tests to pass a node instance
372+
if node.is_a?(Puppet::Node)
373+
node.add_extra_facts(given_facts) if given_facts
374+
else # to allow unit tests to pass a node instance
373375
facts = retrieve_node_facts(node, given_facts)
374376
ni = Puppet::Node.indirection
375377
tc = ni.terminus_class
@@ -399,8 +401,6 @@ def generate_scope
399401
node = ni.find(node, facts: facts, environment: Puppet[:environment])
400402
ni.terminus_class = tc
401403
end
402-
else
403-
node.add_extra_facts(given_facts) if given_facts
404404
end
405405
node.environment = Puppet[:environment] if Puppet.settings.set_by_cli?(:environment)
406406
Puppet[:code] = 'undef' unless options[:compile]

lib/puppet/pops/evaluator/evaluator_impl.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,10 +1064,10 @@ def eval_IfExpression o, scope
10641064
# Evaluates Puppet DSL `unless`
10651065
def eval_UnlessExpression o, scope
10661066
scope.with_guarded_scope do
1067-
unless is_true?(evaluate(o.test, scope), o.test)
1068-
evaluate(o.then_expr, scope)
1069-
else
1067+
if is_true?(evaluate(o.test, scope), o.test)
10701068
evaluate(o.else_expr, scope)
1069+
else
1070+
evaluate(o.then_expr, scope)
10711071
end
10721072
end
10731073
end

lib/puppet/pops/types/type_mismatch_describer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,9 +954,7 @@ def describe_PCallableType(expected, original, actual, path)
954954
if param_errors.empty?
955955
this_return_t = expected.return_type || PAnyType::DEFAULT
956956
that_return_t = actual.return_type || PAnyType::DEFAULT
957-
unless this_return_t.assignable?(that_return_t)
958-
[TypeMismatch.new(path + [ReturnTypeElement.new], this_return_t, that_return_t)]
959-
else
957+
if this_return_t.assignable?(that_return_t)
960958
# names are ignored, they are just information
961959
# Blocks must be compatible
962960
this_block_t = expected.block_type || PUndefType::DEFAULT
@@ -966,6 +964,8 @@ def describe_PCallableType(expected, original, actual, path)
966964
else
967965
[TypeMismatch.new(path + [BlockPathElement.new], this_block_t, that_block_t)]
968966
end
967+
else
968+
[TypeMismatch.new(path + [ReturnTypeElement.new], this_return_t, that_return_t)]
969969
end
970970
else
971971
param_errors

lib/puppet/provider/package/aix.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ def self.instances
152152
def latest
153153
upd = latest_info
154154

155-
unless upd.nil?
156-
(upd[:version]).to_s
157-
else
155+
if upd.nil?
158156
raise Puppet::DevError, _("Tried to get latest on a missing package") if properties[:ensure] == :absent
159157

160158
properties[:ensure]
159+
else
160+
(upd[:version]).to_s
161161
end
162162
end
163163

lib/puppet/provider/package/yum.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,17 @@ def install
333333
# What's the latest package version available?
334334
def latest
335335
upd = self.class.latest_package_version(@resource[:name], disablerepo, enablerepo, disableexcludes)
336-
unless upd.nil?
337-
# FIXME: there could be more than one update for a package
338-
# because of multiarch
339-
"#{upd[:epoch]}:#{upd[:version]}-#{upd[:release]}"
340-
else
336+
if upd.nil?
341337
# Yum didn't find updates, pretend the current version is the latest
342338
debug "Yum didn't find updates, current version (#{properties[:ensure]}) is the latest"
343339
version = properties[:ensure]
344340
raise Puppet::DevError, _("Tried to get latest on a missing package") if version == :absent || version == :purged
345341

346342
version
343+
else
344+
# FIXME: there could be more than one update for a package
345+
# because of multiarch
346+
"#{upd[:epoch]}:#{upd[:version]}-#{upd[:release]}"
347347
end
348348
end
349349

lib/puppet/ssl/openssl_loader.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
# core Puppet code to load correctly in JRuby environments that do not
88
# have a functioning openssl (eg a FIPS enabled one).
99

10-
unless Puppet::Util::Platform.jruby_fips?
11-
require 'openssl'
12-
require 'net/https'
13-
else
10+
if Puppet::Util::Platform.jruby_fips?
1411
# Even in JRuby we need to define the constants that are wrapped in
1512
# Indirections: Puppet::SSL::{Key, Certificate, CertificateRequest}
1613
module OpenSSL
@@ -23,4 +20,7 @@ class Request; end
2320
class Certificate; end
2421
end
2522
end
23+
else
24+
require 'openssl'
25+
require 'net/https'
2626
end

lib/puppet/type/exec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ def sync
163163
when :true
164164
log = @resource[:loglevel]
165165
when :on_failure
166-
unless should.include?(@status.exitstatus.to_s)
167-
log = @resource[:loglevel]
168-
else
166+
if should.include?(@status.exitstatus.to_s)
169167
log = :false
168+
else
169+
log = @resource[:loglevel]
170170
end
171171
end
172172
unless log == :false

lib/puppet/util/command_line/trollop.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,13 @@ def parse cmdline = ARGV
425425
end
426426

427427
if SINGLE_ARG_TYPES.include?(opts[:type])
428-
unless opts[:multi] # single parameter
429-
vals[sym] = vals[sym][0][0]
430-
else # multiple options, each with a single parameter
428+
if opts[:multi] # multiple options, each with a single parameter
431429
vals[sym] = vals[sym].map { |p| p[0] }
430+
else # single parameter
431+
vals[sym] = vals[sym][0][0]
432432
end
433433
elsif MULTI_ARG_TYPES.include?(opts[:type]) && !opts[:multi]
434-
vals[sym] = vals[sym][0] # single option, with multiple parameters
434+
vals[sym] = vals[sym][0] # single option, with multiple parameters
435435
end
436436
# else: multiple options, with multiple parameters
437437

@@ -584,7 +584,10 @@ def each_arg args
584584
i += 1
585585
when /^--(\S+)$/ # long argument
586586
params = collect_argument_parameters(args, i + 1)
587-
unless params.empty?
587+
if params.empty? # long argument no parameter
588+
yield args[i], nil
589+
i += 1
590+
else
588591
num_params_taken = yield args[i], params
589592
unless num_params_taken
590593
if @stop_on_unknown
@@ -595,16 +598,16 @@ def each_arg args
595598
end
596599
end
597600
i += 1 + num_params_taken
598-
else # long argument no parameter
599-
yield args[i], nil
600-
i += 1
601601
end
602602
when /^-(\S+)$/ # one or more short arguments
603603
shortargs = ::Regexp.last_match(1).split(//)
604604
shortargs.each_with_index do |a, j|
605605
if j == (shortargs.length - 1)
606606
params = collect_argument_parameters(args, i + 1)
607-
unless params.empty?
607+
if params.empty? # argument no parameter
608+
yield "-#{a}", nil
609+
i += 1
610+
else
608611
num_params_taken = yield "-#{a}", params
609612
unless num_params_taken
610613
if @stop_on_unknown
@@ -615,9 +618,6 @@ def each_arg args
615618
end
616619
end
617620
i += 1 + num_params_taken
618-
else # argument no parameter
619-
yield "-#{a}", nil
620-
i += 1
621621
end
622622
else
623623
yield "-#{a}", nil

0 commit comments

Comments
 (0)