v vet: give an error for trailing whitespace (#9574)

pull/9648/head
Lukas Neubert 2021-04-09 12:22:14 +02:00 committed by GitHub
parent ddb2e72301
commit 46e7e27ba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 67 additions and 20 deletions

View File

@ -5,7 +5,9 @@ import testing
import v.util
const (
vet_known_failing_exceptions = []string{}
vet_known_failing_exceptions = [
'vlib/v/gen/js/js.v' /* trailing space */,
]
vet_folders = [
'vlib/sqlite',
'vlib/v',
@ -115,7 +117,8 @@ fn tsession(vargs string, tool_source string, tool_cmd string, tool_args string,
}
fn v_test_vetting(vargs string) {
vet_session := tsession(vargs, 'vvet', 'v vet', 'vet', vet_folders, vet_known_failing_exceptions)
expanded_vet_list := util.find_all_v_files(vet_folders) or { return }
vet_session := tsession(vargs, 'vvet', 'v vet', 'vet', expanded_vet_list, vet_known_failing_exceptions)
fmt_cmd, fmt_args := if is_fix { 'v fmt -w', 'fmt -w' } else { 'v fmt -verify', 'fmt -verify' }
expanded_vfmt_list := util.find_all_v_files(vfmt_verify_list) or { return }
verify_session := tsession(vargs, 'vfmt.v', fmt_cmd, fmt_args, expanded_vfmt_list,

View File

@ -0,0 +1,7 @@
cmd/tools/vvet/tests/trailing_space.vv:5: error: Looks like you have trailing whitespace.
cmd/tools/vvet/tests/trailing_space.vv:6: error: Looks like you have trailing whitespace.
cmd/tools/vvet/tests/trailing_space.vv:7: error: Looks like you have trailing whitespace.
cmd/tools/vvet/tests/trailing_space.vv:8: error: Looks like you have trailing whitespace.
cmd/tools/vvet/tests/trailing_space.vv:9: error: Looks like you have trailing whitespace.
cmd/tools/vvet/tests/trailing_space.vv:13: error: Looks like you have trailing whitespace.
cmd/tools/vvet/tests/trailing_space.vv:15: error: Looks like you have trailing whitespace.

View File

@ -0,0 +1,16 @@
// NB: This file has and *should* have trailing spaces.
// When making changes, please ensure they are not removed.
fn after_comments() {
// spaces after line comments give errors
/*
in block comments
too
*/
}
fn main() {
var := 'error about the spaces right there'
no_err := "inside multi line strings it's fine.
but not after"
}

View File

@ -229,6 +229,7 @@ fn (mut vt Vet) error(msg string, line int, fix vet.FixKind) {
pos: pos
kind: .error
fix: fix
typ: .default
}
}
@ -242,6 +243,7 @@ fn (mut vt Vet) warn(msg string, line int, fix vet.FixKind) {
pos: pos
kind: .warning
fix: fix
typ: .default
}
if vt.opt.is_werror {
w.kind = .error

View File

@ -207,12 +207,16 @@ pub fn parse_vet_file(path string, table_ &ast.Table, pref &pref.Preferences) (a
global_scope: global_scope
}
p.set_path(path)
if p.scanner.text.contains('\n ') {
if p.scanner.text.contains_any_substr(['\n ', ' \n']) {
source_lines := os.read_lines(path) or { []string{} }
for lnumber, line in source_lines {
if line.starts_with(' ') {
p.vet_error('Looks like you are using spaces for indentation.', lnumber,
.vfmt)
.vfmt, .space_indent)
}
if line.ends_with(' ') {
p.vet_error('Looks like you have trailing whitespace.', lnumber, .unknown,
.trailing_space)
}
}
}
@ -617,10 +621,10 @@ pub fn (mut p Parser) comment() ast.Comment {
pos.last_line = pos.line_nr + text.count('\n')
p.next()
is_multi := text.contains('\n')
// Filter out space indentation vet errors inside block comments
// Filter out false positive space indent vet errors inside comments
if p.vet_errors.len > 0 && is_multi {
p.vet_errors = p.vet_errors.filter(!(it.pos.line_nr - 1 > pos.line_nr
&& it.pos.line_nr - 1 <= pos.last_line))
p.vet_errors = p.vet_errors.filter(it.typ != .space_indent
|| it.pos.line_nr - 1 > pos.last_line || it.pos.line_nr - 1 <= pos.line_nr)
}
return ast.Comment{
is_multi: is_multi
@ -1648,7 +1652,7 @@ pub fn (mut p Parser) note_with_pos(s string, pos token.Position) {
}
}
pub fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind) {
pub fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind, typ vet.ErrorType) {
pos := token.Position{
line_nr: line + 1
}
@ -1658,6 +1662,7 @@ pub fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind) {
pos: pos
kind: .error
fix: fix
typ: typ
}
}
@ -2339,9 +2344,13 @@ fn (mut p Parser) string_expr() ast.Expr {
pos.last_line = pos.line_nr + val.count('\n')
if p.peek_tok.kind != .str_dollar {
p.next()
if p.vet_errors.len > 0 && val.contains('\n ') {
p.vet_errors = p.vet_errors.filter(!(it.pos.line_nr > pos.line_nr - 1
&& it.pos.line_nr <= pos.last_line - 1))
// Filter out false positive vet errors inside strings
if p.vet_errors.len > 0 {
p.vet_errors = p.vet_errors.filter(
(it.typ == .trailing_space && it.pos.line_nr - 1 >= pos.last_line)
|| (it.typ != .trailing_space && it.pos.line_nr - 1 > pos.last_line)
|| (it.typ == .space_indent && it.pos.line_nr - 1 <= pos.line_nr)
|| (it.typ != .space_indent && it.pos.line_nr - 1 < pos.line_nr))
}
node = ast.StringLiteral{
val: val

View File

@ -447,7 +447,8 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
p.expecting_type = prev_expecting_type
if p.pref.is_vet && op in [.key_in, .not_in] && right is ast.ArrayInit
&& (right as ast.ArrayInit).exprs.len == 1 {
p.vet_error('Use `var == value` instead of `var in [value]`', pos.line_nr, vet.FixKind.vfmt)
p.vet_error('Use `var == value` instead of `var in [value]`', pos.line_nr, vet.FixKind.vfmt,
.default)
}
mut or_stmts := []ast.Stmt{}
mut or_kind := ast.OrKind.absent

View File

@ -1376,6 +1376,7 @@ fn (mut s Scanner) vet_error(msg string, fix vet.FixKind) {
}
kind: .error
fix: fix
typ: .default
}
s.vet_errors << ve
}

View File

@ -15,6 +15,13 @@ pub enum FixKind {
vfmt
}
// ErrorType is used to filter out false positive errors under specific conditions
pub enum ErrorType {
default
space_indent
trailing_space
}
pub struct Error {
pub mut:
kind ErrorKind [required]
@ -25,4 +32,5 @@ pub:
file_path string // file where the error have origin
pos token.Position // position in the file
fix FixKind [required]
typ ErrorType [required]
}