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

Add Lint/UnusedSelf and Lint/UnusedPseudoMethod #543

Open
nobodywasishere opened this issue Jan 20, 2025 · 3 comments · May be fixed by #556
Open

Add Lint/UnusedSelf and Lint/UnusedPseudoMethod #543

nobodywasishere opened this issue Jan 20, 2025 · 3 comments · May be fixed by #556
Assignees
Labels
Milestone

Comments

@nobodywasishere
Copy link
Contributor

nobodywasishere commented Jan 20, 2025

These cannot be redefined and do not have side effects:

  • self
  • pointerof
  • sizeof
  • instance_sizeof
  • alignof
  • instance_alignof
  • offsetof
  • typeof
  • is_a?
  • as
  • as?
  • responds_to?
  • nil?
  • !
class MyInt32
  def self.int_klass : Class
    if @var > 1
      self
    # ^^^^ error: Unused self
    end

    typeof(self)
    # ^^^^^^^^^^ error: Unused pseudo method

    !true
    # ^^^ error: Unused pseudo method
    
    Int32
  end
end
@straight-shoota
Copy link
Contributor

Note: typeof does have a side effect. It forces the compiler to type its arguments, but without creating a code path where this could actually end up in codegen.

This mechanism is used quite a lot in stdlib specs for example. It ensures the respective code is semantically valid, without checking the execution result. For example: https://github.com/crystal-lang/crystal/blob/39aaae569f22e39a155dcfe5a962bd2660cc828f/spec/std/signal_spec.cr#L9-L11

@Sija Sija added the rule label Jan 21, 2025
@nobodywasishere
Copy link
Contributor Author

I have these implemented. I'll create a PR once #537 is merged

@nobodywasishere
Copy link
Contributor Author

as can also have a side effect of raising if the cast cannot be performed (this is used in a few places in the stdlib)

@Sija Sija added this to the 1.7.0 milestone Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants