Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rubocop pre-commit hook; Remove redundant returns #6207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/linters/.rubocop.yml
@@ -0,0 +1,6 @@
AllCops:
DisabledByDefault: true
TargetRubyVersion: 3.3.0

Style/RedundantReturn:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this rule really needed? I am not sure the changes from the rule make the code more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started this PR thinking that we could use some rules from Rubocop to help organize and standardize some of the Ruby codebase.

I think it is better to be consistent and removing these redundant returns sets a code standard going forwards. These extra returns are really extra characters and data that is not needed.

Maybe this rule is not needed but I think there could be some other Rubocop rules that are useful for mruby.

Enabled: true
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Expand Up @@ -49,6 +49,9 @@ repos:
- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 3.0.0
hooks:
- id: rubocop
args: [--config=.github/linters/.rubocop.yml]
exclude: ^test/t/syntax\.rb$|^test/t/vformat\.rb$
- id: script-must-not-have-extension
name: Local policy is to exclude extension from all shell files
types: [shell]
Expand Down
2 changes: 1 addition & 1 deletion benchmark/bm_so_lists.rb
Expand Up @@ -32,7 +32,7 @@ def test_lists
else
# compare li1 and li2 for equality
if li1 != li2
return(0)
0
else
# return the length of the list
li1.length
Expand Down
2 changes: 1 addition & 1 deletion lib/mruby/build/command.rb
Expand Up @@ -371,7 +371,7 @@ def initialize(build)

def emulator
return "" unless @command
return [@command, *@flags].map{|c| shellquote(c)}.join(' ')
[@command, *@flags].map{|c| shellquote(c)}.join(' ')
end

def run(testbinfile)
Expand Down
28 changes: 14 additions & 14 deletions lib/mruby/build/load_gems.rb
Expand Up @@ -68,19 +68,19 @@ def initialize(gemdir, repo, branch, commit, canonical, path = nil)

def full_gemdir
return @gemdir unless @path
return File.join(@gemdir, @path)
File.join(@gemdir, @path)
end

def canonical?() return @canonical; end
def git?() return !!@repo; end
def gemname() return File.basename(@gemdir); end
def canonical?() @canonical; end
def git?() !!@repo; end
def gemname() File.basename(@gemdir); end

def hash
return [@gemdir, @repo, @branch, @commit, @canonical, @path].hash
[@gemdir, @repo, @branch, @commit, @canonical, @path].hash
end

def ==(other)
return @gemdir == other.gemdir &&
@gemdir == other.gemdir &&
@repo == other.repo &&
@branch == other.branch &&
@commit == other.commit &&
Expand All @@ -93,7 +93,7 @@ def to_s
desc = @gemdir
desc += " -> #{@repo}/#{@branch}" if git?
desc += "/#{commit}" if commit
return desc
desc
end
end

Expand Down Expand Up @@ -206,11 +206,11 @@ def fromGemdir!
gem_src = File.expand_path(gem_src, root_dir)
end

return GemCheckout.new(gem_src, nil, nil, nil, @canonical)
GemCheckout.new(gem_src, nil, nil, nil, @canonical)
end

def fromCore!
return GemCheckout.new("#{@build.root}/mrbgems/#{@core}", nil, nil,
GemCheckout.new("#{@build.root}/mrbgems/#{@core}", nil, nil,
nil, @canonical)
end

Expand All @@ -221,7 +221,7 @@ def fromCore!

def fromGitHub!
url = "https://github.com/#{@github}.git"
return fromGit!(url, @branch)
fromGit!(url, @branch)
end

def fromBitBucket!
Expand All @@ -231,7 +231,7 @@ def fromBitBucket!
url = "https://bitbucket.org/#{@bitbucket}.git"
end

return fromGit!(url, @branch)
fromGit!(url, @branch)
end


Expand All @@ -245,7 +245,7 @@ def fromMGem!
url = mgem['repository']
branch = mgem['branch'] || @branch

return fromGit!(url, branch)
fromGit!(url, branch)
end

# Fetch the contents of the named mgem item. Will clone the
Expand All @@ -265,7 +265,7 @@ def fetchMGem(mgem)
fail "unknown mgem protocol: #{conf['protocol']}" if
conf['protocol'] != 'git'

return conf
conf
end


Expand Down Expand Up @@ -304,7 +304,7 @@ def fromGit!(url, branch)
}
end

return GemCheckout.new(repo_dir, url, branch, commit, @canonical,@path)
GemCheckout.new(repo_dir, url, branch, commit, @canonical,@path)
end


Expand Down
2 changes: 1 addition & 1 deletion lib/mruby/gem.rb
Expand Up @@ -110,7 +110,7 @@ def for_windows?
elsif build.kind_of?(MRuby::Build)
return ('A'..'Z').to_a.any? { |vol| Dir.exist?("#{vol}:") }
end
return false
false
end

def disable_cdump
Expand Down
8 changes: 4 additions & 4 deletions mrbgems/mruby-class-ext/mrblib/module.rb
Expand Up @@ -29,9 +29,9 @@ def <(other)
def <=(other)
raise TypeError, 'compared with non class/module' unless other.is_a?(Module)
if self.ancestors.include?(other)
return true
true
elsif other.ancestors.include?(self)
return false
false
end
end

Expand Down Expand Up @@ -64,7 +64,7 @@ def >(other)
#
def >=(other)
raise TypeError, 'compared with non class/module' unless other.is_a?(Module)
return other < self
other < self
end

##
Expand All @@ -84,6 +84,6 @@ def <=>(other)
cmp = self < other
return -1 if cmp
return 1 unless cmp.nil?
return nil
nil
end
end
4 changes: 2 additions & 2 deletions mrbgems/mruby-compar-ext/mrblib/compar.rb
Expand Up @@ -84,9 +84,9 @@ def clamp(min, max=nil)
if c.nil?
raise ArgumentError, "comparison of #{self.class} with #{max.class} failed"
elsif c > 0
return max
max
else
return self
self
end
end
end
2 changes: 1 addition & 1 deletion mrbgems/mruby-enum-ext/mrblib/enum.rb
Expand Up @@ -219,7 +219,7 @@ def first(*args)
self.each do |*val|
return val.__svalue
end
return nil
nil
when 1
i = args[0].__to_int
raise ArgumentError, "attempt to take negative size" if i < 0
Expand Down
4 changes: 2 additions & 2 deletions mrbgems/mruby-io/mrblib/file.rb
Expand Up @@ -159,7 +159,7 @@ def self.foreach(file)
f.each {|l| yield l}
end
else
return self.new(file)
self.new(file)
end
end

Expand Down Expand Up @@ -207,7 +207,7 @@ def self.extname(filename)
fname = self.basename(filename)
epos = fname.rindex('.')
return '' if epos == 0 || epos.nil?
return fname[epos..-1]
fname[epos..-1]
end

def self.path(filename)
Expand Down
2 changes: 1 addition & 1 deletion mrbgems/mruby-proc-ext/test/proc.rb
Expand Up @@ -10,7 +10,7 @@ def enable_debug_info?
if @enable_debug_info && e.backtrace[0].include?("(unknown)")
@enable_debug_info = false
end
return @enable_debug_info
@enable_debug_info
end
end

Expand Down
2 changes: 1 addition & 1 deletion mrbgems/mruby-range-ext/mrblib/range.rb
Expand Up @@ -50,7 +50,7 @@ def last(*args)
nv = args[0]
n = nv.__to_int
raise ArgumentError, "negative array size (or size too big)" unless 0 <= n
return self.to_a.last(nv)
self.to_a.last(nv)
end

def max(&block)
Expand Down
2 changes: 1 addition & 1 deletion mrbgems/mruby-symbol-ext/mrblib/symbol.rb
Expand Up @@ -56,7 +56,7 @@ def casecmp(other)
def casecmp?(sym)
c = self.casecmp(sym)
return nil if c.nil?
return c == 0
c == 0
end

#
Expand Down
4 changes: 2 additions & 2 deletions mrblib/array.rb
Expand Up @@ -119,7 +119,7 @@ def ==(other)
return false if self[i] != other[i]
i += 1
end
return true
true
end

##
Expand All @@ -139,7 +139,7 @@ def eql?(other)
return false unless self[i].eql?(other[i])
i += 1
end
return true
true
end

##
Expand Down
4 changes: 2 additions & 2 deletions mrblib/hash.rb
Expand Up @@ -29,7 +29,7 @@ def ==(hash)
return false unless hash.key?(k)
return false unless self[k] == hash[k]
end
return true
true
end

##
Expand All @@ -49,7 +49,7 @@ def eql?(hash)
return false unless hash.key?(k)
return false unless self[k].eql?(hash[k])
end
return true
true
end

##
Expand Down
2 changes: 1 addition & 1 deletion test/assert.rb
Expand Up @@ -392,7 +392,7 @@ def _eval_assertion(meth, exp, act_or_msg, msg, block)
else
exp, act, msg = exp, act_or_msg, msg
end
return exp.__send__(meth, act), exp, act, msg
[exp.__send__(meth, act), exp, act, msg]
end

##
Expand Down
8 changes: 4 additions & 4 deletions test/t/proc.rb
Expand Up @@ -110,19 +110,19 @@ def initialize
end
def return_array
@block = Proc.new { self }
return []
[]
end
def return_instance_variable
@block = Proc.new { self }
return @block
@block
end
def return_const_fixnum
@block = Proc.new { self }
return 123
123
end
def return_nil
@block = Proc.new { self }
return nil
nil
end
end

Expand Down