-
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
791aeac
commit 7d6238c
Showing
2 changed files
with
99 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,56 @@ | ||
require "../../../spec_helper" | ||
|
||
module Ameba::Rule::Lint | ||
describe UnusedSelfAccess do | ||
subject = UnusedSelfAccess.new | ||
|
||
it "passes if self is used as receiver for a method def" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
def self.foo | ||
end | ||
CRYSTAL | ||
end | ||
|
||
it "passes if self is used as object of call" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
self.foo | ||
CRYSTAL | ||
end | ||
|
||
it "passes if self is used as method of call" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
foo.self | ||
CRYSTAL | ||
end | ||
|
||
it "fails if self is unused in void context of class body" do | ||
expect_issue subject, <<-CRYSTAL | ||
class MyClass | ||
self | ||
# ^^^^ error: `self` is not used | ||
end | ||
CRYSTAL | ||
end | ||
|
||
it "fails if self is unused in void context of begin" do | ||
expect_issue subject, <<-CRYSTAL | ||
begin | ||
self | ||
# ^^^^ error: `self` is not used | ||
"foo" | ||
end | ||
CRYSTAL | ||
end | ||
|
||
it "fails if self is unused in void context of method def" do | ||
expect_issue subject, <<-CRYSTAL | ||
def foo | ||
self | ||
# ^^^^ error: `self` is not used | ||
"bar" | ||
end | ||
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,43 @@ | ||
module Ameba::Rule::Lint | ||
# A rule that disallows unused accesses of `self`. | ||
# | ||
# For example, this is considered invalid: | ||
# | ||
# ``` | ||
# class MyClass | ||
# self | ||
# | ||
# def self.foo | ||
# self | ||
# puts "Hello, world!" | ||
# end | ||
# end | ||
# ``` | ||
# | ||
# YAML configuration example: | ||
# | ||
# ``` | ||
# Lint/UnusedSelfAccess: | ||
# Enabled: true | ||
# ``` | ||
class UnusedSelfAccess < Base | ||
properties do | ||
since_version "1.7.0" | ||
description "Disallows unused self" | ||
end | ||
|
||
MSG = "`self` is not used" | ||
|
||
def test(source : Source) | ||
AST::ImplicitReturnVisitor.new(self, source) | ||
end | ||
|
||
def test(source, node : Crystal::Self, in_macro : Bool) | ||
issue_for node, MSG | ||
end | ||
|
||
def test(source, node : Crystal::Var, in_macro : Bool) | ||
issue_for node, MSG if node.name == "self" | ||
end | ||
end | ||
end |