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

[Fix #10204] Add new Lint/UnsafeGlobalVariables cop #10211

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions changelog/new_add_new_lintunsafeglobalvariables_cop.md
@@ -0,0 +1 @@
* [#10204](https://github.com/rubocop/rubocop/issues/10204): Add new `Lint/UnsafeGlobalVariables` cop. ([@dmarcoux][])
5 changes: 5 additions & 0 deletions config/default.yml
Expand Up @@ -2247,6 +2247,11 @@ Lint/UnreachableLoop:
# eg. `exactly(2).times`
- !ruby/regexp /(exactly|at_least|at_most)\(\d+\)\.times/

Lint/UnsafeGlobalVariables:
Description: 'Do not use unsafe global variables.'
Enabled: pending
VersionAdded: '<<next>>'

Lint/UnusedBlockArgument:
Description: 'Checks for unused block arguments.'
StyleGuide: '#underscore-unused-vars'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -374,6 +374,7 @@
require_relative 'rubocop/cop/lint/unmodified_reduce_accumulator'
require_relative 'rubocop/cop/lint/unreachable_code'
require_relative 'rubocop/cop/lint/unreachable_loop'
require_relative 'rubocop/cop/lint/unsafe_global_variables'
require_relative 'rubocop/cop/lint/unused_block_argument'
require_relative 'rubocop/cop/lint/unused_method_argument'
require_relative 'rubocop/cop/lint/uri_escape_unescape'
Expand Down
39 changes: 39 additions & 0 deletions lib/rubocop/cop/lint/unsafe_global_variables.rb
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Lint
# This cop looks for uses of unsafe global variables.
#
# @example
#
# # bad
# $!
#
# # bad
# $ERROR_INFO
#
# # bad
# $@
#
# # bad
# $ERROR_POSITION
#
class UnsafeGlobalVariables < Base
MSG = 'Do not use unsafe global variable `%<global_variable>s`.'

# built-in global variables and their English aliases
# https://www.zenspider.com/ruby/quickref.html
UNSAFE_GLOBAL_VARIABLES = %i[$! $ERROR_INFO $@ $ERROR_POSITION].freeze

def on_gvar(node)
global_variable, = *node

return unless UNSAFE_GLOBAL_VARIABLES.include?(global_variable)

add_offense(node.loc.name, message: format(MSG, global_variable: global_variable))
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/rubocop/result_cache.rb
Expand Up @@ -52,7 +52,9 @@ def remove_oldest_files(files, dirs, cache_root, verbose)
rescue Errno::ENOENT
# This can happen if parallel RuboCop invocations try to remove the
# same files. No problem.
# rubocop:disable Lint/UnsafeGlobalVariables
puts $ERROR_INFO if verbose
# rubocop:enable Lint/UnsafeGlobalVariables
end

def remove_files(files, dirs, remove_count)
Expand Down
37 changes: 37 additions & 0 deletions spec/rubocop/cop/lint/unsafe_global_variables_spec.rb
@@ -0,0 +1,37 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::UnsafeGlobalVariables, :config do
it 'registers an offense when using `$!`' do
expect_offense(<<~RUBY)
$!
^^ Do not use unsafe global variable `$!`.
RUBY
end

it 'registers an offense when using `$ERROR_INFO`' do
expect_offense(<<~RUBY)
$ERROR_INFO
^^^^^^^^^^^ Do not use unsafe global variable `$ERROR_INFO`.
RUBY
end

it 'registers an offense when using `$@`' do
expect_offense(<<~RUBY)
$@
^^ Do not use unsafe global variable `$@`.
RUBY
end

it 'registers an offense when using `$ERROR_POSITION`' do
expect_offense(<<~RUBY)
$ERROR_POSITION
^^^^^^^^^^^^^^^ Do not use unsafe global variable `$ERROR_POSITION`.
RUBY
end

it 'does not register an offense when using a safe global variable' do
expect_no_offenses(<<~RUBY)
$0
RUBY
end
end