Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant nil checks in RBI::Visitor #312

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

amomchilov
Copy link
Contributor

No description provided.

@amomchilov amomchilov changed the title Misc. tidying Remove redundant nil checks in RBI::Visitor Jun 11, 2024
@@ -117,8 +117,6 @@ def initialize(output, left_name: "left", right_name: "right", keep: Keep::NONE)

sig { override.params(node: T.nilable(Node)).void }
def visit(node)
return unless node
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this causes a type failure before, even if I make Indexable a subtype of Node (by adding requires_ancestor { Node }):

lib/rbi/rewriters/merge_trees.rb:155: Expected RBI::Node but found T.all(RBI::Indexable, T.nilable(RBI::Node)) for argument right https://srb.help/7002
     155 |                make_conflict_tree(prev, node)
                                                   ^^^^

Should T.all(RBI::Indexable, T.nilable(RBI::Node)) be simplified to T.all(RBI::Indexable, RBI::Node) (since it needs to be Indexable, it can't also be nil), and therefore be a subtype of RBI::Node?

@@ -81,8 +81,6 @@ def visit_all(nodes)

sig { override.params(node: T.nilable(Node)).void }
def visit(node)
return unless node
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced removing these is a great idea. They're technically redundant, but they also fail fast on nil, which would be a really common case to hit (rather than doing 3 Class#=== checks below)


# The child nodes could contain private/protected markers. If so, they should not be moved in the file.
# Otherwise, some methods could see their privacy change. To avoid that problem, divide the array of child
# nodes into chunks based on whether any Visibility nodes appear, and sort the chunks independently. This
# applies the ordering rules from the node_rank method as much as possible, while preserving visibility.
sorted_nodes = node.nodes.chunk do |n|
n.is_a?(Visibility)
end.flat_map do |_, nodes|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no Rubocop rule that can enforce this (last I checked), but having end.something_else do || on the same line is kinda weird, and reads better when it's one operation per line IMO.

Comment on lines -13 to -14
case node
when Tree
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing these case statements with only one when worked out nicely.

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.

1 participant