Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions core/warning/warn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ def Warning.warn(msg)
end
end

ruby_version_is "3.4" do
it "warns when category is :strict_unused_block but Warning[:strict_unused_block] is false" do
warn_experimental = Warning[:strict_unused_block]
Warning[:strict_unused_block] = true
begin
-> {
Warning.warn("foo", category: :strict_unused_block)
}.should complain("foo")
ensure
Warning[:strict_unused_block] = warn_experimental
end
end
end

it "doesn't print message when category is :deprecated but Warning[:deprecated] is false" do
warn_deprecated = Warning[:deprecated]
Warning[:deprecated] = false
Expand All @@ -121,6 +135,20 @@ def Warning.warn(msg)
end
end

ruby_version_is "3.4" do
it "doesn't print message when category is :strict_unused_block but Warning[:strict_unused_block] is false" do
warn_experimental = Warning[:strict_unused_block]
Warning[:strict_unused_block] = false
begin
-> {
Warning.warn("foo", category: :strict_unused_block)
}.should_not complain
ensure
Warning[:strict_unused_block] = warn_experimental
end
end
end

ruby_bug '#20573', ''...'3.4' do
it "isn't called by Kernel.warn when category is :deprecated but Warning[:deprecated] is false" do
warn_deprecated = Warning[:deprecated]
Expand Down
160 changes: 160 additions & 0 deletions language/method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1487,3 +1487,163 @@ def greet(person) = "Hi, ".dup.concat person
greet("Homer").should == "Hi, Homer"
end
end

describe "warning about not used block argument" do
ruby_version_is "3.4" do
it "warns when passing a block argument to a method that never uses it" do
def m_that_does_not_use_block
42
end

-> {
m_that_does_not_use_block { }
}.should complain(
/#{__FILE__}:#{__LINE__ - 2}: warning: the block passed to 'm_that_does_not_use_block' defined at #{__FILE__}:#{__LINE__ - 7} may be ignored/,
verbose: true)
end

it "does not warn when passing a block argument to a method that declares a block parameter" do
def m_with_block_parameter(&block)
42
end

-> { m_with_block_parameter { } }.should_not complain(verbose: true)
end

it "does not warn when passing a block argument to a method that declares an anonymous block parameter" do
def m_with_anonymous_block_parameter(&)
42
end

-> { m_with_anonymous_block_parameter { } }.should_not complain(verbose: true)
end

it "does not warn when passing a block argument to a method that yields an implicit block parameter" do
def m_with_yield
yield 42
end

-> { m_with_yield { } }.should_not complain(verbose: true)
end

it "warns when passing a block argument to a method that calls #block_given?" do
def m_with_block_given
block_given?
end

-> {
m_with_block_given { }
}.should complain(
/#{__FILE__}:#{__LINE__ - 2}: warning: the block passed to 'm_with_block_given' defined at #{__FILE__}:#{__LINE__ - 7} may be ignored/,
verbose: true)
end

it "does not warn when passing a block argument to a method that calls super" do
parent = Class.new do
def m
end
end

child = Class.new(parent) do
def m
super
end
end

obj = child.new
-> { obj.m { } }.should_not complain(verbose: true)
end

it "does not warn when passing a block argument to a method that calls super(...)" do
parent = Class.new do
def m(a)
end
end

child = Class.new(parent) do
def m(...)
super(...)
end
end

obj = child.new
-> { obj.m(42) { } }.should_not complain(verbose: true)
end

it "does not warn when called #initialize()" do
klass = Class.new do
def initialize
end
end

-> { klass.new {} }.should_not complain(verbose: true)
end

it "does not warn when passing a block argument to a method that calls super()" do
parent = Class.new do
def m
end
end

child = Class.new(parent) do
def m
super()
end
end

obj = child.new
-> { obj.m { } }.should_not complain(verbose: true)
end

it "warns only once per call site" do
def m_that_does_not_use_block
42
end

def call_m_that_does_not_use_block
m_that_does_not_use_block {}
end

-> {
m_that_does_not_use_block { }
}.should complain(/the block passed to 'm_that_does_not_use_block' defined at .+ may be ignored/, verbose: true)

-> {
m_that_does_not_use_block { }
}.should_not complain(verbose: true)
end

it "can be disabled with :strict_unused_block warning category" do
def m_that_does_not_use_block
42
end

# ensure that warning is emitted
-> { m_that_does_not_use_block { } }.should complain(verbose: true)

warn_experimental = Warning[:strict_unused_block]
Warning[:strict_unused_block] = false
begin
-> { m_that_does_not_use_block { } }.should_not complain(verbose: true)
ensure
Warning[:strict_unused_block] = warn_experimental
end
end

it "can be enabled with :strict_unused_block = true warning category in not verbose mode" do
def m_that_does_not_use_block
42
end

warn_experimental = Warning[:strict_unused_block]
Warning[:strict_unused_block] = true
begin
-> {
m_that_does_not_use_block { }
}.should complain(/the block passed to 'm_that_does_not_use_block' defined at .+ may be ignored/)
ensure
Warning[:strict_unused_block] = warn_experimental
end
end
end
end