Skip to content

Commit 3b8ab34

Browse files
committed
Add new @primitive to warn "given block not used"
1 parent a95249e commit 3b8ab34

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.truffleruby.builtins.NonStandard;
3838
import org.truffleruby.annotations.Primitive;
3939
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
40+
import org.truffleruby.builtins.PrimitiveNode;
4041
import org.truffleruby.core.array.ArrayUtils;
4142
import org.truffleruby.core.array.RubyArray;
4243
import org.truffleruby.core.basicobject.BasicObjectNodes.ObjectIDNode;
@@ -2024,4 +2025,20 @@ protected boolean setCategory(RubySymbol category, boolean newValue) {
20242025

20252026
}
20262027

2028+
@Primitive(name = "warn_given_block_not_used")
2029+
public abstract static class WarnGivenBlockNotUsedNode extends PrimitiveNode {
2030+
@Specialization
2031+
protected Object warn(
2032+
@Cached("new()") WarnNode warnNode) {
2033+
if (warnNode.shouldWarn()) {
2034+
warnNode.warningMessage(
2035+
getContext().getCallStack().getTopMostUserSourceSection(),
2036+
"given block not used");
2037+
}
2038+
2039+
return Nil.INSTANCE;
2040+
}
2041+
2042+
}
2043+
20272044
}

src/main/ruby/truffleruby/core/array.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def combination(num)
304304
def count(item = undefined)
305305
seq = 0
306306
if !Primitive.undefined?(item)
307-
warn 'given block not used', uplevel: 1 if block_given?
307+
Primitive.warn_given_block_not_used if block_given?
308308
each { |o| seq += 1 if item == o }
309309
elsif block_given?
310310
each { |o| seq += 1 if yield(o) }
@@ -1012,7 +1012,7 @@ def rindex(obj=undefined)
10121012
i -= 1
10131013
end
10141014
else
1015-
warn 'given block not used', uplevel: 1 if block_given?
1015+
Primitive.warn_given_block_not_used if block_given?
10161016

10171017
i = size - 1
10181018
while i >= 0

src/main/ruby/truffleruby/core/enumerable.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def compact
128128
def count(item = undefined)
129129
seq = 0
130130
if !Primitive.undefined?(item)
131-
warn 'given block not used', uplevel: 1 if block_given?
131+
Primitive.warn_given_block_not_used if block_given?
132132
each do
133133
o = Primitive.single_block_arg
134134
seq += 1 if item == o
@@ -522,7 +522,7 @@ def inject(initial=undefined, sym=undefined, &block)
522522

523523
# inject(initial_operand, symbol) { ... }
524524
if block_given? && !Primitive.undefined?(sym)
525-
warn 'given block not used', uplevel: 1
525+
Primitive.warn_given_block_not_used
526526
end
527527

528528
if Primitive.undefined?(sym) && block_given?
@@ -563,7 +563,7 @@ def inject(initial=undefined, sym=undefined, &block)
563563

564564
def all?(pattern = undefined)
565565
if !Primitive.undefined?(pattern)
566-
warn 'given block not used', uplevel: 1 if block_given?
566+
Primitive.warn_given_block_not_used if block_given?
567567
each { return false unless pattern === Primitive.single_block_arg }
568568
elsif block_given?
569569
each { |*e| return false unless yield(*e) }
@@ -575,7 +575,7 @@ def all?(pattern = undefined)
575575

576576
def any?(pattern = undefined)
577577
if !Primitive.undefined?(pattern)
578-
warn 'given block not used', uplevel: 1 if block_given?
578+
Primitive.warn_given_block_not_used if block_given?
579579
each { return true if pattern === Primitive.single_block_arg }
580580
elsif block_given?
581581
each { |*o| return true if yield(*o) }
@@ -735,7 +735,7 @@ def find_index(value=undefined)
735735
i += 1
736736
end
737737
else
738-
warn 'given block not used', uplevel: 1 if block_given?
738+
Primitive.warn_given_block_not_used if block_given?
739739

740740
i = 0
741741
each do
@@ -926,7 +926,7 @@ def minmax_by(&block)
926926

927927
def none?(pattern = undefined)
928928
if !Primitive.undefined?(pattern)
929-
warn 'given block not used', uplevel: 1 if block_given?
929+
Primitive.warn_given_block_not_used if block_given?
930930
each { return false if pattern === Primitive.single_block_arg }
931931
elsif block_given?
932932
each { |*o| return false if yield(*o) }
@@ -941,7 +941,7 @@ def one?(pattern = undefined)
941941
found_one = false
942942

943943
if !Primitive.undefined?(pattern)
944-
warn 'given block not used', uplevel: 1 if block_given?
944+
Primitive.warn_given_block_not_used if block_given?
945945
each do
946946
if pattern === Primitive.single_block_arg
947947
return false if found_one

0 commit comments

Comments
 (0)