Add `UselessAssign#exclude_type_declarations`

This commit is contained in:
Sijawusz Pur Rahnama 2023-12-29 01:50:19 +01:00
parent aeffa6ad00
commit 5a24f1eba5
2 changed files with 25 additions and 0 deletions

View File

@ -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
@ -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

View File

@ -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`"
@ -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?