Skip to content

Add TypeToolkit/PreferNotNil cop - #37

Merged
Morriar merged 6 commits into
mainfrom
t-must-not-nil-cop
Aug 7, 2026
Merged

Add TypeToolkit/PreferNotNil cop#37
Morriar merged 6 commits into
mainfrom
t-must-not-nil-cop

Conversation

@Morriar

@Morriar Morriar commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

What

Adds an autocorrecting TypeToolkit/PreferNotNil RuboCop cop that replaces Sorbet's T.must with Type Toolkit's .not_nil!.

# before
value = T.must(fetch_value)

# after
value = fetch_value.not_nil!

The cop is enabled by default.

Why

Type Toolkit's not_nil! provides the same non-nil assertion while producing a more useful UnexpectedNilError when the assertion fails. It also supports Core's migration away from runtime Sorbet constructs.

Autocorrection details

The autocorrection preserves Ruby semantics and syntax for:

  • binary and logical operators
  • conditionals, ranges, and assignments
  • command-style calls and calls with blocks
  • defined?, yield, and super
  • string interpolation
  • multiline calls, including comments and trailing commas
  • nested T.must calls across RuboCop correction passes

Splat arguments are not corrected because they do not have a safe receiver-style equivalent.

@Morriar
Morriar requested a review from amomchilov August 7, 2026 17:39
Comment thread lib/rubocop/cop/type_toolkit/prefer_not_nil.rb Outdated
Comment thread lib/rubocop/cop/type_toolkit/prefer_not_nil.rb Outdated
Comment thread lib/rubocop/cop/type_toolkit/prefer_not_nil.rb
Comment thread lib/rubocop/cop/type_toolkit/prefer_not_nil.rb Outdated
Comment thread lib/rubocop/cop/type_toolkit/prefer_not_nil.rb
@Morriar
Morriar merged commit 8d51a4e into main Aug 7, 2026
1 check passed
@Morriar
Morriar deleted the t-must-not-nil-cop branch August 7, 2026 20:17
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.

2 participants