From 3a146a6dbe7d82716e78202b97fd3a4fe15181a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 9 Sep 2020 15:34:41 +0200 Subject: [PATCH] checker: fix error pos on default value (#6338) --- cmd/tools/vdoc.v | 14 ++++++------- vlib/net/html/dom.v | 2 +- vlib/net/html/parser.v | 10 ++++----- vlib/net/html/tag.v | 2 +- vlib/net/http/http.v | 2 +- vlib/rand/mt19937/mt19937.v | 2 +- vlib/rand/splitmix64/splitmix64.v | 2 +- vlib/rand/wyrand/wyrand.v | 2 +- vlib/regex/regex.v | 2 +- vlib/strconv/format.v | 4 ++-- vlib/v/checker/checker.v | 17 +++++++++------ .../checker/tests/struct_unneeded_default.out | 21 ++++++++++++------- .../checker/tests/struct_unneeded_default.vv | 1 + vlib/v/scanner/scanner.v | 4 ++-- vlib/x/websocket/events.v | 8 +++---- vlib/x/websocket/websocket_client.v | 4 ++-- vlib/x/websocket/websocket_server.v | 2 +- 17 files changed, 56 insertions(+), 43 deletions(-) diff --git a/cmd/tools/vdoc.v b/cmd/tools/vdoc.v index f3fe22c965..a567ff10e9 100644 --- a/cmd/tools/vdoc.v +++ b/cmd/tools/vdoc.v @@ -96,14 +96,14 @@ enum OutputType { struct DocConfig { mut: pub_only bool = true - show_loc bool = false // for plaintext - serve_http bool = false // for html - is_multi bool = false - is_verbose bool = false - include_readme bool = false - open_docs bool = false + show_loc bool // for plaintext + serve_http bool // for html + is_multi bool + is_verbose bool + include_readme bool + open_docs bool server_port int = 8046 - inline_assets bool = false + inline_assets bool output_path string input_path string output_type OutputType = .unset diff --git a/vlib/net/html/dom.v b/vlib/net/html/dom.v index 1f2cdeaa46..2708654391 100644 --- a/vlib/net/html/dom.v +++ b/vlib/net/html/dom.v @@ -5,7 +5,7 @@ import os pub struct DocumentObjectModel { mut: root &Tag - constructed bool = false + constructed bool btree BTree all_tags []&Tag all_attributes map[string][]&Tag diff --git a/vlib/net/html/parser.v b/vlib/net/html/parser.v index 8041ae13d8..44a3939ca3 100644 --- a/vlib/net/html/parser.v +++ b/vlib/net/html/parser.v @@ -5,11 +5,11 @@ import os struct LexycalAttributes { mut: current_tag &Tag - open_tag bool = false - open_code bool = false + open_tag bool + open_code bool open_string int - open_comment bool = false - is_attribute bool = false + open_comment bool + is_attribute bool opened_code_type string line_count int lexeme_builder string @@ -32,7 +32,7 @@ mut: current_tag: &Tag{} } filename string = 'direct-parse' - initialized bool = false + initialized bool tags []&Tag debug_file os.File } diff --git a/vlib/net/html/tag.v b/vlib/net/html/tag.v index 485b5fee79..1961e603cc 100644 --- a/vlib/net/html/tag.v +++ b/vlib/net/html/tag.v @@ -16,7 +16,7 @@ mut: last_attribute string parent &Tag = C.NULL position_in_parent int - closed bool = false + closed bool close_type CloseTagType = .in_name } diff --git a/vlib/net/http/http.v b/vlib/net/http/http.v index 13023c94b6..e2b84349e8 100644 --- a/vlib/net/http/http.v +++ b/vlib/net/http/http.v @@ -35,7 +35,7 @@ pub mut: headers map[string]string cookies map[string]string user_agent string = 'v.http' - verbose bool = false + verbose bool } pub struct Response { diff --git a/vlib/rand/mt19937/mt19937.v b/vlib/rand/mt19937/mt19937.v index 2c8bea0708..4feedb905b 100644 --- a/vlib/rand/mt19937/mt19937.v +++ b/vlib/rand/mt19937/mt19937.v @@ -62,7 +62,7 @@ mut: state []u64 = calculate_state(util.time_seed_array(2), mut []u64{len: nn}) mti int = nn next_rnd u32 - has_next bool = false + has_next bool } fn calculate_state(seed_data []u32, mut state []u64) []u64 { diff --git a/vlib/rand/splitmix64/splitmix64.v b/vlib/rand/splitmix64/splitmix64.v index 4f1956b794..601bbe5422 100644 --- a/vlib/rand/splitmix64/splitmix64.v +++ b/vlib/rand/splitmix64/splitmix64.v @@ -9,7 +9,7 @@ import rand.util pub struct SplitMix64RNG { mut: state u64 = util.time_seed_64() - has_extra bool = false + has_extra bool extra u32 } diff --git a/vlib/rand/wyrand/wyrand.v b/vlib/rand/wyrand/wyrand.v index 25d00eaaf1..4f5abfa9bc 100644 --- a/vlib/rand/wyrand/wyrand.v +++ b/vlib/rand/wyrand/wyrand.v @@ -17,7 +17,7 @@ const ( pub struct WyRandRNG { mut: state u64 = util.time_seed_64() - has_extra bool = false + has_extra bool extra u32 } diff --git a/vlib/regex/regex.v b/vlib/regex/regex.v index e733b0630b..221e760880 100644 --- a/vlib/regex/regex.v +++ b/vlib/regex/regex.v @@ -1482,7 +1482,7 @@ fn state_str(s Match_state) string { struct StateObj { pub mut: - match_flag bool = false + match_flag bool match_index int = -1 match_first int = -1 } diff --git a/vlib/strconv/format.v b/vlib/strconv/format.v index 57f43e547a..74288aa524 100644 --- a/vlib/strconv/format.v +++ b/vlib/strconv/format.v @@ -207,9 +207,9 @@ pub struct BF_param { len0 int = -1 // default len for whole the number or string len1 int = 6 // number of decimal digits, if needed positive bool = true // mandatory: the sign of the number passed - sign_flag bool = false // flag for print sign as prefix in padding + sign_flag bool // flag for print sign as prefix in padding allign Align_text = .right // alignment of the string - rm_tail_zero bool = false // remove the tail zeros from floats + rm_tail_zero bool // remove the tail zeros from floats } pub fn format_str(s string, p BF_param) string { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b209aade69..347caa59ad 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -368,15 +368,20 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) { if field.typ.is_ptr() { continue } - if field.default_expr is ast.IntegerLiteral as x { - if x.val == '0' { + if field.default_expr is ast.IntegerLiteral as lit { + if lit.val == '0' { c.error('unnecessary default value of `0`: struct fields are zeroed by default', - field.pos) + lit.pos) } - } else if field.default_expr is ast.StringLiteral as x { - if x.val == '' { + } else if field.default_expr is ast.StringLiteral as lit { + if lit.val == '' { c.error("unnecessary default value of '': struct fields are zeroed by default", - field.pos) + lit.pos) + } + } else if field.default_expr is ast.BoolLiteral as lit { + if lit.val == false { + c.error('unnecessary default value `false`: struct fields are zeroed by default', + lit.pos) } } } diff --git a/vlib/v/checker/tests/struct_unneeded_default.out b/vlib/v/checker/tests/struct_unneeded_default.out index ed5e0d40f5..029f9fe2c3 100644 --- a/vlib/v/checker/tests/struct_unneeded_default.out +++ b/vlib/v/checker/tests/struct_unneeded_default.out @@ -1,13 +1,20 @@ -vlib/v/checker/tests/struct_unneeded_default.vv:2:2: error: unnecessary default value of `0`: struct fields are zeroed by default +vlib/v/checker/tests/struct_unneeded_default.vv:2:10: error: unnecessary default value of `0`: struct fields are zeroed by default 1 | struct Test { 2 | n int = 0 - | ~~~~~~ + | ^ 3 | s string = '' - 4 | } -vlib/v/checker/tests/struct_unneeded_default.vv:3:2: error: unnecessary default value of '': struct fields are zeroed by default + 4 | b bool = false +vlib/v/checker/tests/struct_unneeded_default.vv:3:13: error: unnecessary default value of '': struct fields are zeroed by default 1 | struct Test { 2 | n int = 0 3 | s string = '' - | ~~~~~~~~~ - 4 | } - 5 | + | ~~ + 4 | b bool = false + 5 | } +vlib/v/checker/tests/struct_unneeded_default.vv:4:11: error: unnecessary default value `false`: struct fields are zeroed by default + 2 | n int = 0 + 3 | s string = '' + 4 | b bool = false + | ~~~~~ + 5 | } + 6 | \ No newline at end of file diff --git a/vlib/v/checker/tests/struct_unneeded_default.vv b/vlib/v/checker/tests/struct_unneeded_default.vv index 93f94d0548..7ddc066167 100644 --- a/vlib/v/checker/tests/struct_unneeded_default.vv +++ b/vlib/v/checker/tests/struct_unneeded_default.vv @@ -1,6 +1,7 @@ struct Test { n int = 0 s string = '' + b bool = false } fn main() { diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index b14ce461ac..5e31e6a094 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -44,7 +44,7 @@ pub mut: is_fmt bool // Used only for skipping ${} in strings, since we need literal // string values when generating formatted code. comments_mode CommentsMode - is_inside_toplvl_statement bool = false // *only* used in comments_mode: .toplevel_comments, toggled by parser + is_inside_toplvl_statement bool // *only* used in comments_mode: .toplevel_comments, toggled by parser all_tokens []token.Token // *only* used in comments_mode: .toplevel_comments, contains all tokens tidx int eofs int @@ -1099,7 +1099,7 @@ fn (mut s Scanner) text_scan() token.Token { // fix line_nr, \n was read; the comment is marked on the next line s.pos-- s.line_nr-- - } + } if s.should_parse_comment() { s.line_comment = s.text[start + 1..comment_line_end] mut comment := s.line_comment.trim_space() diff --git a/vlib/x/websocket/events.v b/vlib/x/websocket/events.v index f6560a7900..0052d7b485 100644 --- a/vlib/x/websocket/events.v +++ b/vlib/x/websocket/events.v @@ -4,28 +4,28 @@ module websocket struct MessageEventHandler { handler SocketMessageFn handler2 SocketMessageFn2 - is_ref bool = false + is_ref bool ref voidptr } struct ErrorEventHandler { handler SocketErrorFn handler2 SocketErrorFn2 - is_ref bool = false + is_ref bool ref voidptr } struct OpenEventHandler { handler SocketOpenFn handler2 SocketOpenFn2 - is_ref bool = false + is_ref bool ref voidptr } struct CloseEventHandler { handler SocketCloseFn handler2 SocketCloseFn2 - is_ref bool = false + is_ref bool ref voidptr } diff --git a/vlib/x/websocket/websocket_client.v b/vlib/x/websocket/websocket_client.v index 5435799faa..6a4702f14f 100644 --- a/vlib/x/websocket/websocket_client.v +++ b/vlib/x/websocket/websocket_client.v @@ -21,7 +21,7 @@ const ( // Client represents websocket client state pub struct Client { - is_server bool = false + is_server bool mut: ssl_conn &openssl.SSLConn flags []Flag @@ -38,7 +38,7 @@ pub: pub mut: conn net.TcpConn nonce_size int = 16 // you can try 18 too - panic_on_callback bool = false + panic_on_callback bool state State resource_name string last_pong_ut u64 diff --git a/vlib/x/websocket/websocket_server.v b/vlib/x/websocket/websocket_server.v index e12dead19c..1434ac26a4 100644 --- a/vlib/x/websocket/websocket_server.v +++ b/vlib/x/websocket/websocket_server.v @@ -18,7 +18,7 @@ mut: close_callbacks []CloseEventHandler pub: port int - is_ssl bool = false + is_ssl bool pub mut: ping_interval int = 30 // in seconds state State