Skip to content

Commit

Permalink
Merge pull request #334 from crystal-ameba/Sija/crystal-1.7.0-fixes
Browse files Browse the repository at this point in the history
Fix specs on Crystal nightly (soon to be v1.7.0)
  • Loading branch information
Sija authored Jan 10, 2023
2 parents a091af1 + 3b79392 commit e58f900
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion spec/ameba/rule/lint/duplicated_require_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Ameba::Rule::Lint
require "big"
require "math"
require "big"
# ^{} error: Duplicated require of `big`
# ^^^^^^^^^^^ error: Duplicated require of `big`
CRYSTAL

expect_no_corrections source
Expand Down
5 changes: 0 additions & 5 deletions spec/ameba/rule/lint/empty_expression_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ module Ameba
it_detects_empty_expression %(
begin; end
)
it_detects_empty_expression %(
begin
nil
end
)
it_detects_empty_expression %(
begin
()
Expand Down
4 changes: 2 additions & 2 deletions spec/ameba/rule/lint/redundant_with_index_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:19"
issue.end_location.to_s.should eq "source.cr:2:29"
issue.end_location.to_s.should eq "source.cr:2:28"
issue.message.should eq "Remove redundant with_index"
end
end
Expand Down Expand Up @@ -155,7 +155,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:14"
issue.end_location.to_s.should eq "source.cr:2:29"
issue.end_location.to_s.should eq "source.cr:2:28"
issue.message.should eq "Use each instead of each_with_index"
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/rule/lint/redundant_with_object_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:14"
issue.end_location.to_s.should eq "source.cr:2:30"
issue.end_location.to_s.should eq "source.cr:2:29"
issue.message.should eq "Use `each` instead of `each_with_object`"
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/ameba/rule/performance/any_after_filter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Ameba::Rule::Performance
it "reports if there is select followed by any? without a block" do
source = expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.any?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `select {...}.any?`
# ^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `select {...}.any?`
CRYSTAL

expect_no_corrections source
Expand All @@ -32,7 +32,7 @@ module Ameba::Rule::Performance
it "reports if there is reject followed by any? without a block" do
source = expect_issue subject, <<-CRYSTAL
[1, 2, 3].reject { |e| e > 2 }.any?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
# ^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
CRYSTAL

expect_no_corrections source
Expand Down Expand Up @@ -60,7 +60,7 @@ module Ameba::Rule::Performance
it "reports in macro scope" do
source = expect_issue subject, <<-CRYSTAL
{{ [1, 2, 3].reject { |e| e > 2 }.any? }}
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
CRYSTAL

expect_no_corrections source
Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/rule/performance/compact_after_map_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Ameba::Rule::Performance
it "reports if there is map followed by compact call" do
expect_issue subject, <<-CRYSTAL
(1..3).map(&.itself).compact
# ^^^^^^^^^^^^^^^^^^^^^^ error: Use `compact_map {...}` instead of `map {...}.compact`
# ^^^^^^^^^^^^^^^^^^^^^ error: Use `compact_map {...}` instead of `map {...}.compact`
CRYSTAL
end

Expand Down
8 changes: 4 additions & 4 deletions spec/ameba/rule/performance/first_last_after_filter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Ameba::Rule::Performance
it "reports if there is select followed by last" do
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.last
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `reverse_each.find {...}` instead of `select {...}.last`
# ^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `reverse_each.find {...}` instead of `select {...}.last`
CRYSTAL
end

Expand All @@ -30,14 +30,14 @@ module Ameba::Rule::Performance
it "reports if there is select followed by last?" do
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.last?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `reverse_each.find {...}` instead of `select {...}.last?`
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `reverse_each.find {...}` instead of `select {...}.last?`
CRYSTAL
end

it "reports if there is select followed by first" do
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.first
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `find {...}` instead of `select {...}.first`
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `find {...}` instead of `select {...}.first`
CRYSTAL
end

Expand All @@ -50,7 +50,7 @@ module Ameba::Rule::Performance
it "reports if there is select followed by first?" do
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.first?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `find {...}` instead of `select {...}.first?`
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `find {...}` instead of `select {...}.first?`
CRYSTAL
end

Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/rule/performance/flatten_after_map_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Ameba::Rule::Performance
it "reports if there is map followed by flatten call" do
expect_issue subject, <<-CRYSTAL
%w[Alice Bob].map(&.chars).flatten
# ^^^^^^^^^^^^^^^^^^^^^ error: Use `flat_map {...}` instead of `map {...}.flatten`
# ^^^^^^^^^^^^^^^^^^^^ error: Use `flat_map {...}` instead of `map {...}.flatten`
CRYSTAL
end

Expand Down
6 changes: 3 additions & 3 deletions spec/ameba/rule/performance/map_instead_of_block_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Ameba::Rule::Performance
it "reports if there is map followed by sum without a block" do
expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum
# ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
# ^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
CRYSTAL
end

Expand All @@ -27,14 +27,14 @@ module Ameba::Rule::Performance
it "reports if there is map followed by sum without a block (with argument)" do
expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum(0)
# ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
# ^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
CRYSTAL
end

it "reports if there is map followed by sum with a block" do
expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum(&.itself)
# ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
# ^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
CRYSTAL
end

Expand Down
6 changes: 3 additions & 3 deletions spec/ameba/rule/performance/size_after_filter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Ameba::Rule::Performance
it "reports if there is a select followed by size" do
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.size
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `select {...}.size`.
# ^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `select {...}.size`.
CRYSTAL
end

Expand All @@ -32,14 +32,14 @@ module Ameba::Rule::Performance
it "reports if there is a reject followed by size" do
expect_issue subject, <<-CRYSTAL
[1, 2, 3].reject { |e| e < 2 }.size
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `reject {...}.size`.
# ^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `reject {...}.size`.
CRYSTAL
end

it "reports if a block shorthand used" do
expect_issue subject, <<-CRYSTAL
[1, 2, 3].reject(&.empty?).size
# ^^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `reject {...}.size`.
# ^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `reject {...}.size`.
CRYSTAL
end

Expand Down
4 changes: 2 additions & 2 deletions spec/ameba/rule/style/query_bool_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module Ameba::Rule::Style
expect_issue subject, <<-CRYSTAL, call: {{ call }}
class Foo
%{call} foo : Bool = true
_{call} # ^ error: Consider using '%{call}?' for 'foo'
_{call} # ^^^ error: Consider using '%{call}?' for 'foo'
end
CRYSTAL
end
Expand All @@ -60,7 +60,7 @@ module Ameba::Rule::Style
expect_issue subject, <<-CRYSTAL, call: {{ call }}
class Foo
%{call} foo : Bool
_{call} # ^ error: Consider using '%{call}?' for 'foo'
_{call} # ^^^ error: Consider using '%{call}?' for 'foo'
def initialize(@foo = true)
end
Expand Down
12 changes: 2 additions & 10 deletions src/ameba/rule/lint/empty_expression.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,11 @@ module Ameba::Rule::Lint
description "Disallows empty expressions"
end

MSG = "Avoid empty expression %s"
MSG_EXRS = "Avoid empty expressions"

def test(source, node : Crystal::NilLiteral)
exp = node_source(node, source.lines)
return if exp.in?(nil, "nil")

issue_for node, MSG % exp
end
MSG = "Avoid empty expressions"

def test(source, node : Crystal::Expressions)
return unless node.expressions.size == 1 && node.expressions.first.nop?
issue_for node, MSG_EXRS
issue_for node, MSG
end
end
end

0 comments on commit e58f900

Please sign in to comment.