mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 01:14:41 +00:00
Minor std/strscans improvements (#24566)
#### Removes UnInit warnings when using `scanTuple`
e.g. this would emit a warning
```nim
import std/strscans
proc main() =
let (ok, number) = "123".scanTuple()
```

#### Error for wrong type now points to the passed in variable
```nim
import std/strscans
var str: string
discard "123".scanf("$i", str)
```
it gave this warning before

now it returns

(cherry picked from commit 5b9ff963c5)
This commit is contained in:
@@ -324,7 +324,7 @@ macro scanf*(input: string; pattern: static[string]; results: varargs[typed]): b
|
||||
template at(s: string; i: int): char = (if i < s.len: s[i] else: '\0')
|
||||
template matchError() =
|
||||
error("type mismatch between pattern '$" & pattern[p] & "' (position: " & $p &
|
||||
") and " & $getTypeInst(results[i]) & " var '" & repr(results[i]) & "'")
|
||||
") and " & $getTypeInst(results[i]) & " var '" & repr(results[i]) & "'", results[i])
|
||||
|
||||
var i = 0
|
||||
var p = 0
|
||||
@@ -490,7 +490,7 @@ macro scanTuple*(input: untyped; pattern: static[string]; matcherTypes: varargs[
|
||||
result = newStmtList()
|
||||
template addVar(typ: string) =
|
||||
let varIdent = ident("temp" & $arguments.len)
|
||||
result.add(newNimNode(nnkVarSection).add(newIdentDefs(varIdent, ident(typ), newEmptyNode())))
|
||||
result.add(newVarStmt(varIdent, newCall(ident"default", ident(typ))))
|
||||
arguments.add(varIdent)
|
||||
while p < pattern.len:
|
||||
if pattern[p] == '$':
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
discard """
|
||||
errormsg: "type mismatch between pattern '$$i' (position: 1) and HourRange var 'hour'"
|
||||
file: "strscans.nim"
|
||||
file: "t8925.nim"
|
||||
"""
|
||||
|
||||
import strscans
|
||||
|
||||
11
tests/stdlib/tstrscans_errs.nim
Normal file
11
tests/stdlib/tstrscans_errs.nim
Normal file
@@ -0,0 +1,11 @@
|
||||
discard """
|
||||
cmd: "nim check $file"
|
||||
"""
|
||||
|
||||
import std/strscans
|
||||
|
||||
|
||||
block:
|
||||
var strVar: string
|
||||
discard "123".scanf("$i", strVar) #[tt.Error
|
||||
^ type mismatch between pattern '$$i' (position: 1) and string var 'strVar']#
|
||||
Reference in New Issue
Block a user