Skip to content

nil-compare: detect typed nil conversions#299

Open
mmorel-35 wants to merge 1 commit intoAntonboom:masterfrom
mmorel-35:issue-222
Open

nil-compare: detect typed nil conversions#299
mmorel-35 wants to merge 1 commit intoAntonboom:masterfrom
mmorel-35:issue-222

Conversation

@mmorel-35
Copy link
Copy Markdown

The nil-compare checker failed to recognise typed nil conversions such as (*tls.Config)(nil), []MailserverTopic(nil), []base.DiskFileNum(nil) as nil values, so patterns like the following went undetected:

assert.EqualValues(t, (*tls.Config)(nil), opts.TLSConfig())  // should → assert.Nil(t, opts.TLSConfig())
require.EqualValues(t, []base.DiskFileNum(nil), recycler.LogNumsForTesting())

nil-compare

  • Added isTypedNil(pass, expr) using pass.TypesInfo.Types[ce.Fun].IsType() to detect any T(nil) type-conversion expression.
  • Updated xorNil (now accepts *analysis.Pass) to match both untyped nil and typed nil via the new isNilExpr helper.
  • Typed nil on either side of the comparison is handled; the surviving non-nil argument is kept in the suggested fix.

expected-actual

  • isExpectedValueCandidate previously treated (T)(nil) as an expected-value candidate via isParenExpr, causing false-positive "reverse arguments" diagnostics for comparison functions.
  • For Equal, EqualValues, Exactly, NotEqual, NotEqualValues etc., typed nil is now excluded from the expected-value heuristic — nil-compare owns those cases.
  • IsType / IsNotType are unaffected; (*T)(nil) as a type witness continues to be recognised correctly.

Tests

  • gen_nil_compare.go: replaced the inert UnsupportedAssertions block with TypedNilAssertions carrying full report/proposed-fix metadata; both left- and right-side typed nil variants are covered for all five affected functions.
  • gen_expected_actual.go: added ignored-assertion entries confirming typed nil in comparison functions does not trigger expected-actual.

Closes #222

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
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.

nil-compare: support EqualValues with typed nil

1 participant