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

Cop idea: Detect usages of defined? that will always be truthy #13437

Closed
Earlopain opened this issue Nov 8, 2024 · 0 comments · Fixed by #13440
Closed

Cop idea: Detect usages of defined? that will always be truthy #13437

Earlopain opened this issue Nov 8, 2024 · 0 comments · Fixed by #13440

Comments

@Earlopain
Copy link
Contributor

Earlopain commented Nov 8, 2024

Is your feature request related to a problem? Please describe.

defined? is a bit of a weird one, both in what it accept and in what it returns.

Usually it is used to check if a constant is defined via defined?(FooBar). But if the user is not very familiar with it they might write it as defined?("FooBar") or defined?(:FooBar) instead in order to not raise an exception during runtime when Ruby seemingly tries to access the constant. Ruby handles this of course. In fact you can write anything in there as long as it is not a syntax error and it will produce some kind of result.

Examples:

# bad
defined?("FooBar") # will always return "expression"
defined?(:FooBar) # will also always return "expression"
defined?("foo#{bar}") # you might have guessed it, also "expression"

# good - anything else
defined?(FooBar)
defined?(foo_bar)
defined?([foo, bar]) # ruby checks each entry for existance. You probably shouldn't write this as well but :shrug:
# etc. etc.
Earlopain added a commit to Earlopain/rubocop that referenced this issue Nov 9, 2024
See the cop documentation for the rationale.

Interpolation is included. Ruby does not check individual parts, for example `defined?("foo#{bar}")` is truthy even if `bar` doesn't exist.

It will find code such as the following:
/~https://github.com/search?q=lang%3Aruby+%2F+defined%5C%3F%5C%28%5B%3A%22%27%5D%5Ba-zA-Z%5D%2F&type=code

Extra reading: /~https://github.com/ruby/spec/blob/bbcd077ac3ff6da3730f388c61d4fcbbf085f4ab/language/defined_spec.rb
Earlopain added a commit to Earlopain/rubocop that referenced this issue Nov 9, 2024
See the cop documentation for the rationale.

Interpolation is included. Ruby does not check individual parts, for example `defined?("foo#{bar}")` is truthy even if `bar` doesn't exist.

It will find code such as the following:
/~https://github.com/search?q=lang%3Aruby+%2F+defined%5C%3F%5C%28%5B%3A%22%27%5D%5Ba-zA-Z%5D%2F&type=code

Extra reading: /~https://github.com/ruby/spec/blob/bbcd077ac3ff6da3730f388c61d4fcbbf085f4ab/language/defined_spec.rb
Earlopain added a commit to Earlopain/rubocop that referenced this issue Nov 10, 2024
See the cop documentation for the rationale.

Interpolation is included. Ruby does not check individual parts, for example `defined?("foo#{bar}")` is truthy even if `bar` doesn't exist.

It will find code such as the following:
/~https://github.com/search?q=lang%3Aruby+%2F+defined%5C%3F%5C%28%5B%3A%22%27%5D%5Ba-zA-Z%5D%2F&type=code

Extra reading: /~https://github.com/ruby/spec/blob/bbcd077ac3ff6da3730f388c61d4fcbbf085f4ab/language/defined_spec.rb
Earlopain added a commit to Earlopain/rubocop that referenced this issue Nov 11, 2024
See the cop documentation for the rationale.

Interpolation is included. Ruby does not check individual parts, for example `defined?("foo#{bar}")` is truthy even if `bar` doesn't exist.

It will find code such as the following:
/~https://github.com/search?q=lang%3Aruby+%2F+defined%5C%3F%5C%28%5B%3A%22%27%5D%5Ba-zA-Z%5D%2F&type=code

Extra reading: /~https://github.com/ruby/spec/blob/bbcd077ac3ff6da3730f388c61d4fcbbf085f4ab/language/defined_spec.rb
Earlopain added a commit to Earlopain/rubocop that referenced this issue Nov 20, 2024
See the cop documentation for the rationale.

Interpolation is included. Ruby does not check individual parts, for example `defined?("foo#{bar}")` is truthy even if `bar` doesn't exist.

It will find code such as the following:
/~https://github.com/search?q=lang%3Aruby+%2F+defined%5C%3F%5C%28%5B%3A%22%27%5D%5Ba-zA-Z%5D%2F&type=code

Extra reading: /~https://github.com/ruby/spec/blob/bbcd077ac3ff6da3730f388c61d4fcbbf085f4ab/language/defined_spec.rb
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 a pull request may close this issue.

1 participant