Reject unsafe typeof suppression in defensive.null-assumption rule - #149
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
typeof <param> ===/==as a null guard, which allows unsafe patterns liketypeof x === "object"(notetypeof null === "object") or non-dominating checks to suppressdefensive.null-assumptionfindings.Description
"typeof <name> ==="/"typeof <name> =="entries from the nullable-parameter guard list innullableParamGuardedso arbitrary whole-functiontypeofoccurrences no longer count as guards.nullableStatementUsesOnlyNullSafeOperatorsto preserve only safe, same-statement TypeScript narrowing patterns by matching atypeof-based ternary narrowing that directly dereferences the narrowed value in the same expression.TestDefensiveNullAssumptionRejectsUnsafeTypeofGuardsintests/checks/quality_precision_followup_retune_test.gothat asserts unsafetypeofusages (e.g.typeof user === 'object'and a latetypeofafter an unsafe dereference) still triggerdefensive.null-assumption.internal/codeguard/checks/quality/quality_defensive.goandtests/checks/quality_precision_followup_retune_test.go.Testing
go test ./tests/checks -run 'TestDefensiveNullAssumption(CreditsTypeScriptNarrowing|RejectsUnsafeTypeofGuards)$' -count=1, which passed.go test ./...after fixing test assertions, which completed successfully for the repository packages that include tests.gofmt -wto the modified files and validatedgit diff --checkproduced no whitespace issues during the edit cycle.Codex Task