From 38dfeaaabdc6792d0f4d701621cbe34001978456 Mon Sep 17 00:00:00 2001 From: jangko Date: Wed, 15 Feb 2023 15:26:53 +0700 Subject: [PATCH] reduce compiler warnings --- confutils.nim | 39 ++++++++++++++++++++++++---------- confutils/cli_parser.nim | 4 ++-- confutils/config_file.nim | 10 ++++++--- confutils/std/net.nim | 4 ++-- confutils/winreg/reader.nim | 2 +- tests/test_config_file.nim | 6 +++--- tests/test_parsecmdarg.nim | 5 ++++- tests/test_qualified_ident.nim | 2 +- 8 files changed, 48 insertions(+), 24 deletions(-) diff --git a/confutils.nim b/confutils.nim index 0ba610f..1a3219b 100644 --- a/confutils.nim +++ b/confutils.nim @@ -465,7 +465,7 @@ else: # TODO remove the overloads here to get better "missing overload" error message proc parseCmdArg*(T: type InputDir, p: string): T = - if not dirExists(p.string): + if not dirExists(p): raise newException(ValueError, "Directory doesn't exist") T(p) @@ -474,17 +474,17 @@ proc parseCmdArg*(T: type InputFile, p: string): T = # TODO this is needed only because InputFile cannot be made # an alias of TypedInputFile at the moment, because of a generics # caching issue - if not fileExists(p.string): + if not fileExists(p): raise newException(ValueError, "File doesn't exist") when not defined(nimscript): try: - let f = system.open(p.string, fmRead) + let f = system.open(p, fmRead) close f except IOError: raise newException(ValueError, "File not accessible") - T(p.string) + T(p) proc parseCmdArg*(T: type TypedInputFile, p: string): T = var path = p @@ -513,10 +513,10 @@ template parseCmdArg*(T: type string, s: string): string = s func parseCmdArg*(T: type SomeSignedInt, s: string): T = - T parseBiggestInt(string s) + T parseBiggestInt(s) func parseCmdArg*(T: type SomeUnsignedInt, s: string): T = - T parseBiggestUInt(string s) + T parseBiggestUInt(s) func parseCmdArg*(T: type SomeFloat, p: string): T = parseFloat(p) @@ -525,10 +525,10 @@ func parseCmdArg*(T: type bool, p: string): T = try: p.len == 0 or parseBool(p) except CatchableError: - raise newException(ValueError, "'" & p.string & "' is not a valid boolean value. Supported values are on/off, yes/no, true/false or 1/0") + raise newException(ValueError, "'" & p & "' is not a valid boolean value. Supported values are on/off, yes/no, true/false or 1/0") func parseCmdArg*(T: type enum, s: string): T = - parseEnum[T](string(s)) + parseEnum[T](s) proc parseCmdArgAux(T: type, s: string): T = # {.raises: [ValueError].} = # The parseCmdArg procs are allowed to raise only `ValueError`. @@ -662,6 +662,13 @@ proc generateFieldSetters(RecordType: NimNode): NimNode = newCall(bindSym"requiresInput", fixedFieldType), newCall(bindSym"acceptsMultipleValues", fixedFieldType)) + when (NimMajor, NimMinor) >= (1, 6): + result.add quote do: + {.push hint[XCannotRaiseY]: off.} + else: + result.add quote do: + {.push hint[XDeclaredButNotUsed]: off.} + result.add quote do: proc `completerName`(val: string): seq[string] {. nimcall @@ -681,12 +688,15 @@ proc generateFieldSetters(RecordType: NimNode): NimNode = # TODO: For some reason, the normal `setField` rejects enum fields # when they are used as case discriminators. File this as a bug. if isSome(val): - `configField` = parseEnumNormalized[type(`configField`)](string(val.get)) + `configField` = parseEnumNormalized[type(`configField`)](val.get) else: `configField` = `defaultValue` else: setField(`configField`, val, `defaultValue`) + result.add quote do: + {.pop.} + result.add settersArray debugMacroResult "Field Setters" @@ -891,15 +901,21 @@ proc loadImpl[C, SecondarySources]( let confAddr = addr result template applySetter(setterIdx: int, cmdLineVal: string) = + when (NimMajor, NimMinor) >= (1, 6): + {.warning[BareExcept]:off.} + try: fieldSetters[setterIdx][1](confAddr[], some(cmdLineVal)) inc fieldCounters[setterIdx] except: fail("Error while processing the ", fgOption, fieldSetters[setterIdx][0], - "=", cmdLineVal.string, resetStyle, " parameter: ", + "=", cmdLineVal, resetStyle, " parameter: ", getCurrentExceptionMsg()) + when (NimMajor, NimMinor) >= (1, 6): + {.warning[BareExcept]:on.} + when hasCompletions: template getArgCompletions(opt: OptInfo, prefix: string): seq[string] = fieldSetters[opt.idx][2](prefix) @@ -1017,7 +1033,8 @@ proc loadImpl[C, SecondarySources]( flushOutputAndQuit QuitSuccess for kind, key, val in getopt(cmdLine): - let key = string(key) + when key isnot string: + let key = string(key) case kind of cmdLongOption, cmdShortOption: processHelpAndVersionOptions key diff --git a/confutils/cli_parser.nim b/confutils/cli_parser.nim index d81e3fe..867ac20 100644 --- a/confutils/cli_parser.nim +++ b/confutils/cli_parser.nim @@ -55,14 +55,14 @@ func handleShortOption(p: var OptParser; cmd: string) = var i = p.pos p.kind = cmdShortOption if i < cmd.len: - add(p.key.string, cmd[i]) + add(p.key, cmd[i]) inc(i) p.inShortState = true while i < cmd.len and cmd[i] in {'\t', ' '}: inc(i) p.inShortState = false if i < cmd.len and cmd[i] in {':', '='} or - card(p.shortNoVal) > 0 and p.key.string[0] notin p.shortNoVal: + card(p.shortNoVal) > 0 and p.key[0] notin p.shortNoVal: if i < cmd.len and cmd[i] in {':', '='}: inc(i) p.inShortState = false diff --git a/confutils/config_file.nim b/confutils/config_file.nim index 8a62af1..f9ee277 100644 --- a/confutils/config_file.nim +++ b/confutils/config_file.nim @@ -1,6 +1,10 @@ import - std/[tables, macrocache, typetraits], - stew/shims/macros, + std/[tables, macrocache], + stew/shims/macros + +{.warning[UnusedImport]:off.} +import + std/typetraits, ./defs #[ @@ -312,7 +316,7 @@ proc generateConfigFileSetters(confType, optType: NimNode, data*: seq[`optT`] setters: array[`numSetters`, `SetterProcType`] - proc defaultConfigFileSetter(s: var `T`, cf: ref `CF`): bool {.nimcall, gcsafe.} = + proc defaultConfigFileSetter(s: var `T`, cf: ref `CF`): bool {.nimcall, gcsafe, used.} = discard `setterProcs` diff --git a/confutils/std/net.nim b/confutils/std/net.nim index c449624..a1d1a5e 100644 --- a/confutils/std/net.nim +++ b/confutils/std/net.nim @@ -5,7 +5,7 @@ export stewNet export ValidIpAddress func parseCmdArg*(T: type ValidIpAddress, s: string): T = - ValidIpAddress.init(string s) + ValidIpAddress.init(s) func completeCmdArg*(T: type ValidIpAddress, val: string): seq[string] = # TODO: Maybe complete the local IP address? @@ -17,7 +17,7 @@ func parseCmdArg*(T: type Port, s: string): T = "The supplied port must be an integer value in the range 1-65535") var intVal: int - let parsedChars = try: parseInt(string s, intVal): + let parsedChars = try: parseInt(s, intVal): except CatchableError: fail() if parsedChars != len(s) or intVal < 1 or intVal > 65535: diff --git a/confutils/winreg/reader.nim b/confutils/winreg/reader.nim index bad0db4..fb9aea7 100644 --- a/confutils/winreg/reader.nim +++ b/confutils/winreg/reader.nim @@ -1,6 +1,6 @@ import tables, typetraits, options, - serialization/[object_serialization, errors], + serialization/[object_serialization], ./utils, ./types type diff --git a/tests/test_config_file.nim b/tests/test_config_file.nim index 2e9c41c..52d9666 100644 --- a/tests/test_config_file.nim +++ b/tests/test_config_file.nim @@ -183,13 +183,13 @@ proc readValue(r: var WinregReader, type T = type value value = r.readValue(string).T -proc readValue(r: var WinregReader, value: var ValidIpAddress) = +proc readValue(r: var WinregReader, value: var ValidIpAddress) {.used.} = value = ValidIpAddress.init(r.readValue(string)) -proc readValue(r: var WinregReader, value: var Port) = +proc readValue(r: var WinregReader, value: var Port) {.used.} = value = r.readValue(int).Port -proc readValue(r: var WinregReader, value: var GraffitiBytes) = +proc readValue(r: var WinregReader, value: var GraffitiBytes) {.used.} = value = hexToByteArray[value.len](r.readValue(string)) proc testConfigFile() = diff --git a/tests/test_parsecmdarg.nim b/tests/test_parsecmdarg.nim index 4961253..0980f1b 100644 --- a/tests/test_parsecmdarg.nim +++ b/tests/test_parsecmdarg.nim @@ -9,6 +9,9 @@ import std/[sequtils], unittest2, ../confutils + +{.warning[UnusedImport]:off.} +import stew/shims/stddefects func testValidValues[T](lo: T = low(T), hi: T = high(T)): bool = allIt(lo .. hi, T.parseCmdArg($it) == it) @@ -24,7 +27,7 @@ func testInvalidValues[T](lo, hi: int64): bool = else: discard it != T.parseCmdArg($it).int64 false - except RangeError: + except RangeDefect: true) const span = 300000 diff --git a/tests/test_qualified_ident.nim b/tests/test_qualified_ident.nim index 67f2e5f..56d973a 100644 --- a/tests/test_qualified_ident.nim +++ b/tests/test_qualified_ident.nim @@ -15,7 +15,7 @@ type name: "la2" }: specialint.SInt func parseCmdArg(T: type specialint.SInt, p: string): T = - parseInt(string p).T + parseInt(p).T func completeCmdArg(T: type specialint.SInt, val: string): seq[string] = @[]