diff --git a/src/ameba/ast/variabling/assignment.cr b/src/ameba/ast/variabling/assignment.cr index 6908572a..626e847f 100644 --- a/src/ameba/ast/variabling/assignment.cr +++ b/src/ameba/ast/variabling/assignment.cr @@ -49,7 +49,7 @@ module Ameba::AST # a ||= 1 # ``` def op_assign? - node.is_a? Crystal::OpAssign + node.is_a?(Crystal::OpAssign) end # Returns true if this assignment is in a branch, false if not. diff --git a/src/ameba/ast/variabling/variable.cr b/src/ameba/ast/variabling/variable.cr index 1a7eab42..526b417f 100644 --- a/src/ameba/ast/variabling/variable.cr +++ b/src/ameba/ast/variabling/variable.cr @@ -91,8 +91,8 @@ module Ameba::AST next if consumed_branches.includes?(assignment.branch) assignment.referenced = true - break unless assignment.branch - consumed_branches << assignment.branch.not_nil! + break unless branch = assignment.branch + consumed_branches << branch end end diff --git a/src/ameba/config.cr b/src/ameba/config.cr index 5bf0261e..c3f7b339 100644 --- a/src/ameba/config.cr +++ b/src/ameba/config.cr @@ -270,7 +270,7 @@ class Ameba::Config include YAML::Serializable::Strict def self.new(config = nil) - if (raw = config.try &.raw).is_a? Hash + if (raw = config.try &.raw).is_a?(Hash) yaml = raw[rule_name]?.try &.to_yaml end from_yaml yaml || "{}" diff --git a/src/ameba/formatter/todo_formatter.cr b/src/ameba/formatter/todo_formatter.cr index 88584d1d..47314407 100644 --- a/src/ameba/formatter/todo_formatter.cr +++ b/src/ameba/formatter/todo_formatter.cr @@ -41,7 +41,7 @@ module Ameba::Formatter private def rule_issues_map(issues) Hash(Rule::Base, Array(Issue)).new.tap do |h| issues.each do |issue| - next if issue.disabled? || issue.rule.is_a? Rule::Lint::Syntax + next if issue.disabled? || issue.rule.is_a?(Rule::Lint::Syntax) (h[issue.rule] ||= Array(Issue).new) << issue end end diff --git a/src/ameba/rule/lint/literal_in_condition.cr b/src/ameba/rule/lint/literal_in_condition.cr index 15478516..642350d6 100644 --- a/src/ameba/rule/lint/literal_in_condition.cr +++ b/src/ameba/rule/lint/literal_in_condition.cr @@ -30,8 +30,7 @@ module Ameba::Rule::Lint MSG = "Literal value found in conditional" def check_node(source, node) - return unless literal?(node.cond) - issue_for node, MSG + issue_for node, MSG if literal?(node.cond) end def test(source, node : Crystal::If) diff --git a/src/ameba/rule/lint/percent_array.cr b/src/ameba/rule/lint/percent_array.cr index b8bb1b54..c8ea02a3 100644 --- a/src/ameba/rule/lint/percent_array.cr +++ b/src/ameba/rule/lint/percent_array.cr @@ -26,8 +26,8 @@ module Ameba::Rule::Lint struct PercentArrays < Base properties do description "Disallows some unwanted symbols in percent array literals" - string_array_unwanted_symbols ",\"" - symbol_array_unwanted_symbols ",:" + string_array_unwanted_symbols %(,") + symbol_array_unwanted_symbols %(,:) end MSG = "Symbols `%s` may be unwanted in %s array literals" @@ -62,8 +62,7 @@ module Ameba::Rule::Lint end private def check_array_entry(entry, symbols, literal) - return unless entry =~ /[#{symbols}]/ - MSG % {symbols, literal} + MSG % {symbols, literal} if entry =~ /[#{symbols}]/ end end end diff --git a/src/ameba/rule/lint/rand_zero.cr b/src/ameba/rule/lint/rand_zero.cr index efd8ab7b..ad1843ca 100644 --- a/src/ameba/rule/lint/rand_zero.cr +++ b/src/ameba/rule/lint/rand_zero.cr @@ -33,7 +33,7 @@ module Ameba::Rule::Lint return unless node.name == "rand" && node.args.size == 1 && (arg = node.args.first) && - (arg.is_a? Crystal::NumberLiteral) && + arg.is_a?(Crystal::NumberLiteral) && (value = arg.value) && value.in?("0", "1") diff --git a/src/ameba/rule/lint/redundant_string_coercion.cr b/src/ameba/rule/lint/redundant_string_coercion.cr index 4e85de49..f451e9ea 100644 --- a/src/ameba/rule/lint/redundant_string_coercion.cr +++ b/src/ameba/rule/lint/redundant_string_coercion.cr @@ -30,7 +30,9 @@ module Ameba::Rule::Lint MSG = "Redundant use of `Object#to_s` in interpolation" def test(source, node : Crystal::StringInterpolation) - string_coercion_nodes(node).each { |n| issue_for n.name_location, n.end_location, MSG } + string_coercion_nodes(node).each do |n| + issue_for n.name_location, n.end_location, MSG + end end private def string_coercion_nodes(node) diff --git a/src/ameba/rule/lint/shadowed_exception.cr b/src/ameba/rule/lint/shadowed_exception.cr index a937f8bb..84f2f435 100644 --- a/src/ameba/rule/lint/shadowed_exception.cr +++ b/src/ameba/rule/lint/shadowed_exception.cr @@ -41,11 +41,10 @@ module Ameba::Rule::Lint MSG = "Exception handler has shadowed exceptions: %s" def test(source, node : Crystal::ExceptionHandler) - return unless excs = node.rescues + return unless excs = node.rescues.try &.map(&.types) + return if (excs = shadowed excs).empty? - if (excs = shadowed excs.map(&.types)).any? - issue_for node, MSG % excs.join(", ") - end + issue_for node, MSG % excs.join(", ") end private def shadowed(exceptions, exception_found = false) diff --git a/src/ameba/rule/metrics/cyclomatic_complexity.cr b/src/ameba/rule/metrics/cyclomatic_complexity.cr index 167ebfe7..9ce2bce4 100644 --- a/src/ameba/rule/metrics/cyclomatic_complexity.cr +++ b/src/ameba/rule/metrics/cyclomatic_complexity.cr @@ -20,9 +20,8 @@ module Ameba::Rule::Metrics complexity = AST::CountingVisitor.new(node).count if complexity > max_complexity && (location = node.name_location) - issue_for location, def_name_end_location(node), MSG % { - complexity, max_complexity, - } + issue_for location, def_name_end_location(node), + MSG % {complexity, max_complexity} end end diff --git a/src/ameba/rule/performance/any_after_filter.cr b/src/ameba/rule/performance/any_after_filter.cr index b8aa3f34..c6dbc2b7 100644 --- a/src/ameba/rule/performance/any_after_filter.cr +++ b/src/ameba/rule/performance/any_after_filter.cr @@ -35,12 +35,10 @@ module Ameba::Rule::Performance def test(source, node : Crystal::Call) return unless node.name == ANY_NAME && (obj = node.obj) - return unless obj.is_a?(Crystal::Call) - return if obj.block.nil? || !node.block.nil? + return unless obj.is_a?(Crystal::Call) && obj.block && node.block.nil? + return unless filter_names.includes?(obj.name) - if filter_names.includes?(obj.name) - issue_for obj.name_location, node.name_end_location, MSG % obj.name - end + issue_for obj.name_location, node.name_end_location, MSG % obj.name end end end diff --git a/src/ameba/rule/performance/first_last_after_filter.cr b/src/ameba/rule/performance/first_last_after_filter.cr index 78f3302a..ced0a1e3 100644 --- a/src/ameba/rule/performance/first_last_after_filter.cr +++ b/src/ameba/rule/performance/first_last_after_filter.cr @@ -44,13 +44,12 @@ module Ameba::Rule::Performance def test(source, node : Crystal::Call) return unless CALL_NAMES.includes?(node.name) && (obj = node.obj) - return unless obj.is_a?(Crystal::Call) - return if obj.block.nil? || !node.block.nil? || node.args.any? + return unless obj.is_a?(Crystal::Call) && obj.block + return if !node.block.nil? || node.args.any? + return unless filter_names.includes?(obj.name) - if filter_names.includes?(obj.name) - message = node.name.includes?(CALL_NAMES.first) ? MSG : MSG_REVERSE - issue_for obj.name_location, node.name_end_location, message % {obj.name, node.name} - end + message = node.name.includes?(CALL_NAMES.first) ? MSG : MSG_REVERSE + issue_for obj.name_location, node.name_end_location, message % {obj.name, node.name} end end end diff --git a/src/ameba/rule/performance/size_after_filter.cr b/src/ameba/rule/performance/size_after_filter.cr index 6a281432..0339f998 100644 --- a/src/ameba/rule/performance/size_after_filter.cr +++ b/src/ameba/rule/performance/size_after_filter.cr @@ -50,12 +50,10 @@ module Ameba::Rule::Performance def test(source, node : Crystal::Call) return unless node.name == SIZE_NAME && (obj = node.obj) - return unless obj.is_a?(Crystal::Call) - return if obj.block.nil? + return unless obj.is_a?(Crystal::Call) && obj.block + return unless filter_names.includes?(obj.name) - if filter_names.includes?(obj.name) - issue_for obj.name_location, node.name_end_location, MSG % obj.name - end + issue_for obj.name_location, node.name_end_location, MSG % obj.name end end end diff --git a/src/ameba/rule/style/constant_names.cr b/src/ameba/rule/style/constant_names.cr index fdd7caad..180ed8be 100644 --- a/src/ameba/rule/style/constant_names.cr +++ b/src/ameba/rule/style/constant_names.cr @@ -29,7 +29,7 @@ module Ameba::Rule::Style MSG = "Constant name should be screaming-cased: %s, not %s" def test(source, node : Crystal::Assign) - if (target = node.target).is_a? Crystal::Path + if (target = node.target).is_a?(Crystal::Path) name = target.names.first expected = name.upcase diff --git a/src/ameba/rule/style/is_a_nil.cr b/src/ameba/rule/style/is_a_nil.cr index cbf4aced..4848cd2f 100644 --- a/src/ameba/rule/style/is_a_nil.cr +++ b/src/ameba/rule/style/is_a_nil.cr @@ -4,7 +4,7 @@ module Ameba::Rule::Style # This is considered bad: # # ``` - # var.is_a? Nil + # var.is_a?(Nil) # ``` # # And needs to be written as: @@ -31,7 +31,8 @@ module Ameba::Rule::Style return if node.nil_check? const = node.const - return unless const.is_a?(Crystal::Path) && const.names == PATH_NIL_NAMES + return unless const.is_a?(Crystal::Path) + return unless const.names == PATH_NIL_NAMES issue_for const, MSG end diff --git a/src/ameba/rule/style/negated_conditions_in_unless.cr b/src/ameba/rule/style/negated_conditions_in_unless.cr index 7225c5df..2780e228 100644 --- a/src/ameba/rule/style/negated_conditions_in_unless.cr +++ b/src/ameba/rule/style/negated_conditions_in_unless.cr @@ -42,7 +42,7 @@ module Ameba::Rule::Style when Crystal::BinaryOp negated_condition?(node.left) || negated_condition?(node.right) when Crystal::Expressions - node.expressions.any? { |e| negated_condition? e } + node.expressions.any? { |e| negated_condition?(e) } when Crystal::Not true else