Skip to content

Make Lint/NotNilAfterNoBang report calls to #rindex #323

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

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/ameba/rule/lint/not_nil_after_no_bang_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module Ameba::Rule::Lint
it "passes for valid cases" do
expect_no_issues subject, <<-CRYSTAL
(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).rindex { |i| i > 2 }.not_nil!(:foo)
(1..3).find { |i| i > 2 }.not_nil!(:foo)
CRYSTAL
end
Expand All @@ -23,6 +25,17 @@ module Ameba::Rule::Lint
CRYSTAL
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
source = expect_issue subject, <<-CRYSTAL
(1..3).index { |i| i > 2 }.not_nil!
Expand All @@ -34,6 +47,17 @@ module Ameba::Rule::Lint
CRYSTAL
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
source = expect_issue subject, <<-CRYSTAL
(1..3).find { |i| i > 2 }.not_nil!
Expand Down
9 changes: 5 additions & 4 deletions src/ameba/rule/lint/not_nil_after_no_bang.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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:
#
Expand All @@ -23,11 +24,11 @@ module Ameba::Rule::Lint
include AST::Util

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

BLOCK_CALL_NAMES = %w(index find)
CALL_NAMES = %w(index)
BLOCK_CALL_NAMES = %w(index rindex find)
CALL_NAMES = %w(index rindex)

NOT_NIL_NAME = "not_nil!"
MSG = "Use `%s! {...}` instead of `%s {...}.not_nil!`"
Expand Down