checker: check using redundant parentheses (#11228)

pull/11219/head
yuyi 2021-08-18 18:49:50 +08:00 committed by GitHub
parent 09e854c064
commit e07678d6f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View File

@ -3410,8 +3410,8 @@ pub fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
return ast.void_type
}
node.expr_type = typ
if node.expr_type.has_flag(.optional) && !((node.expr is ast.Ident
&& (node.expr as ast.Ident).kind == .constant)) {
if node.expr_type.has_flag(.optional) && !(node.expr is ast.Ident
&& (node.expr as ast.Ident).kind == .constant) {
c.error('cannot access fields of an optional, handle the error with `or {...}` or propagate it with `?`',
node.pos)
}
@ -5435,6 +5435,9 @@ pub fn (mut c Checker) expr(node ast.Expr) ast.Type {
// return node.typ
// }
ast.ParExpr {
if node.expr is ast.ParExpr {
c.warn('redundant parentheses are used', node.pos)
}
return c.expr(node.expr)
}
ast.RangeExpr {
@ -7316,7 +7319,7 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
if node.left.obj is ast.Var {
v := node.left.obj as ast.Var
// `mut param []T` function parameter
is_ok = ((v.is_mut && v.is_arg)) && !typ.deref().is_ptr()
is_ok = v.is_mut && v.is_arg && !typ.deref().is_ptr()
}
}
if !is_ok && !c.pref.translated {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/redundant_parentheses_warning.vv:3:7: error: redundant parentheses are used
1 | fn main() {
2 | a := 2
3 | b := ((a + 2))
| ~~~~~~~~~
4 | println(b)
5 | }

View File

@ -0,0 +1,5 @@
fn main() {
a := 2
b := ((a + 2))
println(b)
}