Merge pull request #323 from crystal-ameba/Sija/lint-not-nil-after-no-bang-reports-rindex-calls

Make `Lint/NotNilAfterNoBang` report calls to `#rindex`
This commit is contained in:
Sijawusz Pur Rahnama 2023-05-01 11:16:42 +02:00 committed by GitHub
commit 14f6ba0c0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View file

@ -7,7 +7,9 @@ module Ameba::Rule::Lint
it "passes for valid cases" do it "passes for valid cases" do
expect_no_issues subject, <<-CRYSTAL expect_no_issues subject, <<-CRYSTAL
(1..3).index(1).not_nil!(:foo) (1..3).index(1).not_nil!(:foo)
(1..3).rindex(1).not_nil!(:foo)
(1..3).index { |i| i > 2 }.not_nil!(:foo) (1..3).index { |i| i > 2 }.not_nil!(:foo)
(1..3).rindex { |i| i > 2 }.not_nil!(:foo)
(1..3).find { |i| i > 2 }.not_nil!(:foo) (1..3).find { |i| i > 2 }.not_nil!(:foo)
CRYSTAL CRYSTAL
end end
@ -23,6 +25,17 @@ module Ameba::Rule::Lint
CRYSTAL CRYSTAL
end end
it "reports if there is an `rindex` call followed by `not_nil!`" do
source = expect_issue subject, <<-CRYSTAL
(1..3).rindex(1).not_nil!
# ^^^^^^^^^^^^^^^^^^ error: Use `rindex! {...}` instead of `rindex {...}.not_nil!`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).rindex!(1)
CRYSTAL
end
it "reports if there is an `index` call with block followed by `not_nil!`" do it "reports if there is an `index` call with block followed by `not_nil!`" do
source = expect_issue subject, <<-CRYSTAL source = expect_issue subject, <<-CRYSTAL
(1..3).index { |i| i > 2 }.not_nil! (1..3).index { |i| i > 2 }.not_nil!
@ -34,6 +47,17 @@ module Ameba::Rule::Lint
CRYSTAL CRYSTAL
end end
it "reports if there is an `rindex` call with block followed by `not_nil!`" do
source = expect_issue subject, <<-CRYSTAL
(1..3).rindex { |i| i > 2 }.not_nil!
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `rindex! {...}` instead of `rindex {...}.not_nil!`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).rindex! { |i| i > 2 }
CRYSTAL
end
it "reports if there is a `find` call with block followed by `not_nil!`" do it "reports if there is a `find` call with block followed by `not_nil!`" do
source = expect_issue subject, <<-CRYSTAL source = expect_issue subject, <<-CRYSTAL
(1..3).find { |i| i > 2 }.not_nil! (1..3).find { |i| i > 2 }.not_nil!

View file

@ -1,5 +1,6 @@
module Ameba::Rule::Lint module Ameba::Rule::Lint
# This rule is used to identify usage of `index/find` calls followed by `not_nil!`. # This rule is used to identify usage of `index/rindex/find` calls
# followed by a call to `not_nil!`.
# #
# For example, this is considered a code smell: # For example, this is considered a code smell:
# #
@ -23,11 +24,11 @@ module Ameba::Rule::Lint
include AST::Util include AST::Util
properties do properties do
description "Identifies usage of `index/find` calls followed by `not_nil!`" description "Identifies usage of `index/rindex/find` calls followed by `not_nil!`"
end end
BLOCK_CALL_NAMES = %w(index find) BLOCK_CALL_NAMES = %w(index rindex find)
CALL_NAMES = %w(index) CALL_NAMES = %w(index rindex)
NOT_NIL_NAME = "not_nil!" NOT_NIL_NAME = "not_nil!"
MSG = "Use `%s! {...}` instead of `%s {...}.not_nil!`" MSG = "Use `%s! {...}` instead of `%s {...}.not_nil!`"