From 43e7deb827f7fbc58039e988e50c6a6eb1c37a0b Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 19 Dec 2023 15:23:12 +0100 Subject: [PATCH] Fix BareExcept warnings --- questionable/private/bareexcept.nim | 6 ++++++ questionable/results.nim | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 questionable/private/bareexcept.nim diff --git a/questionable/private/bareexcept.nim b/questionable/private/bareexcept.nim new file mode 100644 index 0000000..b87c426 --- /dev/null +++ b/questionable/private/bareexcept.nim @@ -0,0 +1,6 @@ +template ignoreBareExceptWarning*(body) = + when defined(nimHasWarnBareExcept): + {.push warning[BareExcept]:off warning[UnreachableCode]:off.} + body + when defined(nimHasWarnBareExcept): + {.pop.} diff --git a/questionable/results.nim b/questionable/results.nim index 396f618..f3d4565 100644 --- a/questionable/results.nim +++ b/questionable/results.nim @@ -7,6 +7,7 @@ import ./indexing import ./operators import ./without import ./withoutresult +import ./private/bareexcept include ./private/errorban @@ -109,12 +110,13 @@ proc option*[T,E](value: Result[T,E]): ?T = ## Converts a Result into an Option. if value.isOk: - try: # workaround for erroneous exception tracking when T is a closure - value.unsafeGet.some - except Exception as exception: - raise newException(Defect, exception.msg, exception) + ignoreBareExceptWarning: + try: # workaround for erroneous exception tracking when T is a closure + return value.unsafeGet.some + except Exception as exception: + raise newException(Defect, exception.msg, exception) else: - T.none + return T.none template toOption*[T, E](value: Result[T, E]): ?T = ## Converts a Result into an Option.