From 49155ec3129ac08a062cd95242776391d5ca30d5 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 15 Mar 2022 22:11:53 +0800 Subject: [PATCH] parser: notice unnecessary parenthesis of reference (fix #11179) (#13739) --- examples/ttf_font/example_ttf.v | 6 +- vlib/builtin/string_interpolation.v | 2 +- .../unnecessary_parenthesis_of_reference.out | 35 ++++++++++ .../unnecessary_parenthesis_of_reference.vv | 69 +++++++++++++++++++ vlib/v/parser/expr.v | 7 ++ vlib/x/ttf/README.md | 2 +- 6 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 vlib/v/checker/tests/unnecessary_parenthesis_of_reference.out create mode 100644 vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv diff --git a/examples/ttf_font/example_ttf.v b/examples/ttf_font/example_ttf.v index d22ea84e62..2edb417f20 100644 --- a/examples/ttf_font/example_ttf.v +++ b/examples/ttf_font/example_ttf.v @@ -144,7 +144,7 @@ fn main() { // TTF render 0 Frame counter app.ttf_render << &ttf.TTF_render_Sokol{ bmp: &ttf.BitMap{ - tf: &(app.tf[0]) + tf: &app.tf[0] buf: unsafe { malloc_noscan(32000000) } buf_size: (32000000) color: 0xFF0000FF @@ -155,7 +155,7 @@ fn main() { // TTF render 1 Text Block app.ttf_render << &ttf.TTF_render_Sokol{ bmp: &ttf.BitMap{ - tf: &(app.tf[1]) + tf: &app.tf[1] // color : 0xFF0000_10 // style: .raw // use_font_metrics: true @@ -164,7 +164,7 @@ fn main() { // TTF mouse position render app.ttf_render << &ttf.TTF_render_Sokol{ bmp: &ttf.BitMap{ - tf: &(app.tf[0]) + tf: &app.tf[0] } } // setup sokol_gfx diff --git a/vlib/builtin/string_interpolation.v b/vlib/builtin/string_interpolation.v index ccba92f25d..53c965d7fb 100644 --- a/vlib/builtin/string_interpolation.v +++ b/vlib/builtin/string_interpolation.v @@ -660,7 +660,7 @@ pub fn str_intp(data_len int, in_data voidptr) string { mut res := strings.new_builder(256) input_base := &StrIntpData(in_data) for i := 0; i < data_len; i++ { - data := unsafe { &(input_base[i]) } + data := unsafe { &input_base[i] } // avoid empty strings if data.str.len != 0 { res.write_string(data.str) diff --git a/vlib/v/checker/tests/unnecessary_parenthesis_of_reference.out b/vlib/v/checker/tests/unnecessary_parenthesis_of_reference.out new file mode 100644 index 0000000000..3aa279de02 --- /dev/null +++ b/vlib/v/checker/tests/unnecessary_parenthesis_of_reference.out @@ -0,0 +1,35 @@ +vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv:29:10: notice: unnecessary `()`, use `&Quad{....}` instead of `&(Quad{....})` + 27 | // ritorna una nuova Quadrica somma del ricevente e di un'altra + 28 | fn (q &Quad) add(other &Quad) &Quad { + 29 | return &(Quad{q.x + other.x, q.y + other.y, q.z + other.z, q.w + other.w}) + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 30 | } + 31 | +vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv:34:10: notice: unnecessary `()`, use `&Quad{....}` instead of `&(Quad{....})` + 32 | // ritorna una nuova Quadrica differenza tra il ricevente e un'altra + 33 | fn (q &Quad) sub(other &Quad) &Quad { + 34 | return &(Quad{q.x - other.x, q.y - other.y, q.z - other.z, q.w - other.w}) + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 35 | } + 36 | +vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv:39:10: notice: unnecessary `()`, use `&Quad{....}` instead of `&(Quad{....})` + 37 | // ritorna una nuova Quadrica ottenuta negando il ricevente + 38 | fn (q &Quad) neg() &Quad { + 39 | return &(Quad{-q.x, -q.y, -q.z, -q.w}) + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 40 | } + 41 | +vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv:44:10: notice: unnecessary `()`, use `&Quad{....}` instead of `&(Quad{....})` + 42 | // ritorna una nuova Quadrica ottenuta moltiplicando il ricevente per una costante + 43 | fn (q &Quad) mult(factor f64) &Quad { + 44 | return &(Quad{q.x * factor, q.y * factor, q.z * factor, q.w * factor}) + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 45 | } + 46 | +vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv:49:10: notice: unnecessary `()`, use `&Quad{....}` instead of `&(Quad{....})` + 47 | // ritorna una nuova Quadrica ottenuta dividendo il ricevente per una costante + 48 | fn (q &Quad) div(factor f64) &Quad { + 49 | return &(Quad{q.x / factor, q.y / factor, q.z / factor, q.w / factor}) + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 50 | } + 51 | diff --git a/vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv b/vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv new file mode 100644 index 0000000000..1fe1a36986 --- /dev/null +++ b/vlib/v/checker/tests/unnecessary_parenthesis_of_reference.vv @@ -0,0 +1,69 @@ +struct Quad { +mut: + x f64 + y f64 + z f64 + w f64 +} + +fn (q Quad) get(i int) f64 { + return match i { + 0 { q.x } + 1 { q.y } + 2 { q.z } + else { q.w } + } +} + +fn (mut q Quad) set(i int, v f64) { + match i { + 0 { q.x = v } + 1 { q.y = v } + 2 { q.z = v } + else { q.w = v } + } +} + +// ritorna una nuova Quadrica somma del ricevente e di un'altra +fn (q &Quad) add(other &Quad) &Quad { + return &(Quad{q.x + other.x, q.y + other.y, q.z + other.z, q.w + other.w}) +} + +// ritorna una nuova Quadrica differenza tra il ricevente e un'altra +fn (q &Quad) sub(other &Quad) &Quad { + return &(Quad{q.x - other.x, q.y - other.y, q.z - other.z, q.w - other.w}) +} + +// ritorna una nuova Quadrica ottenuta negando il ricevente +fn (q &Quad) neg() &Quad { + return &(Quad{-q.x, -q.y, -q.z, -q.w}) +} + +// ritorna una nuova Quadrica ottenuta moltiplicando il ricevente per una costante +fn (q &Quad) mult(factor f64) &Quad { + return &(Quad{q.x * factor, q.y * factor, q.z * factor, q.w * factor}) +} + +// ritorna una nuova Quadrica ottenuta dividendo il ricevente per una costante +fn (q &Quad) div(factor f64) &Quad { + return &(Quad{q.x / factor, q.y / factor, q.z / factor, q.w / factor}) +} + +fn main() { + mut n := Quad{1, 2, 3, 4} + + println(n) + println(n.get(0)) + println(n.get(1)) + println(n.get(2)) + println(n.get(3)) + n.set(0, 5) + n.set(1, 6) + n.set(2, 7) + n.set(3, 8) + println(n) + println(n.get(0)) + println(n.get(1)) + println(n.get(2)) + println(n.get(3)) +} diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 92cfa0074c..6b5d793c99 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -604,6 +604,13 @@ fn (mut p Parser) prefix_expr() ast.Expr { return right } } + if mut right is ast.ParExpr { + if right.expr is ast.StructInit { + p.note_with_pos('unnecessary `()`, use `&$right.expr` instead of `&($right.expr)`', + right.pos) + right = right.expr + } + } } mut or_stmts := []ast.Stmt{} mut or_kind := ast.OrKind.absent diff --git a/vlib/x/ttf/README.md b/vlib/x/ttf/README.md index 811f281a55..2a1062332c 100644 --- a/vlib/x/ttf/README.md +++ b/vlib/x/ttf/README.md @@ -298,7 +298,7 @@ fn main() { // TTF render 0 Frame counter app.ttf_render << &ttf.TTF_render_Sokol{ bmp: &ttf.BitMap{ - tf: &(app.tf[0]) + tf: &app.tf[0] buf: unsafe { malloc(32000000) } buf_size: (32000000) color: 0xFF0000FF