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

Incorrrect IncompatibleAssignment error in Array#min with a static value #1368

Open
KieranP opened this issue Nov 29, 2024 · 1 comment
Open

Comments

@KieranP
Copy link

KieranP commented Nov 29, 2024

In the latest release (1.9), and starting to see IncompatibleAssignment errors where I did not previously.

class Models
  class Health < Base
    attr_reader :value #: ::Integer

    # @rbs amount: ::Integer
    # @rbs return: void
    def gain(amount)
      @value = [
        value + amount,
        100
      ].min
    end

    # @rbs amount: ::Integer
    # @rbs return: void
    def lose(amount)
      @value = [
        0,
        value - amount
      ].max
    end
  end
end
app/models/health.rb:8:6: [error] Cannot assign a value of type `(::Integer | nil)` to a variable of type `::Integer`
│   (::Integer | nil) <: ::Integer
│     nil <: ::Integer
│
│ Diagnostic ID: Ruby::IncompatibleAssignment
│
└       @value = [
        ~~~~~~~~~~

app/models/health.rb:17:6: [error] Cannot assign a value of type `(::Integer | nil)` to a variable of type `::Integer`
│   (::Integer | nil) <: ::Integer
│     nil <: ::Integer
│
│ Diagnostic ID: Ruby::IncompatibleAssignment
│
└       @value = [

Looks like the Array#min is saying it can return nil, which for an empty array would be the case ([].max => nil), but the array has a static Integer in it (100), so [100].max can never return nil. Additionally, the amount argument and value reader are also of type ::Integer, not ::Integer?, which means both elements in the array will always contain a value from a typing perspective...

This error should not show in cases where the type check determines that there will always be at least one non-nil value in the array.

@KieranP KieranP changed the title Latest master introduced false positives Incorrrect IncompatibleAssignment error in Array#min with a static value Dec 6, 2024
@KieranP
Copy link
Author

KieranP commented Dec 6, 2024

Confirming that this issue is still present in the newly released 1.9 version.

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

No branches or pull requests

1 participant