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
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

View File

@ -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

View File

@ -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`

View File

@ -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:

View File

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

View File

@ -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() =

View File

@ -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

View File

@ -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] =
@[]