Skip to content

Commit fc0d8ae

Browse files
Remove redundant begin/end blocks (#16554)
1 parent d80c252 commit fc0d8ae

File tree

11 files changed

+90
-114
lines changed

11 files changed

+90
-114
lines changed

src/compiler/crystal/codegen/cache_dir.cr

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,10 @@ module Crystal
9595

9696
# Return the first one for which we could create a directory
9797
candidates.each do |candidate|
98-
begin
99-
Dir.mkdir_p(candidate)
100-
return @dir = candidate
101-
rescue File::Error
102-
# Try next one
103-
end
98+
Dir.mkdir_p(candidate)
99+
return @dir = candidate
100+
rescue File::Error
101+
# Try next one
104102
end
105103

106104
msg = String.build do |io|

src/compiler/crystal/command.cr

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -277,35 +277,33 @@ class Crystal::Command
277277
private def execute(output_filename, run_args, compiler, *, error_on_exit = false)
278278
time = @time && !@progress_tracker.stats?
279279
status, elapsed_time = @progress_tracker.stage("Execute") do
280-
begin
281-
elapsed = Time.measure do
282-
Process.run(output_filename, args: run_args, input: Process::Redirect::Inherit, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit) do |process|
283-
{% unless flag?(:wasm32) %}
284-
# Ignore the signal so we don't exit the running process
285-
# (the running process can still handle this signal)
286-
Process.ignore_interrupts!
287-
{% end %}
288-
end
280+
elapsed = Time.measure do
281+
Process.run(output_filename, args: run_args, input: Process::Redirect::Inherit, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit) do |process|
282+
{% unless flag?(:wasm32) %}
283+
# Ignore the signal so we don't exit the running process
284+
# (the running process can still handle this signal)
285+
Process.ignore_interrupts!
286+
{% end %}
289287
end
290-
{$?, elapsed}
291-
ensure
292-
File.delete?(output_filename)
293-
294-
# Delete related PDB generated by MSVC, if any exist
295-
{% if flag?(:msvc) %}
296-
unless compiler.debug.none?
297-
basename = output_filename.rchop(".exe")
298-
File.delete?("#{basename}.pdb")
299-
end
300-
{% end %}
301-
302-
# Delete related dwarf generated by dsymutil, if any exist
303-
{% if flag?(:darwin) %}
304-
unless compiler.debug.none?
305-
File.delete?("#{output_filename}.dwarf")
306-
end
307-
{% end %}
308288
end
289+
{$?, elapsed}
290+
ensure
291+
File.delete?(output_filename)
292+
293+
# Delete related PDB generated by MSVC, if any exist
294+
{% if flag?(:msvc) %}
295+
unless compiler.debug.none?
296+
basename = output_filename.rchop(".exe")
297+
File.delete?("#{basename}.pdb")
298+
end
299+
{% end %}
300+
301+
# Delete related dwarf generated by dsymutil, if any exist
302+
{% if flag?(:darwin) %}
303+
unless compiler.debug.none?
304+
File.delete?("#{output_filename}.dwarf")
305+
end
306+
{% end %}
309307
end
310308

311309
if time

src/compiler/crystal/macros/macros.cr

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,11 @@ class Crystal::Program
197197
# We start with the target filename
198198
required_files = Set{filename}
199199
recorded_requires.map do |recorded_require|
200-
begin
201-
files = @program.find_in_path(recorded_require.filename, recorded_require.relative_to)
202-
required_files.concat(files) if files
203-
rescue Crystal::CrystalPath::NotFoundError
204-
# Maybe the file is gone
205-
next
206-
end
200+
files = @program.find_in_path(recorded_require.filename, recorded_require.relative_to)
201+
required_files.concat(files) if files
202+
rescue Crystal::CrystalPath::NotFoundError
203+
# Maybe the file is gone
204+
next
207205
end
208206

209207
new_requires_with_timestamps = required_files.map do |required_file|

src/compiler/crystal/semantic/semantic_visitor.cr

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,10 @@ abstract class Crystal::SemanticVisitor < Crystal::Visitor
443443
skip_macro_exception = nil
444444

445445
generated_nodes = expand_macro(the_macro, node, mode: mode, visibility: :public, accept: accept) do
446-
begin
447-
@program.expand_macro node, (@scope || current_type), @path_lookup, free_vars, @untyped_def
448-
rescue ex : SkipMacroException
449-
skip_macro_exception = ex
450-
{ex.expanded_before_skip, ex.macro_expansion_pragmas}
451-
end
446+
@program.expand_macro node, (@scope || current_type), @path_lookup, free_vars, @untyped_def
447+
rescue ex : SkipMacroException
448+
skip_macro_exception = ex
449+
{ex.expanded_before_skip, ex.macro_expansion_pragmas}
452450
end
453451

454452
node.expanded = generated_nodes

src/compiler/crystal/semantic/top_level_visitor.cr

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -858,13 +858,11 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
858858

859859
def visit(node : Expressions)
860860
node.expressions.each_with_index do |child, i|
861-
begin
862-
child.accept self
863-
rescue ex : SkipMacroException
864-
@program.macro_expansion_error_hook.try &.call(ex.cause) if ex.is_a? SkipMacroCodeCoverageException
865-
node.expressions.delete_at(i..-1)
866-
break
867-
end
861+
child.accept self
862+
rescue ex : SkipMacroException
863+
@program.macro_expansion_error_hook.try &.call(ex.cause) if ex.is_a? SkipMacroCodeCoverageException
864+
node.expressions.delete_at(i..-1)
865+
break
868866
end
869867
false
870868
end

src/compiler/crystal/syntax/parser.cr

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5406,17 +5406,15 @@ module Crystal
54065406
# Looks ahead next tokens to check whether they indicate type.
54075407
def type_start?(*, consume_newlines)
54085408
peek_ahead do
5409-
begin
5410-
if consume_newlines
5411-
next_token_skip_space_or_newline
5412-
else
5413-
next_token_skip_space
5414-
end
5415-
5416-
type_start?
5417-
rescue
5418-
false
5409+
if consume_newlines
5410+
next_token_skip_space_or_newline
5411+
else
5412+
next_token_skip_space
54195413
end
5414+
5415+
type_start?
5416+
rescue
5417+
false
54205418
end
54215419
end
54225420

src/compiler/crystal/tools/playground/server.cr

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,19 @@ module Crystal::Playground
187187
private def bind_io_as_output(tag, io)
188188
spawn do
189189
loop do
190-
begin
191-
output = String.new(4096) do |buffer|
192-
length = io.read_utf8(Slice.new(buffer, 4096))
193-
{length, 0}
194-
end
195-
break if output.empty?
190+
output = String.new(4096) do |buffer|
191+
length = io.read_utf8(Slice.new(buffer, 4096))
192+
{length, 0}
193+
end
194+
break if output.empty?
196195

197-
send_with_json_builder do |json|
198-
json.field "type", "output"
199-
json.field "tag", tag
200-
json.field "content", output
201-
end
202-
rescue
203-
break
196+
send_with_json_builder do |json|
197+
json.field "type", "output"
198+
json.field "tag", tag
199+
json.field "content", output
204200
end
201+
rescue
202+
break
205203
end
206204
end
207205
end

src/crystal/event_loop/polling/arena.cr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,9 @@ class Crystal::EventLoop::Polling::Arena(T, BLOCK_BYTESIZE)
148148
# *index* is out of bounds.
149149
def free(index : Index, &) : Nil
150150
at?(index) do |entry|
151-
begin
152-
yield entry.value.pointer
153-
ensure
154-
entry.value.free
155-
end
151+
yield entry.value.pointer
152+
ensure
153+
entry.value.free
156154
end
157155
end
158156

src/file_utils.cr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,8 @@ module FileUtils
333333
def mv(srcs : Enumerable(Path | String), dest : Path | String) : Nil
334334
raise ArgumentError.new("No such directory : #{dest}") unless Dir.exists?(dest)
335335
srcs.each do |src|
336-
begin
337-
mv(src, File.join(dest, File.basename(src)))
338-
rescue File::Error
339-
end
336+
mv(src, File.join(dest, File.basename(src)))
337+
rescue File::Error
340338
end
341339
end
342340

@@ -441,10 +439,8 @@ module FileUtils
441439
# ```
442440
def rm_rf(paths : Enumerable(Path | String)) : Nil
443441
paths.each do |path|
444-
begin
445-
rm_r(path)
446-
rescue File::Error
447-
end
442+
rm_r(path)
443+
rescue File::Error
448444
end
449445
end
450446

src/json/any.cr

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,13 @@ struct JSON::Any
299299
inspect_key = {0_u64, :__json_inspect__}
300300

301301
hash.put(inspect_key, nil) do
302-
begin
303-
# here's the root case, where we wrap with JSON::Any(...)
304-
io << "JSON::Any("
305-
@raw.inspect(io)
306-
io << ")"
307-
return
308-
ensure
309-
hash.delete(inspect_key)
310-
end
302+
# here's the root case, where we wrap with JSON::Any(...)
303+
io << "JSON::Any("
304+
@raw.inspect(io)
305+
io << ")"
306+
return
307+
ensure
308+
hash.delete(inspect_key)
311309
end
312310

313311
# This is the non-root case, where we don't wrap with anything.

0 commit comments

Comments
 (0)