From eb64fde437105badc3bd50b497603d3972eb5952 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sun, 30 Oct 2022 18:15:00 +0100 Subject: [PATCH] Fix few issues reported by the newly added rule --- spec/ameba/ast/scope_spec.cr | 6 ++++-- spec/ameba/formatter/todo_formatter_spec.cr | 4 ++-- src/ameba/rule/lint/percent_array.cr | 8 ++++---- src/ameba/rule/lint/redundant_with_object.cr | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/spec/ameba/ast/scope_spec.cr b/spec/ameba/ast/scope_spec.cr index 2e97ab6c..c3033a6e 100644 --- a/spec/ameba/ast/scope_spec.cr +++ b/spec/ameba/ast/scope_spec.cr @@ -135,14 +135,16 @@ module Ameba::AST scope = Scope.new as_node("foo = 1") scope.add_variable Crystal::Var.new "foo" scope.assign_variable("foo", Crystal::Var.new "foo") - scope.find_variable("foo").not_nil!.assignments.size.should eq 1 + var = scope.find_variable("foo").should_not be_nil + var.assignments.size.should eq 1 end it "does not create the assignment if variable is wrong" do scope = Scope.new as_node("foo = 1") scope.add_variable Crystal::Var.new "foo" scope.assign_variable("bar", Crystal::Var.new "bar") - scope.find_variable("foo").not_nil!.assignments.size.should eq 0 + var = scope.find_variable("foo").should_not be_nil + var.assignments.size.should eq 0 end end diff --git a/spec/ameba/formatter/todo_formatter_spec.cr b/spec/ameba/formatter/todo_formatter_spec.cr index 8c1d7dee..aab58414 100644 --- a/spec/ameba/formatter/todo_formatter_spec.cr +++ b/spec/ameba/formatter/todo_formatter_spec.cr @@ -86,8 +86,8 @@ module Ameba s1.add_issue DummyRule.new, {2, 2}, "message1" s2.add_issue DummyRule.new, {2, 2}, "message2" - file = formatter.finished([s1, s2]) - content = File.read(file.not_nil!.path) + file = formatter.finished([s1, s2]).should_not be_nil + content = File.read(file.path) content.should contain <<-CONTENT # Problems found: 3 # Run `ameba --only Ameba/DummyRule` for details diff --git a/src/ameba/rule/lint/percent_array.cr b/src/ameba/rule/lint/percent_array.cr index fde05e88..1bccd987 100644 --- a/src/ameba/rule/lint/percent_array.cr +++ b/src/ameba/rule/lint/percent_array.cr @@ -41,12 +41,12 @@ module Ameba::Rule::Lint when .string_array_start?, .symbol_array_start? start_token = token.dup when .string? - if start_token && issue.nil? - issue = array_entry_invalid?(token.value, start_token.not_nil!.raw) + if (_start = start_token) && !issue + issue = array_entry_invalid?(token.value, _start.raw) end when .string_array_end? - if issue - issue_for start_token.not_nil!, issue.not_nil! + if (_start = start_token) && (_issue = issue) + issue_for _start, _issue end issue = start_token = nil end diff --git a/src/ameba/rule/lint/redundant_with_object.cr b/src/ameba/rule/lint/redundant_with_object.cr index 9acf0bf8..ed1bcc44 100644 --- a/src/ameba/rule/lint/redundant_with_object.cr +++ b/src/ameba/rule/lint/redundant_with_object.cr @@ -37,8 +37,8 @@ module Ameba::Rule::Lint def test(source, node : Crystal::Call) return if node.name != "each_with_object" || node.args.size != 1 || - node.block.nil? || - with_index_arg?(node.block.not_nil!) + !(block = node.block) || + with_index_arg?(block) issue_for node.name_location, node.name_end_location, MSG end