-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d6238c
commit 18f7e81
Showing
2 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
require "../../../spec_helper" | ||
|
||
module Ameba::Rule::Lint | ||
describe UnusedPseudoMethodCall do | ||
subject = UnusedPseudoMethodCall.new | ||
|
||
it "passes if typeof is unused" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
typeof(1) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if as is unused" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
as(Int32) | ||
CRYSTAL | ||
end | ||
|
||
it "fails if pointerof is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
pointerof(Int32) | ||
# ^^^^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if sizeof is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
sizeof(Int32) | ||
# ^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if instance_sizeof is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
instance_sizeof(Int32) | ||
# ^^^^^^^^^^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if alignof is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
alignof(Int32) | ||
# ^^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if instance_alignof is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
instance_alignof(Int32) | ||
# ^^^^^^^^^^^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if offsetof is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
offsetof(Int32, 1) | ||
# ^^^^^^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if is_a? is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
foo = 1 | ||
foo.is_a?(Int32) | ||
# ^^^^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if as? is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
foo = 1 | ||
foo.as?(Int32) | ||
# ^^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if responds_to? is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
foo = 1 | ||
foo.responds_to?(:bar) | ||
# ^^^^^^^^^^^^^^^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if nil? is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
foo = 1 | ||
foo.nil? | ||
# ^^^^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if prefix not is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
foo = 1 | ||
!foo | ||
# ^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "fails if suffix not is unused" do | ||
expect_issue subject, <<-CRYSTAL | ||
foo = 1 | ||
foo.! | ||
# ^^^ error: Pseudo-method call is not used | ||
CRYSTAL | ||
end | ||
|
||
it "passes if pointerof is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = pointerof(Int32) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if sizeof is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = sizeof(Int32) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if instance_sizeof is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = instance_sizeof(Int32) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if alignof is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = alignof(Int32) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if instance_alignof is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = instance_alignof(Int32) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if offsetof is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = offsetof(Int32, 1) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if is_a? is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = is_a?(Int32) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if as? is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = as?(Int32) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if responds_to? is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = responds_to?(:foo) | ||
CRYSTAL | ||
end | ||
|
||
it "passes if nil? is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = nil? | ||
CRYSTAL | ||
end | ||
|
||
it "passes if prefix not is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = !true | ||
CRYSTAL | ||
end | ||
|
||
it "passes if suffix not is used as an assign value" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
var = true.! | ||
CRYSTAL | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Ameba::Rule::Lint | ||
# A rule that disallows unused pseudo method calls (is_a?, sizeof, etc). | ||
# | ||
# For example, these are considered invalid: | ||
# | ||
# ``` | ||
# pointerof(foo) | ||
# sizeof(Bar) | ||
# | ||
# def method | ||
# !!valid? if guard? | ||
# nil | ||
# end | ||
# ``` | ||
# | ||
# YAML configuration example: | ||
# | ||
# ``` | ||
# Lint/UnusedPseudoMethodCall: | ||
# Enabled: true | ||
# ``` | ||
class UnusedPseudoMethodCall < Base | ||
properties do | ||
since_version "1.7.0" | ||
description "Disallows unused pseudo-method calls" | ||
end | ||
|
||
MSG = "Pseudo-method call is not used" | ||
|
||
def test(source : Source) | ||
AST::ImplicitReturnVisitor.new(self, source) | ||
end | ||
|
||
def test( | ||
source, | ||
node : Crystal::PointerOf | Crystal::SizeOf | Crystal::InstanceSizeOf | | ||
Crystal::AlignOf | Crystal::InstanceAlignOf | Crystal::OffsetOf | | ||
Crystal::IsA | Crystal::NilableCast | Crystal::RespondsTo | Crystal::Not, | ||
in_macro : Bool, | ||
) | ||
issue_for node, MSG | ||
end | ||
end | ||
end |