mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Add Performance/AnyInsteadOfEmpty rule
This commit is contained in:
parent
ea98554191
commit
d71091a40c
2 changed files with 90 additions and 0 deletions
46
spec/ameba/rule/performance/any_instead_of_empty_spec.cr
Normal file
46
spec/ameba/rule/performance/any_instead_of_empty_spec.cr
Normal file
|
@ -0,0 +1,46 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::Rule::Performance
|
||||
subject = AnyInsteadOfEmpty.new
|
||||
|
||||
describe AnyInsteadOfEmpty do
|
||||
it "passes if there is no potential performance improvements" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].any?(&.zero?)
|
||||
[1, 2, 3].any?(String)
|
||||
[1, 2, 3].any?(1..3)
|
||||
[1, 2, 3].any? { |e| e > 1 }
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
end
|
||||
|
||||
it "reports if there is any? call without a block nor argument" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].any?
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
end
|
||||
|
||||
context "macro" do
|
||||
it "reports in macro scope" do
|
||||
source = Source.new %(
|
||||
{{ [1, 2, 3].any? }}
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new path: "source.cr", code: %(
|
||||
[1, 2, 3].any?
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
issue = source.issues.first
|
||||
|
||||
issue.rule.should_not be_nil
|
||||
issue.location.to_s.should eq "source.cr:1:11"
|
||||
issue.end_location.to_s.should eq "source.cr:1:15"
|
||||
issue.message.should eq "Use `!{...}.empty?` instead of `{...}.any?`"
|
||||
end
|
||||
end
|
||||
end
|
44
src/ameba/rule/performance/any_instead_of_empty.cr
Normal file
44
src/ameba/rule/performance/any_instead_of_empty.cr
Normal file
|
@ -0,0 +1,44 @@
|
|||
module Ameba::Rule::Performance
|
||||
# This rule is used to identify usage of arg-less `Enumerable#any?` calls.
|
||||
#
|
||||
# Using `Enumerable#any?` instead of `Enumerable#empty?` might lead to an
|
||||
# unexpected results (like `[nil, false].any? # => false`). In some cases
|
||||
# it also might be less efficient, since it iterates until the block will
|
||||
# return a _truthy_ value, instead of just checking if there's at least
|
||||
# one value present.
|
||||
#
|
||||
# For example, this is considered invalid:
|
||||
#
|
||||
# ```
|
||||
# [1, 2, 3].any?
|
||||
# ```
|
||||
#
|
||||
# And it should be written as this:
|
||||
#
|
||||
# ```
|
||||
# ![1, 2, 3].empty?
|
||||
# ```
|
||||
#
|
||||
# YAML configuration example:
|
||||
#
|
||||
# ```
|
||||
# Performance/AnyInsteadOfEmpty:
|
||||
# Enabled: true
|
||||
# ```
|
||||
class AnyInsteadOfEmpty < Base
|
||||
properties do
|
||||
description "Identifies usage of arg-less `any?` calls."
|
||||
end
|
||||
|
||||
ANY_NAME = "any?"
|
||||
MSG = "Use `!{...}.empty?` instead of `{...}.any?`"
|
||||
|
||||
def test(source, node : Crystal::Call)
|
||||
return unless node.name == ANY_NAME
|
||||
return unless node.block.nil? && node.args.empty?
|
||||
return unless node.obj
|
||||
|
||||
issue_for node.name_location, node.name_end_location, MSG
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue