Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust checker: allow to ignore secondary spans #1696

Merged
merged 5 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions autoload/ale/handlers/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ if !exists('g:ale_rust_ignore_error_codes')
let g:ale_rust_ignore_error_codes = []
endif

if !exists('g:ale_rust_ignore_secondary_spans')
let g:ale_rust_ignore_secondary_spans = 0
endif

function! s:FindSpan(buffer, span) abort
if ale#path#IsBufferPath(a:buffer, a:span.file_name) || a:span.file_name is# '<anon>'
return a:span
Expand Down Expand Up @@ -47,6 +51,10 @@ function! ale#handlers#rust#HandleRustErrors(buffer, lines) abort
for l:root_span in l:error.spans
let l:span = s:FindSpan(a:buffer, l:root_span)

if ale#Var(a:buffer, 'rust_ignore_secondary_spans') && !get(l:span, 'is_primary', 1)
continue
endif

if !empty(l:span)
call add(l:output, {
\ 'lnum': l:span.line_start,
Expand Down
16 changes: 16 additions & 0 deletions doc/ale-rust.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ g:ale_rust_ignore_error_codes *g:ale_rust_ignore_error_codes*
>
let g:ale_rust_ignore_error_codes = ['E0432', 'E0433']

g:ale_rust_ignore_secondary_spans *g:ale_rust_ignore_secondary_spans*
*b:ale_rust_ignore_secondary_spans*
Type: Number
Default: 0

When set to 1, instructs the Rust error repporting to ignore secondary
spans. The problem with secondary spans is that they sometimes appear in
error messages before the main cause of the error, for example: >

1 src/main.rs|98 col 5 error| this function takes 4 parameters but 5
parameters were supplied: defined here
2 src/main.rs|430 col 32 error| this function takes 4 parameters but 5
parameters were supplied: expected 4 parameters
<
This is due to the sorting by line numbers. With this option set to 1,
the 'defined here' span will not be presented.

===============================================================================
rustfmt *ale-rust-rustfmt*
Expand Down
145 changes: 145 additions & 0 deletions test/handler/test_rust_handler.vader
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,148 @@ Execute(The Rust handler should find correct files):
\ },
\ }),
\ ])

Execute(The Rust handler should remove secondary spans if set):
call ale#test#SetFilename('src/noerrors/mod.rs')

let g:ale_rust_ignore_secondary_spans = 0
AssertEqual
\ [
\ {
\ 'lnum': 1,
\ 'end_lnum': 1,
\ 'type': 'E',
\ 'end_col': 21,
\ 'col': 1,
\ 'text': 'this function takes 1 parameter but 0 were supplied: defined here',
\ },
\ {
\ 'lnum': 1,
\ 'end_lnum': 1,
\ 'type': 'E',
\ 'end_col': 46,
\ 'col': 40,
\ 'text': 'this function takes 1 parameter but 0 were supplied: expected 1 parameter',
\ },
\ ],
\ ale#handlers#rust#HandleRustErrors(bufnr(''), [
\ '',
\ 'fn test(x: u8) -> u8 { x } fn main() { x(); }',
\ json_encode({
\ 'message': {
\ 'code': {
\ 'code': 'E0061',
\ 'explanation': 'Dummy explanation; not used'
\ },
\ 'level': 'error',
\ 'message': 'this function takes 1 parameter but 0 were supplied',
\ 'spans': [
\ {
\ 'byte_end': 20,
\ 'byte_start': 0,
\ 'column_end': 21,
\ 'column_start': 1,
\ 'file_name': 'src/noerrors/mod.rs',
\ 'is_primary': v:false,
\ 'label': 'defined here',
\ 'line_end': 1,
\ 'line_start': 1,
\ },
\ {
\ 'byte_end': 45,
\ 'byte_start': 39,
\ 'column_end': 46,
\ 'column_start': 40,
\ 'file_name': '<anon>',
\ 'is_primary': v:true,
\ 'label': 'expected 1 parameter',
\ 'line_end': 1,
\ 'line_start': 1,
\ },
\ ]
\ },
\ }),
\ json_encode({
\ 'message': {
\ 'code': v:null,
\ 'level': 'error',
\ 'message': 'aborting due to previous error',
\ 'spans': []
\ },
\ }),
\ json_encode({
\ 'message': {
\ 'code': v:null,
\ 'level': 'error',
\ 'message': 'For more information about this error, try `rustc --explain E0061`.',
\ 'spans': []
\ },
\ }),
\ ])

let g:ale_rust_ignore_secondary_spans = 1
AssertEqual
\ [
\ {
\ 'lnum': 1,
\ 'end_lnum': 1,
\ 'type': 'E',
\ 'end_col': 46,
\ 'col': 40,
\ 'text': 'this function takes 1 parameter but 0 were supplied: expected 1 parameter',
\ },
\ ],
\ ale#handlers#rust#HandleRustErrors(bufnr(''), [
\ '',
\ 'fn test(x: u8) -> u8 { x } fn main() { x(); }',
\ json_encode({
\ 'message': {
\ 'code': {
\ 'code': 'E0061',
\ 'explanation': 'Dummy explanation; not used'
\ },
\ 'level': 'error',
\ 'message': 'this function takes 1 parameter but 0 were supplied',
\ 'spans': [
\ {
\ 'byte_end': 20,
\ 'byte_start': 0,
\ 'column_end': 21,
\ 'column_start': 1,
\ 'file_name': 'src/noerrors/mod.rs',
\ 'is_primary': v:false,
\ 'label': 'defined here',
\ 'line_end': 1,
\ 'line_start': 1,
\ },
\ {
\ 'byte_end': 45,
\ 'byte_start': 39,
\ 'column_end': 46,
\ 'column_start': 40,
\ 'file_name': '<anon>',
\ 'is_primary': v:true,
\ 'label': 'expected 1 parameter',
\ 'line_end': 1,
\ 'line_start': 1,
\ },
\ ]
\ },
\ }),
\ json_encode({
\ 'message': {
\ 'code': v:null,
\ 'level': 'error',
\ 'message': 'aborting due to previous error',
\ 'spans': []
\ },
\ }),
\ json_encode({
\ 'message': {
\ 'code': v:null,
\ 'level': 'error',
\ 'message': 'For more information about this error, try `rustc --explain E0061`.',
\ 'spans': []
\ },
\ }),
\ ])