mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Basic implementation of UnreachableCode rule
This commit is contained in:
parent
d7952204a2
commit
67d76116f7
7 changed files with 415 additions and 0 deletions
102
spec/ameba/rule/lint/unreachable_code_spec.cr
Normal file
102
spec/ameba/rule/lint/unreachable_code_spec.cr
Normal file
|
@ -0,0 +1,102 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::Rule::Lint
|
||||
subject = UnreachableCode.new
|
||||
|
||||
describe UnreachableCode do
|
||||
context "return" do
|
||||
it "reports if there is unreachable code after return" do
|
||||
s = Source.new %(
|
||||
def foo
|
||||
a = 1
|
||||
return false
|
||||
b = 2
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
issue = s.issues.first
|
||||
issue.location.to_s.should eq ":4:3"
|
||||
end
|
||||
|
||||
it "doesn't report if there is return in if" do
|
||||
s = Source.new %(
|
||||
def foo
|
||||
a = 1
|
||||
return false if bar
|
||||
b = 2
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "doesn't report if there are returns in if-then-else" do
|
||||
s = Source.new %(
|
||||
if a > 0
|
||||
return :positivie
|
||||
else
|
||||
return :negative
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context "break" do
|
||||
it "reports if there is unreachable code after break" do
|
||||
s = Source.new %(
|
||||
def foo
|
||||
loop do
|
||||
break
|
||||
a = 1
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
issue = s.issues.first
|
||||
issue.location.to_s.should eq ":4:5"
|
||||
end
|
||||
|
||||
it "doesn't report if break is in a condition" do
|
||||
s = Source.new %(
|
||||
a = -100
|
||||
while true
|
||||
break if a > 0
|
||||
a += 1
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context "next" do
|
||||
it "reports if there is unreachable code after next" do
|
||||
s = Source.new %(
|
||||
a = 1
|
||||
while a < 5
|
||||
next
|
||||
puts a
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
issue = s.issues.first
|
||||
issue.location.to_s.should eq ":4:3"
|
||||
end
|
||||
|
||||
it "doesn't report if next is in a condition" do
|
||||
s = Source.new %(
|
||||
a = 1
|
||||
while a < 5
|
||||
if a == 3
|
||||
next
|
||||
end
|
||||
puts a
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue