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

a = a を検出するCop #6

Closed
pocke opened this issue Nov 12, 2017 · 2 comments
Closed

a = a を検出するCop #6

pocke opened this issue Nov 12, 2017 · 2 comments

Comments

@pocke
Copy link
Contributor

pocke commented Nov 12, 2017

代入の右辺で代入される変数を使用すると、それは必ずnilなのでなんらかの間違いである可能性が高い。
そのため、そのような代入を検出するCopが欲しい

a = a # 右辺のaは常にnil
a = x do
  a # このaは常にnil
end

これは、実装が大変だとおもう(たぶんね)。VariableForceとか見ると答えがあるかも知れないけど、私もよく知らない。

@koic
Copy link
Member

koic commented Oct 29, 2022

Lint/SelfAssignment cop で実現されているためクローズします。RuboCop 0.89 以上で利用可能です。
https://docs.rubocop.org/rubocop/1.37/cops_lint.html#lintselfassignment

ただし、以下のケースはサポートされていなさそうで、別途拡張できそうです。

a = x do
  a # このaは常にnil
end

@koic koic closed this as completed Oct 29, 2022
@pocke
Copy link
Contributor Author

pocke commented Oct 29, 2022

Issueの整理ありがとうございます 🙏

a = x do
  a # このaは常にnil
end

改めて考えてみると、これは必ずしもこうはならなさそうですね。

a = Thread.new do
  sleep 0.1
  p a
  # => #<Thread:0x0000000102f176d0 /path/top/test.rb:1 run>
end

a.join

このブロックがa変数が初期化される前にだけ呼ばれるのかを判定するのは、静的には難しそうな気がするので、拡張も難しそうな気がしています 💭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants