ale icon indicating copy to clipboard operation
ale copied to clipboard

Show warnings as warnings in golangci_lint

Open jonasrichard opened this issue 2 years ago • 2 comments

golangci_lint cannot really distinguish if a lint message is a compilation error or it is a warning, or I think one way maybe:

When a file has syntax errors, the last part of the message in parenthesis will be typecheck see it here

$ golangci-lint run main.go
main.go:3:1: expected declaration, found mport (typecheck)
mport (
^

Although if there is no compilation error, other sublinters kick in and those should be shown as warnings.

$ golangci-lint run main.go
main.go:25:6: `publish` is unused (deadcode)
func publish(port int) {
     ^

I think in the message collection part we can match the last word of the linter message in the parentheses, and if it is not typecheck we can append them as 'W' warning.

https://github.com/dense-analysis/ale/blob/6c51bb1573f0bf5deff04508769208fd503b5ff7/ale_linters/go/golangci_lint.vim#L43

I don't know the other linters, but one-by-one those also can be improved.

jonasrichard avatar Apr 28 '22 12:04 jonasrichard

Can it be something like this?

function! ale_linters#go#golangci_lint#GetMatches(lines) abort
    let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?:?:?:?\s\*?(.+)\s+(.+)$'

    return ale#util#GetMatches(a:lines, l:pattern)
endfunction

function! ale_linters#go#golangci_lint#Handler(buffer, lines) abort
    let l:dir = expand('#' . a:buffer . ':p:h')
    let l:output = []

    for l:match in ale_linters#go#golangci_lint#GetMatches(a:lines)
        let l:msg_type = 'W'

        if l:match[5] == 'typecheck'
            let l:msg_type = 'E'
        endif

        " l:match[1] will already be an absolute path, output from
        " golangci_lint
        call add(l:output, {
        \   'filename': ale#path#GetAbsPath(l:dir, l:match[1]),
        \   'lnum': l:match[2] + 0,
        \   'col': l:match[3] + 0,
        \   'type': l:msg_type,
        \   'text': l:match[4],
        \})
    endfor

    return l:output
endfunction

jonasrichard avatar Apr 28 '22 13:04 jonasrichard

Looks good... you can make a PR with these changes. Make sure to update and add new tests to test/handler/test_gometalinter_handler.vader.

I do not develop frequently in Golang but I would consider also marking typecheck, staticcheck, errcheck, and govet as Errors since their preset is "bug" and these are enabled by default.

hsanson avatar Apr 30 '22 04:04 hsanson

Handled by #4182

hsanson avatar Feb 28 '24 01:02 hsanson