Skip to content

Commit

Permalink
Add UselessAssign#exclude_type_declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Dec 29, 2023
1 parent aeffa6a commit 5a24f1e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions spec/ameba/rule/lint/useless_assign_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "../../../spec_helper"
module Ameba::Rule::Lint
describe UselessAssign do
subject = UselessAssign.new
.tap(&.exclude_type_declarations = false)

it "does not report used assignments" do
expect_no_issues subject, <<-CRYSTAL
Expand Down Expand Up @@ -1007,6 +1008,27 @@ module Ameba::Rule::Lint
end
end

context "#properties" do
context "#exclude_type_declarations" do
it "doesn't report type declarations if enabled" do
rule = UselessAssign.new
rule.exclude_type_declarations = true

expect_no_issues rule, <<-CRYSTAL
a : String?
class Foo
b : String?
end
def foo
c : String?
end
CRYSTAL
end
end
end

context "uninitialized" do
it "reports if uninitialized assignment is not referenced at a top level" do
expect_issue subject, <<-CRYSTAL
Expand Down
3 changes: 3 additions & 0 deletions src/ameba/rule/lint/useless_assign.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ module Ameba::Rule::Lint
# ```
# Lint/UselessAssign:
# Enabled: true
# ExcludeTypeDeclarations: false
# ```
class UselessAssign < Base
properties do
description "Disallows useless variable assignments"
exclude_type_declarations false
end

MSG = "Useless assignment to variable `%s`"
Expand All @@ -39,6 +41,7 @@ module Ameba::Rule::Lint
def test(source, node, scope : AST::Scope)
scope.variables.each do |var|
next if var.ignored? || var.used_in_macro? || var.captured_by_block?
next if exclude_type_declarations? && scope.assigns_type_dec?(var.name)

var.assignments.each do |assign|
next if assign.referenced?
Expand Down

0 comments on commit 5a24f1e

Please sign in to comment.