suppress unreachable string format errors (#81)

* suppress unreachable string format errors

We use `%` and `&` to format some strings, which may raise `ValueError`
if the format string is weird. As we are using them in ways that should
always succeed, catch those `ValueError` and convert to asserts.

* add `{.raises: [].}`
This commit is contained in:
Etan Kissling 2023-06-09 21:16:55 +02:00 committed by GitHub
parent 6c3566850d
commit b30f22da26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -406,9 +406,15 @@ func getNextArgIdx(cmd: CmdInfo, consumedArgIdx: int): int =
-1
proc noMoreArgsError(cmd: CmdInfo): string =
result = if cmd.isSubCommand: "The command '$1'" % [cmd.name]
else: appInvocation()
proc noMoreArgsError(cmd: CmdInfo): string {.raises: [].} =
result =
if cmd.isSubCommand:
try:
"The command '$1'" % [cmd.name]
except ValueError as err:
raiseAssert "strutils.`%` failed: " & err.msg
else:
appInvocation()
result.add " does not accept"
if cmd.hasArgs: result.add " additional"
result.add " arguments"
@ -870,11 +876,13 @@ proc addConfigFileContent*(secondarySources: auto,
except IOError:
raiseAssert "This should not be possible"
func constructEnvKey*(prefix: string, key: string): string =
func constructEnvKey*(prefix: string, key: string): string {.raises: [].} =
## Generates env. variable names from keys and prefix following the
## IEEE Open Group env. variable spec: https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html
return (&"{prefix}_{key}").toUpperAscii.multiReplace(("-", "_"), (" ", "_"))
try:
(&"{prefix}_{key}").toUpperAscii.multiReplace(("-", "_"), (" ", "_"))
except ValueError as err:
raiseAssert "strformat.`&` failed: " & err.msg
proc loadImpl[C, SecondarySources](
Configuration: typedesc[C],