1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix error pos on default value (#6338)

This commit is contained in:
Daniel Däschle 2020-09-09 15:34:41 +02:00 committed by GitHub
parent 78e28a72ed
commit 3a146a6dbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 56 additions and 43 deletions

View File

@ -96,14 +96,14 @@ enum OutputType {
struct DocConfig { struct DocConfig {
mut: mut:
pub_only bool = true pub_only bool = true
show_loc bool = false // for plaintext show_loc bool // for plaintext
serve_http bool = false // for html serve_http bool // for html
is_multi bool = false is_multi bool
is_verbose bool = false is_verbose bool
include_readme bool = false include_readme bool
open_docs bool = false open_docs bool
server_port int = 8046 server_port int = 8046
inline_assets bool = false inline_assets bool
output_path string output_path string
input_path string input_path string
output_type OutputType = .unset output_type OutputType = .unset

View File

@ -5,7 +5,7 @@ import os
pub struct DocumentObjectModel { pub struct DocumentObjectModel {
mut: mut:
root &Tag root &Tag
constructed bool = false constructed bool
btree BTree btree BTree
all_tags []&Tag all_tags []&Tag
all_attributes map[string][]&Tag all_attributes map[string][]&Tag

View File

@ -5,11 +5,11 @@ import os
struct LexycalAttributes { struct LexycalAttributes {
mut: mut:
current_tag &Tag current_tag &Tag
open_tag bool = false open_tag bool
open_code bool = false open_code bool
open_string int open_string int
open_comment bool = false open_comment bool
is_attribute bool = false is_attribute bool
opened_code_type string opened_code_type string
line_count int line_count int
lexeme_builder string lexeme_builder string
@ -32,7 +32,7 @@ mut:
current_tag: &Tag{} current_tag: &Tag{}
} }
filename string = 'direct-parse' filename string = 'direct-parse'
initialized bool = false initialized bool
tags []&Tag tags []&Tag
debug_file os.File debug_file os.File
} }

View File

@ -16,7 +16,7 @@ mut:
last_attribute string last_attribute string
parent &Tag = C.NULL parent &Tag = C.NULL
position_in_parent int position_in_parent int
closed bool = false closed bool
close_type CloseTagType = .in_name close_type CloseTagType = .in_name
} }

View File

@ -35,7 +35,7 @@ pub mut:
headers map[string]string headers map[string]string
cookies map[string]string cookies map[string]string
user_agent string = 'v.http' user_agent string = 'v.http'
verbose bool = false verbose bool
} }
pub struct Response { pub struct Response {

View File

@ -62,7 +62,7 @@ mut:
state []u64 = calculate_state(util.time_seed_array(2), mut []u64{len: nn}) state []u64 = calculate_state(util.time_seed_array(2), mut []u64{len: nn})
mti int = nn mti int = nn
next_rnd u32 next_rnd u32
has_next bool = false has_next bool
} }
fn calculate_state(seed_data []u32, mut state []u64) []u64 { fn calculate_state(seed_data []u32, mut state []u64) []u64 {

View File

@ -9,7 +9,7 @@ import rand.util
pub struct SplitMix64RNG { pub struct SplitMix64RNG {
mut: mut:
state u64 = util.time_seed_64() state u64 = util.time_seed_64()
has_extra bool = false has_extra bool
extra u32 extra u32
} }

View File

@ -17,7 +17,7 @@ const (
pub struct WyRandRNG { pub struct WyRandRNG {
mut: mut:
state u64 = util.time_seed_64() state u64 = util.time_seed_64()
has_extra bool = false has_extra bool
extra u32 extra u32
} }

View File

@ -1482,7 +1482,7 @@ fn state_str(s Match_state) string {
struct StateObj { struct StateObj {
pub mut: pub mut:
match_flag bool = false match_flag bool
match_index int = -1 match_index int = -1
match_first int = -1 match_first int = -1
} }

View File

@ -207,9 +207,9 @@ pub struct BF_param {
len0 int = -1 // default len for whole the number or string len0 int = -1 // default len for whole the number or string
len1 int = 6 // number of decimal digits, if needed len1 int = 6 // number of decimal digits, if needed
positive bool = true // mandatory: the sign of the number passed 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 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 { pub fn format_str(s string, p BF_param) string {

View File

@ -368,15 +368,20 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
if field.typ.is_ptr() { if field.typ.is_ptr() {
continue continue
} }
if field.default_expr is ast.IntegerLiteral as x { if field.default_expr is ast.IntegerLiteral as lit {
if x.val == '0' { if lit.val == '0' {
c.error('unnecessary default value of `0`: struct fields are zeroed by default', 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 { } else if field.default_expr is ast.StringLiteral as lit {
if x.val == '' { if lit.val == '' {
c.error("unnecessary default value of '': struct fields are zeroed by default", 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)
} }
} }
} }

View File

@ -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 { 1 | struct Test {
2 | n int = 0 2 | n int = 0
| ~~~~~~ | ^
3 | s string = '' 3 | s string = ''
4 | } 4 | b bool = false
vlib/v/checker/tests/struct_unneeded_default.vv:3:2: error: unnecessary default value of '': struct fields are zeroed by default vlib/v/checker/tests/struct_unneeded_default.vv:3:13: error: unnecessary default value of '': struct fields are zeroed by default
1 | struct Test { 1 | struct Test {
2 | n int = 0 2 | n int = 0
3 | s string = '' 3 | s string = ''
| ~~~~~~~~~ | ~~
4 | } 4 | b bool = false
5 | 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 |

View File

@ -1,6 +1,7 @@
struct Test { struct Test {
n int = 0 n int = 0
s string = '' s string = ''
b bool = false
} }
fn main() { fn main() {

View File

@ -44,7 +44,7 @@ pub mut:
is_fmt bool // Used only for skipping ${} in strings, since we need literal is_fmt bool // Used only for skipping ${} in strings, since we need literal
// string values when generating formatted code. // string values when generating formatted code.
comments_mode CommentsMode 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 all_tokens []token.Token // *only* used in comments_mode: .toplevel_comments, contains all tokens
tidx int tidx int
eofs 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 // fix line_nr, \n was read; the comment is marked on the next line
s.pos-- s.pos--
s.line_nr-- s.line_nr--
} }
if s.should_parse_comment() { if s.should_parse_comment() {
s.line_comment = s.text[start + 1..comment_line_end] s.line_comment = s.text[start + 1..comment_line_end]
mut comment := s.line_comment.trim_space() mut comment := s.line_comment.trim_space()

View File

@ -4,28 +4,28 @@ module websocket
struct MessageEventHandler { struct MessageEventHandler {
handler SocketMessageFn handler SocketMessageFn
handler2 SocketMessageFn2 handler2 SocketMessageFn2
is_ref bool = false is_ref bool
ref voidptr ref voidptr
} }
struct ErrorEventHandler { struct ErrorEventHandler {
handler SocketErrorFn handler SocketErrorFn
handler2 SocketErrorFn2 handler2 SocketErrorFn2
is_ref bool = false is_ref bool
ref voidptr ref voidptr
} }
struct OpenEventHandler { struct OpenEventHandler {
handler SocketOpenFn handler SocketOpenFn
handler2 SocketOpenFn2 handler2 SocketOpenFn2
is_ref bool = false is_ref bool
ref voidptr ref voidptr
} }
struct CloseEventHandler { struct CloseEventHandler {
handler SocketCloseFn handler SocketCloseFn
handler2 SocketCloseFn2 handler2 SocketCloseFn2
is_ref bool = false is_ref bool
ref voidptr ref voidptr
} }

View File

@ -21,7 +21,7 @@ const (
// Client represents websocket client state // Client represents websocket client state
pub struct Client { pub struct Client {
is_server bool = false is_server bool
mut: mut:
ssl_conn &openssl.SSLConn ssl_conn &openssl.SSLConn
flags []Flag flags []Flag
@ -38,7 +38,7 @@ pub:
pub mut: pub mut:
conn net.TcpConn conn net.TcpConn
nonce_size int = 16 // you can try 18 too nonce_size int = 16 // you can try 18 too
panic_on_callback bool = false panic_on_callback bool
state State state State
resource_name string resource_name string
last_pong_ut u64 last_pong_ut u64

View File

@ -18,7 +18,7 @@ mut:
close_callbacks []CloseEventHandler close_callbacks []CloseEventHandler
pub: pub:
port int port int
is_ssl bool = false is_ssl bool
pub mut: pub mut:
ping_interval int = 30 // in seconds ping_interval int = 30 // in seconds
state State state State