Merge pull request #281 from crystal-ameba/Sija/reduce-not-nil-usage

This commit is contained in:
Sijawusz Pur Rahnama 2022-10-29 11:58:08 +02:00 committed by GitHub
commit 0025cb8486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 16 deletions

View File

@ -53,8 +53,8 @@ module Ameba::AST
scope = Scope.new nodes.def_nodes.first
variable = Variable.new(nodes.var_nodes.first, scope)
assignment = Assignment.new(nodes.assign_nodes.first, variable, scope)
assignment.branch.should_not be_nil
assignment.branch.not_nil!.node.class.should eq Crystal::Expressions
branch = assignment.branch.should_not be_nil
branch.node.class.should eq Crystal::Expressions
end
it "returns inner branch" do
@ -70,8 +70,8 @@ module Ameba::AST
scope = Scope.new nodes.def_nodes.first
variable = Variable.new(nodes.var_nodes.first, scope)
assignment = Assignment.new(nodes.assign_nodes.first, variable, scope)
assignment.branch.should_not be_nil
assignment.branch.not_nil!.node.class.should eq Crystal::Assign
branch = assignment.branch.should_not be_nil
branch.node.class.should eq Crystal::Assign
end
it "returns nil if assignment does not have a branch" do

View File

@ -91,9 +91,8 @@ module Ameba::Cli
describe "-e/--explain" do
it "configures file/line/column" do
c = Cli.parse_args %w(--explain src/file.cr:3:5)
c.location_to_explain.should_not be_nil
location_to_explain = c.location_to_explain.not_nil!
location_to_explain = c.location_to_explain.should_not be_nil
location_to_explain[:file].should eq "src/file.cr"
location_to_explain[:line].should eq 3
location_to_explain[:column].should eq 5
@ -154,9 +153,8 @@ module Ameba::Cli
it "accepts one unknown arg as explain location if it has correct format" do
c = Cli.parse_args %w(source.cr:3:22)
c.location_to_explain.should_not be_nil
location_to_explain = c.location_to_explain.not_nil!
location_to_explain = c.location_to_explain.should_not be_nil
location_to_explain[:file].should eq "source.cr"
location_to_explain[:line].should eq 3
location_to_explain[:column].should eq 22

View File

@ -165,7 +165,7 @@ module Ameba
it "updates enabled property" do
name = DummyRule.rule_name
config.update_rule name, enabled: false
rule = config.rules.find(&.name.== name).not_nil!
rule = config.rules.find!(&.name.== name)
rule.enabled.should be_false
end
@ -173,7 +173,7 @@ module Ameba
name = DummyRule.rule_name
excluded = %w(spec/source.cr)
config.update_rule name, excluded: excluded
rule = config.rules.find(&.name.== name).not_nil!
rule = config.rules.find!(&.name.== name)
rule.excluded.should eq excluded
end
end
@ -184,7 +184,7 @@ module Ameba
it "updates multiple rules by enabled property" do
name = DummyRule.rule_name
config.update_rules [name], enabled: false
rule = config.rules.find(&.name.== name).not_nil!
rule = config.rules.find!(&.name.== name)
rule.enabled.should be_false
end
@ -192,14 +192,14 @@ module Ameba
name = DummyRule.rule_name
excluded = %w(spec/source.cr)
config.update_rules [name], excluded: excluded
rule = config.rules.find(&.name.== name).not_nil!
rule = config.rules.find!(&.name.== name)
rule.excluded.should eq excluded
end
it "updates a group of rules by enabled property" do
group = DummyRule.group_name
config.update_rules [group], enabled: false
rule = config.rules.find(&.name.== DummyRule.rule_name).not_nil!
rule = config.rules.find!(&.name.== DummyRule.rule_name)
rule.enabled.should be_false
end
@ -207,7 +207,7 @@ module Ameba
name = DummyRule.group_name
excluded = %w(spec/source.cr)
config.update_rules [name], excluded: excluded
rule = config.rules.find(&.name.== DummyRule.rule_name).not_nil!
rule = config.rules.find!(&.name.== DummyRule.rule_name)
rule.excluded.should eq excluded
end
end

View File

@ -114,7 +114,7 @@ module Ameba::Rule::Style
def test(source, node : Crystal::Next, visitor : AST::RedundantControlExpressionVisitor)
return if allow_multi_next && node.exp.is_a?(Crystal::TupleLiteral)
return if allow_empty_next && (node.exp.nil? || node.exp.not_nil!.nop?)
return if allow_empty_next && (node.exp.nil? || node.exp.try(&.nop?))
if (exp_code = control_exp_code(node, source.lines))
issue_for node, MSG do |corrector|

View File

@ -111,7 +111,7 @@ module Ameba::Rule::Style
def test(source, node : Crystal::Return, visitor : AST::RedundantControlExpressionVisitor)
return if allow_multi_return && node.exp.is_a?(Crystal::TupleLiteral)
return if allow_empty_return && (node.exp.nil? || node.exp.not_nil!.nop?)
return if allow_empty_return && (node.exp.nil? || node.exp.try(&.nop?))
if (exp_code = control_exp_code(node, source.lines))
issue_for node, MSG do |corrector|