(via HaTS)
Gopls has an analyzer that reports failure to use the result of a call to certain functions. We haven't yet come up with a way to generalize this feature without adding a lot of noisy diagnostics. However, it would be easy to add an (optional) discrete inlay hint showing that an error result has been ignored, as this is often a mistake. (Clearly common functions like fmt.Println would need to be excluded.) For example:
🙈 file.Close()
(That's U+1F648 SEE-NO-EVIL MONKEY. Better ideas welcome.)
Comment From: adonovan
FWIW, this analyzer turned up a lot of mistakes in the gopls codebase where we unwittingly ignored real error results. Perhaps we should add something like it as an optional analyzer:
info := pass.TypesInfo
errorType := types.Universe.Lookup("error").Type()
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
for curExprStmt := range inspect.Root().Preorder((*ast.ExprStmt)(nil)) {
child, _ := curExprStmt.FirstChild()
if call, ok := child.Node().(*ast.CallExpr); ok {
if tv, ok := info.Types[call]; ok {
t := tv.Type
if sig, ok := t.(*types.Signature); ok && sig.Results().Len() > 1 {
t = sig.Results().At(sig.Results().Len() - 1).Type()
}
if types.Identical(t, errorType) {
pass.ReportRangef(call.Fun, "ignores error result")
}
// TODO suppress if "// ignore error" comment present,
// or for common list including fmt.Println, bytes.Buffer.Write*, etc.
}
}
}
Comment From: gopherbot
Change https://go.dev/cl/677516 mentions this issue: gopls/internal: handle errors, or document that we ignore them
Comment From: gopherbot
Change https://go.dev/cl/683835 mentions this issue: gopls/internal/golang: InlayHint: reveal ignored errors