diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index b63c20548f..6f372472a9 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -146,7 +146,9 @@ proc discardCheck(result: PNode) = if result.typ.kind == tyNil: fixNilType(result) else: - localError(result.info, errDiscardValue) + var n = result + while n.kind in skipForDiscardable: n = n.lastSon + localError(n.info, errDiscardValue) proc semIf(c: PContext, n: PNode): PNode = result = n diff --git a/tests/reject/tneedsdiscard.nim b/tests/reject/tneedsdiscard.nim new file mode 100644 index 0000000000..24f5b2eeee --- /dev/null +++ b/tests/reject/tneedsdiscard.nim @@ -0,0 +1,12 @@ +discard """ + line: 10 + errormsg: "value returned by statement has to be discarded" +""" + +proc p = + var f: TFile + echo "hi" + + open(f, "arg.txt") + +p()