From ffc712a8387872e0a49fdf82839d65c8b95275b2 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 29 Oct 2022 00:03:39 +0200 Subject: [PATCH] Reduce usage of `Object#not_nil!` --- spec/ameba/ast/variabling/assignment_spec.cr | 8 ++++---- spec/ameba/cli/cmd_spec.cr | 6 ++---- spec/ameba/config_spec.cr | 12 ++++++------ src/ameba/rule/style/redundant_next.cr | 2 +- src/ameba/rule/style/redundant_return.cr | 2 +- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/spec/ameba/ast/variabling/assignment_spec.cr b/spec/ameba/ast/variabling/assignment_spec.cr index 7f50f6d1..fc91348a 100644 --- a/spec/ameba/ast/variabling/assignment_spec.cr +++ b/spec/ameba/ast/variabling/assignment_spec.cr @@ -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 diff --git a/spec/ameba/cli/cmd_spec.cr b/spec/ameba/cli/cmd_spec.cr index def5b6bf..6f058f7c 100644 --- a/spec/ameba/cli/cmd_spec.cr +++ b/spec/ameba/cli/cmd_spec.cr @@ -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 diff --git a/spec/ameba/config_spec.cr b/spec/ameba/config_spec.cr index d7547a2a..98ecb851 100644 --- a/spec/ameba/config_spec.cr +++ b/spec/ameba/config_spec.cr @@ -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 diff --git a/src/ameba/rule/style/redundant_next.cr b/src/ameba/rule/style/redundant_next.cr index 214690b5..8f9acb5c 100644 --- a/src/ameba/rule/style/redundant_next.cr +++ b/src/ameba/rule/style/redundant_next.cr @@ -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| diff --git a/src/ameba/rule/style/redundant_return.cr b/src/ameba/rule/style/redundant_return.cr index 77eafe73..a85ea7ef 100644 --- a/src/ameba/rule/style/redundant_return.cr +++ b/src/ameba/rule/style/redundant_return.cr @@ -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|