-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from koic/add_new_performance_concurrent_mono…
…tonic_time_cop Add new `Performance/ConcurrentMonotonicTime` cop
- Loading branch information
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/new_add_new_performance_concurrent_monotonic_time_cop.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#267](https://github.com/rubocop/rubocop-performance/pull/267): Add new `Performance/ConcurrentMonotonicTime` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop identifies places where `Concurrent.monotonic_time` | ||
# can be replaced by `Process.clock_gettime(Process::CLOCK_MONOTONIC)`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# Concurrent.monotonic_time | ||
# | ||
# # good | ||
# Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
# | ||
class ConcurrentMonotonicTime < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `%<prefer>s` instead of `%<current>s`.' | ||
RESTRICT_ON_SEND = %i[monotonic_time].freeze | ||
|
||
def_node_matcher :concurrent_monotonic_time?, <<~PATTERN | ||
(send | ||
(const {nil? cbase} :Concurrent) :monotonic_time ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless concurrent_monotonic_time?(node) | ||
|
||
optional_unit_parameter = ", #{node.first_argument.source}" if node.first_argument | ||
prefer = "Process.clock_gettime(Process::CLOCK_MONOTONIC#{optional_unit_parameter})" | ||
message = format(MSG, prefer: prefer, current: node.source) | ||
|
||
add_offense(node, message: message) do |corrector| | ||
corrector.replace(node, prefer) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
spec/rubocop/cop/performance/concurrent_monotonic_time_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::ConcurrentMonotonicTime, :config do | ||
it 'registers an offense when using `Concurrent.monotonic_time`' do | ||
expect_offense(<<~RUBY) | ||
Concurrent.monotonic_time | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Process.clock_gettime(Process::CLOCK_MONOTONIC)` instead of `Concurrent.monotonic_time`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `::Concurrent.monotonic_time`' do | ||
expect_offense(<<~RUBY) | ||
::Concurrent.monotonic_time | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Process.clock_gettime(Process::CLOCK_MONOTONIC)` instead of `::Concurrent.monotonic_time`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `Concurrent.monotonic_time` with an optional unit parameter' do | ||
expect_offense(<<~RUBY) | ||
Concurrent.monotonic_time(:float_second) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)` instead of `Concurrent.monotonic_time(:float_second)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `Process.clock_gettime(Process::CLOCK_MONOTONIC)`' do | ||
expect_no_offenses(<<~RUBY) | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
RUBY | ||
end | ||
end |