-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Comments
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
8 tasks
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
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 asdefined?("FooBar")
ordefined?(: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:
The text was updated successfully, but these errors were encountered: