Adds the Rubocop ReturnNil cop

This style change enforces `return if ...` instead of
`return nil if ...` to save maintainers a few minor review points
This commit is contained in:
Andrew Newdigate 2019-02-08 14:19:53 +02:00
parent 7bbdb2a29f
commit 3288e1a874
71 changed files with 95 additions and 92 deletions

View File

@ -181,3 +181,6 @@ Cop/InjectEnterpriseEditionModule:
Exclude:
- 'spec/**/*'
- 'ee/spec/**/*'
Style/ReturnNil:
Enabled: true

View File

@ -6,7 +6,7 @@ module ContinueParams
def continue_params
continue_params = params[:continue]
return nil unless continue_params
return unless continue_params
continue_params = continue_params.permit(:to, :notice, :notice_now)
continue_params[:to] = safe_redirect_path(continue_params[:to])

View File

@ -16,7 +16,7 @@ module RendersNotes
private
def preload_max_access_for_authors(notes, project)
return nil unless project
return unless project
user_ids = notes.map(&:author_id)
project.team.max_member_access_for_user_ids(user_ids)

View File

@ -17,7 +17,7 @@ class Projects::ApplicationController < ApplicationController
def project
return @project if @project
return nil unless params[:project_id] || params[:id]
return unless params[:project_id] || params[:id]
path = File.join(params[:namespace_id], params[:project_id] || params[:id])
auth_proc = ->(project) { !project.pending_delete? }

View File

@ -46,8 +46,8 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic
# rubocop: disable CodeReuse/ActiveRecord
def commit
return nil unless commit_id = params[:commit_id].presence
return nil unless @merge_request.all_commits.exists?(sha: commit_id)
return unless commit_id = params[:commit_id].presence
return unless @merge_request.all_commits.exists?(sha: commit_id)
@commit ||= @project.commit(commit_id)
end

View File

@ -28,13 +28,13 @@ class UploadsController < ApplicationController
end
def find_model
return nil unless params[:id]
return unless params[:id]
upload_model_class.find(params[:id])
end
def authorize_access!
return nil unless model
return unless model
authorized =
case model
@ -54,7 +54,7 @@ class UploadsController < ApplicationController
end
def authorize_create_access!
return nil unless model
return unless model
# for now we support only personal snippets comments
authorized = can?(current_user, :comment_personal_snippet, model)

View File

@ -74,7 +74,7 @@ module MarkupHelper
# the tag contents are truncated without removing the closing tag.
def first_line_in_markdown(object, attribute, max_chars = nil, options = {})
md = markdown_field(object, attribute, options)
return nil unless md.present?
return unless md.present?
tags = %w(a gl-emoji b pre code p span)
tags << 'img' if options[:allow_images]

View File

@ -29,7 +29,7 @@ module MergeRequestsHelper
def ci_build_details_path(merge_request)
build_url = merge_request.source_project.ci_service.build_page(merge_request.diff_head_sha, merge_request.source_branch)
return nil unless build_url
return unless build_url
parsed_url = URI.parse(build_url)
@ -92,7 +92,7 @@ module MergeRequestsHelper
end
def version_index(merge_request_diff)
return nil if @merge_request_diffs.empty?
return if @merge_request_diffs.empty?
@merge_request_diffs.size - @merge_request_diffs.index(merge_request_diff)
end
@ -149,7 +149,7 @@ module MergeRequestsHelper
def merge_request_source_project_for_project(project = @project)
unless can?(current_user, :create_merge_request_in, project)
return nil
return
end
if can?(current_user, :create_merge_request_from, project)

View File

@ -122,7 +122,7 @@ module NotesHelper
end
def new_form_url
return nil unless @snippet.is_a?(PersonalSnippet)
return unless @snippet.is_a?(PersonalSnippet)
snippet_notes_path(@snippet)
end

View File

@ -735,7 +735,7 @@ module Ci
# Virtual deployment status depending on the environment status.
def deployment_status
return nil unless starts_environment?
return unless starts_environment?
if success?
return successful_deployment_status

View File

@ -134,25 +134,25 @@ class CommitRange
end
def sha_from
return nil unless @commit_from
return unless @commit_from
@commit_from.id
end
def sha_to
return nil unless @commit_to
return unless @commit_to
@commit_to.id
end
def sha_start
return nil unless sha_from
return unless sha_from
exclude_start? ? sha_from + '^' : sha_from
end
def commit_start
return nil unless sha_start
return unless sha_start
if exclude_start?
@commit_start ||= project.commit(sha_start)

View File

@ -5,7 +5,7 @@ module BlobLanguageFromGitAttributes
extend ActiveSupport::Concern
def language_from_gitattributes
return nil unless project
return unless project
repository = project.repository
repository.gitattribute(path, 'gitlab-language')

View File

@ -2,7 +2,7 @@
module FeatureGate
def flipper_id
return nil if new_record?
return if new_record?
"#{self.class.name}:#{id}"
end

View File

@ -79,7 +79,7 @@ module MirrorAuthentication
end
def ssh_public_key
return nil if ssh_private_key.blank?
return if ssh_private_key.blank?
comment = "git@#{::Gitlab.config.gitlab.host}"
::SSHKey.new(ssh_private_key, comment: comment).ssh_public_key

View File

@ -69,7 +69,7 @@ module ReactiveCaching
def with_reactive_cache(*args, &blk)
unless within_reactive_cache_lifetime?(*args)
refresh_reactive_cache!(*args)
return nil
return
end
keep_alive_reactive_cache!(*args)

View File

@ -119,7 +119,7 @@ class Environment < ActiveRecord::Base
def first_deployment_for(commit_sha)
ref = project.repository.ref_name_for_sha(ref_path, commit_sha)
return nil unless ref
return unless ref
deployment_iid = ref.split('/').last
deployments.find_by(iid: deployment_iid)
@ -130,7 +130,7 @@ class Environment < ActiveRecord::Base
end
def formatted_external_url
return nil unless external_url
return unless external_url
external_url.gsub(%r{\A.*?://}, '')
end

View File

@ -81,7 +81,7 @@ class LabelNote < Note
deleted = label_refs.count - existing_refs.count
deleted_str = deleted == 0 ? nil : "#{deleted} deleted"
return nil unless refs_str || deleted_str
return unless refs_str || deleted_str
label_list_str = [refs_str, deleted_str].compact.join(' + ')
suffix = 'label'.pluralize(deleted > 0 ? deleted : existing_refs.count)

View File

@ -73,7 +73,7 @@ class LegacyDiffNote < Note
private
def find_diff
return nil unless noteable
return unless noteable
return @diff if defined?(@diff)
@diff = noteable.raw_diffs(Commit.max_diff_options).find do |d|

View File

@ -149,7 +149,7 @@ class Namespace < ApplicationRecord
end
def find_fork_of(project)
return nil unless project.fork_network
return unless project.fork_network
if Gitlab::SafeRequestStore.active?
forks_in_namespace = Gitlab::SafeRequestStore.fetch("namespaces:#{id}:forked_projects") do

View File

@ -119,7 +119,7 @@ class NotificationRecipient
private
def read_ability
return nil if @skip_read_ability
return if @skip_read_ability
return @read_ability if instance_variable_defined?(:@read_ability)
@read_ability =
@ -136,7 +136,7 @@ class NotificationRecipient
end
def default_project
return nil if @target.nil?
return if @target.nil?
return @target if @target.is_a?(Project)
return @target.project if @target.respond_to?(:project)
end

View File

@ -1230,7 +1230,7 @@ class Project < ActiveRecord::Base
end
def fork_source
return nil unless forked?
return unless forked?
forked_from_project || fork_network&.root_project
end
@ -1679,7 +1679,7 @@ class Project < ActiveRecord::Base
end
def export_path
return nil unless namespace.present? || hashed_storage?(:repository)
return unless namespace.present? || hashed_storage?(:repository)
import_export_shared.archive_path
end

View File

@ -57,7 +57,7 @@ class CampfireService < Service
# https://github.com/basecamp/campfire-api/blob/master/sections/messages.md#create-message
def speak(room_name, message, auth)
room = rooms(auth).find { |r| r["name"] == room_name }
return nil unless room
return unless room
path = "/room/#{room["id"]}/speak.json"
body = {

View File

@ -112,7 +112,7 @@ class IrkerService < Service
end
def consider_uri(uri)
return nil if uri.scheme.nil?
return if uri.scheme.nil?
# Authorize both irc://domain.com/#chan and irc://domain.com/chan
if uri.is_a?(URI) && uri.scheme[/^ircs?\z/] && !uri.path.nil?

View File

@ -79,7 +79,7 @@ class Repository
end
def raw_repository
return nil unless full_path
return unless full_path
@raw_repository ||= initialize_raw_repository
end
@ -103,7 +103,7 @@ class Repository
end
def commit(ref = nil)
return nil unless exists?
return unless exists?
return ref if ref.is_a?(::Commit)
find_commit(ref || root_ref)

View File

@ -27,7 +27,7 @@ class SshHostKey
def self.find_by(opts = {})
opts = HashWithIndifferentAccess.new(opts)
return nil unless opts.key?(:id)
return unless opts.key?(:id)
project_id, url = opts[:id].split(':', 2)
project = Project.find_by(id: project_id)

View File

@ -470,7 +470,7 @@ class User < ApplicationRecord
end
def by_login(login)
return nil unless login
return unless login
if login.include?('@'.freeze)
unscoped.iwhere(email: login).take

View File

@ -132,7 +132,7 @@ class WikiPage
# The GitLab Commit instance for this page.
def version
return nil unless persisted?
return unless persisted?
@version ||= @page.version
end

View File

@ -11,7 +11,7 @@ module UserStatusTooltip
expose :user_status_if_loaded, as: :status_tooltip_html
def user_status_if_loaded
return nil unless object.association(:status).loaded?
return unless object.association(:status).loaded?
user_status(object)
end

View File

@ -38,7 +38,7 @@ module ApplicationSettings
def performance_bar_allowed_group_id
performance_bar_enabled = !params.key?(:performance_bar_enabled) || params.delete(:performance_bar_enabled)
group_full_path = params.delete(:performance_bar_allowed_group_path)
return nil unless Gitlab::Utils.to_boolean(performance_bar_enabled)
return unless Gitlab::Utils.to_boolean(performance_bar_enabled)
Group.find_by_full_path(group_full_path)&.id if group_full_path.present?
end

View File

@ -4,7 +4,7 @@ module Boards
module Visits
class LatestService < Boards::BaseService
def execute
return nil unless current_user
return unless current_user
recent_visit_model.latest(current_user, parent, count: params[:count])
end

View File

@ -12,7 +12,7 @@ module Groups
end
def execute
return nil unless group_path
return unless group_path
if namespace = namespace_or_group(group_path)
return namespace

View File

@ -11,7 +11,7 @@ module Projects
end
def execute
return nil unless valid_url?(@url)
return unless valid_url?(@url)
uploader = FileUploader.new(@project)
uploader.download!(@url)

View File

@ -46,7 +46,7 @@ class PushEventPayloadService
def commit_title
commit = @push_data.fetch(:commits).last
return nil unless commit && commit[:message]
return unless commit && commit[:message]
raw_msg = commit[:message]

View File

@ -6,7 +6,7 @@ class UploadService
end
def execute
return nil unless @file && @file.size <= max_attachment_size
return unless @file && @file.size <= max_attachment_size
uploader = @uploader_class.new(@model, nil, @uploader_context)
uploader.store!(@file)

View File

@ -149,7 +149,7 @@ module API
def conditions(pagination)
fields = pagination.fields
return nil if fields.empty?
return if fields.empty?
placeholder = fields.map { '?' }

View File

@ -25,13 +25,13 @@ module BitbucketServer
end
def prev_page
return nil unless current_page > 1
return unless current_page > 1
current_page - 1
end
def next_page
return nil unless has_next_page?
return unless has_next_page?
current_page + 1
end

View File

@ -84,7 +84,7 @@ module DeclarativePolicy
# returns nil unless it's already cached
def cached_pass?(context)
condition = context.condition(@name)
return nil unless condition.cached?
return unless condition.cached?
condition.pass?
end
@ -124,7 +124,7 @@ module DeclarativePolicy
def cached_pass?(context)
condition = delegated_context(context).condition(@name)
return nil unless condition.cached?
return unless condition.cached?
condition.pass?
rescue MissingDelegate
@ -161,7 +161,7 @@ module DeclarativePolicy
def cached_pass?(context)
runner = context.runner(@ability)
return nil unless runner.cached?
return unless runner.cached?
runner.pass?
end

View File

@ -195,7 +195,7 @@ module Gitlab
def encryption_options
method = translate_method
return nil unless method
return unless method
{
method: method,
@ -211,7 +211,7 @@ module Gitlab
return @tls_options if defined?(@tls_options)
method = translate_method
return nil unless method
return unless method
opts = if options['verify_certificates'] && method != 'plain'
# Dup so we don't accidentally overwrite the constant

View File

@ -112,7 +112,7 @@ module Gitlab
attributes = Array(config.attributes[attribute.to_s])
selected_attr = attributes.find { |attr| entry.respond_to?(attr) }
return nil unless selected_attr
return unless selected_attr
entry.public_send(selected_attr) # rubocop:disable GitlabSecurity/PublicSend
end

View File

@ -10,11 +10,11 @@ module Gitlab
def authn_context
response_object = auth_hash.extra[:response_object]
return nil if response_object.blank?
return if response_object.blank?
document = response_object.decrypted_document
document ||= response_object.document
return nil if document.blank?
return if document.blank?
extract_authn_context(document)
end

View File

@ -24,7 +24,7 @@ module Gitlab
def commit_title
commit = commits.last
return nil unless commit && commit[:message]
return unless commit && commit[:message]
index = commit[:message].index("\n")
message = index ? commit[:message][0..index] : commit[:message]

View File

@ -127,7 +127,7 @@ module Gitlab
full_path = matchd[1]
project = Gitlab::BackgroundMigration::PopulateUntrackedUploadsDependencies::Project.find_by_full_path(full_path)
return nil unless project
return unless project
project.id
end

View File

@ -108,7 +108,7 @@ module Gitlab
end
def find_or_create_groups
return nil unless group_path.present?
return unless group_path.present?
log " * Using namespace: #{group_path}"

View File

@ -47,7 +47,7 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
def find_user_id(username)
return nil unless username
return unless username
return users[username] if users.key?(username)

View File

@ -65,7 +65,7 @@ module Gitlab
end
def find_user_id(email)
return nil unless email
return unless email
return users[email] if users.key?(email)

View File

@ -313,7 +313,7 @@ module Gitlab
def get_term_color_class(color_index, prefix)
color_name = COLOR[color_index]
return nil if color_name.nil?
return if color_name.nil?
get_color_class(["term", prefix, color_name])
end

View File

@ -103,7 +103,7 @@ module Gitlab
def read_string(gz)
string_size = read_uint32(gz)
return nil unless string_size
return unless string_size
gz.read(string_size)
end

View File

@ -44,7 +44,7 @@ module Gitlab
end
def parent
return nil unless has_parent?
return unless has_parent?
self.class.new(@path.to_s.chomp(basename), @entries)
end

View File

@ -40,11 +40,11 @@ module Gitlab
end
def first_time_reference_commit(event)
return nil unless event && merge_request_diff_commits
return unless event && merge_request_diff_commits
commits = merge_request_diff_commits[event['id'].to_i]
return nil if commits.blank?
return if commits.blank?
commits.find do |commit|
next unless commit[:committed_date] && event['first_mentioned_in_commit_at']

View File

@ -36,7 +36,7 @@ module Gitlab
def perform_count(model, estimate)
# If we estimate 0, we may not have statistics at all. Don't use them.
return nil unless estimate && estimate > 0
return unless estimate && estimate > 0
if estimate < EXACT_COUNT_THRESHOLD
# The table is considered small, the assumption here is that

View File

@ -75,7 +75,7 @@ module Gitlab
end
def line_for_position(pos)
return nil unless pos.position_type == 'text'
return unless pos.position_type == 'text'
# This method is normally used to find which line the diff was
# commented on, and in this context, it's normally the raw diff persisted

View File

@ -61,7 +61,7 @@ module Gitlab
# Force encoding to UTF-8 on a Mail::Message or Mail::Part
def fix_charset(object)
return nil if object.nil?
return if object.nil?
if object.charset
object.body.decoded.force_encoding(object.charset.gsub(/utf8/i, "UTF-8")).encode("UTF-8").to_s

View File

@ -239,7 +239,7 @@ module Gitlab
res = ::Projects::DownloadService.new(project, link).execute
return nil if res.nil?
return if res.nil?
res[:markdown]
end

View File

@ -58,10 +58,10 @@ module Gitlab
return commit_id if commit_id.is_a?(Gitlab::Git::Commit)
# Some weird thing?
return nil unless commit_id.is_a?(String)
return unless commit_id.is_a?(String)
# This saves us an RPC round trip.
return nil if commit_id.include?(':')
return if commit_id.include?(':')
commit = find_commit(repo, commit_id)

View File

@ -276,7 +276,7 @@ module Gitlab
# senddata response.
def archive_file_path(storage_path, sha, name, format = "tar.gz")
# Build file path
return nil unless name
return unless name
extension =
case format

View File

@ -44,7 +44,7 @@ module Gitlab
entry[:name] == path_arr[0] && entry[:type] == :tree
end
return nil unless entry
return unless entry
if path_arr.size > 1
path_arr.shift

View File

@ -384,13 +384,13 @@ module Gitlab
# Returns the stacks that calls Gitaly the most times. Used for n+1 detection
def self.max_stacks
return nil unless Gitlab::SafeRequestStore.active?
return unless Gitlab::SafeRequestStore.active?
stack_counter = Gitlab::SafeRequestStore[:stack_counter]
return nil unless stack_counter
return unless stack_counter
max = max_call_count
return nil if max.zero?
return if max.zero?
stack_counter.select { |_, v| v == max }.keys
end

View File

@ -27,7 +27,7 @@ module Gitlab
data << msg.data
end
return nil if blob.oid.blank?
return if blob.oid.blank?
data = data.join

View File

@ -62,7 +62,7 @@ module Gitlab
end
branch = response.branch
return nil unless branch
return unless branch
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
Gitlab::Git::Branch.new(@repository, branch.name, target_commit.id, target_commit)

View File

@ -39,7 +39,7 @@ module Gitlab
private
def custom_language
return nil unless @language
return unless @language
Rouge::Lexer.find_fancy(@language)
end

View File

@ -15,7 +15,7 @@ module Gitlab
end
def expected_forms
return nil unless plural_information
return unless plural_information
plural_information['nplurals'].to_i
end

View File

@ -67,7 +67,7 @@ module Gitlab
# +value+ existing model to be included in the hash
# +parsed_hash+ the original hash
def parse_hash(value)
return nil if already_contains_methods?(value)
return if already_contains_methods?(value)
@attributes_finder.parse(value) do |hash|
{ include: hash_or_merge(value, hash) }

View File

@ -75,7 +75,7 @@ module Gitlab
# the relation_hash, updating references with new object IDs, mapping users using
# the "members_mapper" object, also updating notes if required.
def create
return nil if unknown_service?
return if unknown_service?
setup_models

View File

@ -57,7 +57,7 @@ module Gitlab
def address_regex
wildcard_address = config.address
return nil unless wildcard_address
return unless wildcard_address
regex = Regexp.escape(wildcard_address)
regex = regex.sub(Regexp.escape(WILDCARD_PLACEHOLDER), '(.+)')

View File

@ -68,7 +68,7 @@ module Gitlab
end
def user(login)
return nil unless login.present?
return unless login.present?
return @users[login] if @users.key?(login)
@users[login] = api.user(login)

View File

@ -25,7 +25,7 @@ module Gitlab
end
def find_by_email
return nil unless email
return unless email
User.find_by_any_email(email)
.try(:id)
@ -33,7 +33,7 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
def find_by_external_uid
return nil unless id
return unless id
identities = ::Identity.arel_table

View File

@ -33,7 +33,7 @@ module Gitlab
# `LOWER(column) = query` instead of using `ILIKE`.
def fuzzy_arel_match(column, query, lower_exact_match: false)
query = query.squish
return nil unless query.present?
return unless query.present?
words = select_fuzzy_words(query)

View File

@ -60,7 +60,7 @@ module Gitlab
elsif udp_endpoint.present?
sender = get_udp_sender(encoder, udp_endpoint)
else
return nil
return
end
Jaeger::Reporters::RemoteReporter.new(

View File

@ -95,7 +95,7 @@ module Gitlab
end
def cache_commit(commit)
return nil unless commit.present?
return unless commit.present?
resolved_commits[commit.id] ||= commit
end

View File

@ -53,7 +53,7 @@ module SystemCheck
end
def ssh_dir
return nil unless home_dir
return unless home_dir
File.join(home_dir, '.ssh')
end

View File

@ -91,7 +91,7 @@ module ExportFileHelper
loop do
object_with_parent = deep_find_with_parent(sensitive_word, project_hash)
return nil unless object_with_parent && object_with_parent.object
return unless object_with_parent && object_with_parent.object
if is_safe_hash?(object_with_parent.parent, sensitive_word)
# It's in the safe list, remove hash and keep looking