Skip to content

staticcheck/sa1012: handle method expressions - #1851

Open
ychampion wants to merge 1 commit into
dominikh:masterfrom
ychampion:fix/sa1012-method-expr
Open

ychampion wants to merge 1 commit into
dominikh:masterfrom
ychampion:fix/sa1012-method-expr

Conversation

@ychampion

Copy link
Copy Markdown

When a method expression such as (*S).F(nil, context.Background()) is called, call.Args[0] is the method receiver and call.Args[1] corresponds to sig.Params().At(0). SA1012 previously inspected call.Args[0] unconditionally whenever sig.Params().Len() > 0, falsely flagging a nil receiver when the method's first parameter had type context.Context and missing a nil context passed as call.Args[1].

Detect types.MethodExpr selections on call.Fun and inspect call.Args[1] (with a matching pattern branch for nil in the second argument position) while continuing to inspect call.Args[0] for ordinary function and bound method-value calls.

Closes #1719

When a method expression such as (*S).F(nil, context.Background()) is
called, call.Args[0] is the method receiver and call.Args[1] corresponds
to sig.Params().At(0). Inspect call.Args[1] when call.Fun resolves to a
types.MethodExpr selection so nil receivers are not falsely flagged as
nil contexts and nil contexts passed after the receiver are reported.

Closes dominikh#1719
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SA1012: false positive when calling method expression

1 participant