+
Skip to content

False positive for status.Error with non-OK gRPC codes #367

@nick-we

Description

@nick-we

The linter incorrectly flags a return nil, err statement where err is generated by google.golang.org/grpc/status.Error.

The linter seems to assume that status.Error() can return a nil error, which is only true if the gRPC code is codes.OK. When a non-OK code like codes.InvalidArgument is used, the function is guaranteed to return a non-nil error. The linter's static analysis doesn't appear to differentiate between these cases, leading to a false positive.

package main

import (
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// This function will be flagged by the nilerr linter.
func someFunction(isValid bool) (*string, error) {
	if !isValid {
		// The linter complains on the following line, even though
		// codes.InvalidArgument guarantees a non-nil error.
		return nil, status.Error(codes.InvalidArgument, "input is invalid")
	}
	result := "success"
	return &result, nil
}

The output will be:

literal `nil` returned from `someFunction()` in position 0 when the error return in position 1 is not guaranteed to be non-nil through all paths

Expected Behavior:
The linter should not report an error for this code, as status.Error(codes.InvalidArgument, "...") is guaranteed to produce a non-nil error, making a (nil, nil) return impossible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载