Reduce usage of Object#not_nil!

This commit is contained in:
Sijawusz Pur Rahnama 2022-10-29 00:03:39 +02:00
parent cac1ce4138
commit ffc712a838
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 scope = Scope.new nodes.def_nodes.first
variable = Variable.new(nodes.var_nodes.first, scope) variable = Variable.new(nodes.var_nodes.first, scope)
assignment = Assignment.new(nodes.assign_nodes.first, variable, scope) assignment = Assignment.new(nodes.assign_nodes.first, variable, scope)
assignment.branch.should_not be_nil branch = assignment.branch.should_not be_nil
assignment.branch.not_nil!.node.class.should eq Crystal::Expressions branch.node.class.should eq Crystal::Expressions
end end
it "returns inner branch" do it "returns inner branch" do
@ -70,8 +70,8 @@ module Ameba::AST
scope = Scope.new nodes.def_nodes.first scope = Scope.new nodes.def_nodes.first
variable = Variable.new(nodes.var_nodes.first, scope) variable = Variable.new(nodes.var_nodes.first, scope)
assignment = Assignment.new(nodes.assign_nodes.first, variable, scope) assignment = Assignment.new(nodes.assign_nodes.first, variable, scope)
assignment.branch.should_not be_nil branch = assignment.branch.should_not be_nil
assignment.branch.not_nil!.node.class.should eq Crystal::Assign branch.node.class.should eq Crystal::Assign
end end
it "returns nil if assignment does not have a branch" do it "returns nil if assignment does not have a branch" do

View file

@ -91,9 +91,8 @@ module Ameba::Cli
describe "-e/--explain" do describe "-e/--explain" do
it "configures file/line/column" do it "configures file/line/column" do
c = Cli.parse_args %w(--explain src/file.cr:3:5) 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[:file].should eq "src/file.cr"
location_to_explain[:line].should eq 3 location_to_explain[:line].should eq 3
location_to_explain[:column].should eq 5 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 it "accepts one unknown arg as explain location if it has correct format" do
c = Cli.parse_args %w(source.cr:3:22) 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[:file].should eq "source.cr"
location_to_explain[:line].should eq 3 location_to_explain[:line].should eq 3
location_to_explain[:column].should eq 22 location_to_explain[:column].should eq 22

View file

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

View file

@ -114,7 +114,7 @@ module Ameba::Rule::Style
def test(source, node : Crystal::Next, visitor : AST::RedundantControlExpressionVisitor) def test(source, node : Crystal::Next, visitor : AST::RedundantControlExpressionVisitor)
return if allow_multi_next && node.exp.is_a?(Crystal::TupleLiteral) 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)) if (exp_code = control_exp_code(node, source.lines))
issue_for node, MSG do |corrector| 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) def test(source, node : Crystal::Return, visitor : AST::RedundantControlExpressionVisitor)
return if allow_multi_return && node.exp.is_a?(Crystal::TupleLiteral) 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)) if (exp_code = control_exp_code(node, source.lines))
issue_for node, MSG do |corrector| issue_for node, MSG do |corrector|