reduce compiler warnings

This commit is contained in:
jangko 2023-02-15 15:26:53 +07:00
parent a1f3b22bbe
commit 38dfeaaabd
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
8 changed files with 48 additions and 24 deletions

View File

@ -465,7 +465,7 @@ else:
# TODO remove the overloads here to get better "missing overload" error message # TODO remove the overloads here to get better "missing overload" error message
proc parseCmdArg*(T: type InputDir, p: string): T = proc parseCmdArg*(T: type InputDir, p: string): T =
if not dirExists(p.string): if not dirExists(p):
raise newException(ValueError, "Directory doesn't exist") raise newException(ValueError, "Directory doesn't exist")
T(p) T(p)
@ -474,17 +474,17 @@ proc parseCmdArg*(T: type InputFile, p: string): T =
# TODO this is needed only because InputFile cannot be made # TODO this is needed only because InputFile cannot be made
# an alias of TypedInputFile at the moment, because of a generics # an alias of TypedInputFile at the moment, because of a generics
# caching issue # caching issue
if not fileExists(p.string): if not fileExists(p):
raise newException(ValueError, "File doesn't exist") raise newException(ValueError, "File doesn't exist")
when not defined(nimscript): when not defined(nimscript):
try: try:
let f = system.open(p.string, fmRead) let f = system.open(p, fmRead)
close f close f
except IOError: except IOError:
raise newException(ValueError, "File not accessible") raise newException(ValueError, "File not accessible")
T(p.string) T(p)
proc parseCmdArg*(T: type TypedInputFile, p: string): T = proc parseCmdArg*(T: type TypedInputFile, p: string): T =
var path = p var path = p
@ -513,10 +513,10 @@ template parseCmdArg*(T: type string, s: string): string =
s s
func parseCmdArg*(T: type SomeSignedInt, s: string): T = func parseCmdArg*(T: type SomeSignedInt, s: string): T =
T parseBiggestInt(string s) T parseBiggestInt(s)
func parseCmdArg*(T: type SomeUnsignedInt, s: string): T = func parseCmdArg*(T: type SomeUnsignedInt, s: string): T =
T parseBiggestUInt(string s) T parseBiggestUInt(s)
func parseCmdArg*(T: type SomeFloat, p: string): T = func parseCmdArg*(T: type SomeFloat, p: string): T =
parseFloat(p) parseFloat(p)
@ -525,10 +525,10 @@ func parseCmdArg*(T: type bool, p: string): T =
try: try:
p.len == 0 or parseBool(p) p.len == 0 or parseBool(p)
except CatchableError: 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 = func parseCmdArg*(T: type enum, s: string): T =
parseEnum[T](string(s)) parseEnum[T](s)
proc parseCmdArgAux(T: type, s: string): T = # {.raises: [ValueError].} = proc parseCmdArgAux(T: type, s: string): T = # {.raises: [ValueError].} =
# The parseCmdArg procs are allowed to raise only `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"requiresInput", fixedFieldType),
newCall(bindSym"acceptsMultipleValues", 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: result.add quote do:
proc `completerName`(val: string): seq[string] {. proc `completerName`(val: string): seq[string] {.
nimcall nimcall
@ -681,12 +688,15 @@ proc generateFieldSetters(RecordType: NimNode): NimNode =
# TODO: For some reason, the normal `setField` rejects enum fields # TODO: For some reason, the normal `setField` rejects enum fields
# when they are used as case discriminators. File this as a bug. # when they are used as case discriminators. File this as a bug.
if isSome(val): if isSome(val):
`configField` = parseEnumNormalized[type(`configField`)](string(val.get)) `configField` = parseEnumNormalized[type(`configField`)](val.get)
else: else:
`configField` = `defaultValue` `configField` = `defaultValue`
else: else:
setField(`configField`, val, `defaultValue`) setField(`configField`, val, `defaultValue`)
result.add quote do:
{.pop.}
result.add settersArray result.add settersArray
debugMacroResult "Field Setters" debugMacroResult "Field Setters"
@ -891,15 +901,21 @@ proc loadImpl[C, SecondarySources](
let confAddr = addr result let confAddr = addr result
template applySetter(setterIdx: int, cmdLineVal: string) = template applySetter(setterIdx: int, cmdLineVal: string) =
when (NimMajor, NimMinor) >= (1, 6):
{.warning[BareExcept]:off.}
try: try:
fieldSetters[setterIdx][1](confAddr[], some(cmdLineVal)) fieldSetters[setterIdx][1](confAddr[], some(cmdLineVal))
inc fieldCounters[setterIdx] inc fieldCounters[setterIdx]
except: except:
fail("Error while processing the ", fail("Error while processing the ",
fgOption, fieldSetters[setterIdx][0], fgOption, fieldSetters[setterIdx][0],
"=", cmdLineVal.string, resetStyle, " parameter: ", "=", cmdLineVal, resetStyle, " parameter: ",
getCurrentExceptionMsg()) getCurrentExceptionMsg())
when (NimMajor, NimMinor) >= (1, 6):
{.warning[BareExcept]:on.}
when hasCompletions: when hasCompletions:
template getArgCompletions(opt: OptInfo, prefix: string): seq[string] = template getArgCompletions(opt: OptInfo, prefix: string): seq[string] =
fieldSetters[opt.idx][2](prefix) fieldSetters[opt.idx][2](prefix)
@ -1017,7 +1033,8 @@ proc loadImpl[C, SecondarySources](
flushOutputAndQuit QuitSuccess flushOutputAndQuit QuitSuccess
for kind, key, val in getopt(cmdLine): for kind, key, val in getopt(cmdLine):
let key = string(key) when key isnot string:
let key = string(key)
case kind case kind
of cmdLongOption, cmdShortOption: of cmdLongOption, cmdShortOption:
processHelpAndVersionOptions key processHelpAndVersionOptions key

View File

@ -55,14 +55,14 @@ func handleShortOption(p: var OptParser; cmd: string) =
var i = p.pos var i = p.pos
p.kind = cmdShortOption p.kind = cmdShortOption
if i < cmd.len: if i < cmd.len:
add(p.key.string, cmd[i]) add(p.key, cmd[i])
inc(i) inc(i)
p.inShortState = true p.inShortState = true
while i < cmd.len and cmd[i] in {'\t', ' '}: while i < cmd.len and cmd[i] in {'\t', ' '}:
inc(i) inc(i)
p.inShortState = false p.inShortState = false
if i < cmd.len and cmd[i] in {':', '='} or 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 {':', '='}: if i < cmd.len and cmd[i] in {':', '='}:
inc(i) inc(i)
p.inShortState = false p.inShortState = false

View File

@ -1,6 +1,10 @@
import import
std/[tables, macrocache, typetraits], std/[tables, macrocache],
stew/shims/macros, stew/shims/macros
{.warning[UnusedImport]:off.}
import
std/typetraits,
./defs ./defs
#[ #[
@ -312,7 +316,7 @@ proc generateConfigFileSetters(confType, optType: NimNode,
data*: seq[`optT`] data*: seq[`optT`]
setters: array[`numSetters`, `SetterProcType`] 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 discard
`setterProcs` `setterProcs`

View File

@ -5,7 +5,7 @@ export stewNet
export ValidIpAddress export ValidIpAddress
func parseCmdArg*(T: type ValidIpAddress, s: string): T = func parseCmdArg*(T: type ValidIpAddress, s: string): T =
ValidIpAddress.init(string s) ValidIpAddress.init(s)
func completeCmdArg*(T: type ValidIpAddress, val: string): seq[string] = func completeCmdArg*(T: type ValidIpAddress, val: string): seq[string] =
# TODO: Maybe complete the local IP address? # 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") "The supplied port must be an integer value in the range 1-65535")
var intVal: int var intVal: int
let parsedChars = try: parseInt(string s, intVal): let parsedChars = try: parseInt(s, intVal):
except CatchableError: fail() except CatchableError: fail()
if parsedChars != len(s) or intVal < 1 or intVal > 65535: if parsedChars != len(s) or intVal < 1 or intVal > 65535:

View File

@ -1,6 +1,6 @@
import import
tables, typetraits, options, tables, typetraits, options,
serialization/[object_serialization, errors], serialization/[object_serialization],
./utils, ./types ./utils, ./types
type type

View File

@ -183,13 +183,13 @@ proc readValue(r: var WinregReader,
type T = type value type T = type value
value = r.readValue(string).T 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)) 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 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)) value = hexToByteArray[value.len](r.readValue(string))
proc testConfigFile() = proc testConfigFile() =

View File

@ -9,6 +9,9 @@ import
std/[sequtils], std/[sequtils],
unittest2, unittest2,
../confutils ../confutils
{.warning[UnusedImport]:off.}
import stew/shims/stddefects
func testValidValues[T](lo: T = low(T), hi: T = high(T)): bool = func testValidValues[T](lo: T = low(T), hi: T = high(T)): bool =
allIt(lo .. hi, T.parseCmdArg($it) == it) allIt(lo .. hi, T.parseCmdArg($it) == it)
@ -24,7 +27,7 @@ func testInvalidValues[T](lo, hi: int64): bool =
else: else:
discard it != T.parseCmdArg($it).int64 discard it != T.parseCmdArg($it).int64
false false
except RangeError: except RangeDefect:
true) true)
const span = 300000 const span = 300000

View File

@ -15,7 +15,7 @@ type
name: "la2" }: specialint.SInt name: "la2" }: specialint.SInt
func parseCmdArg(T: type specialint.SInt, p: string): T = 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] = func completeCmdArg(T: type specialint.SInt, val: string): seq[string] =
@[] @[]