flyerhzm / rails_best_practices

a code metric tool for rails projects
http://rails-bestpractices.com
MIT License
4.16k stars 276 forks source link

Meaning of stmts_add and unary in line number? #338

Closed MuneebSarfraz94 closed 4 years ago

MuneebSarfraz94 commented 5 years ago

When i run results in html format i sometimes get "stmts_add" and "unary" in my line number column. What does it mean?

flyerhzm commented 5 years ago

@MuneebSarfraz94 could you show me how to reproduce it?

MuneebSarfraz94 commented 5 years ago

When i generated an html report is shows me unary in column line number rather than showing any numeric value. i'm unable to attach a screenshot of that report though.

ybiquitous commented 4 years ago

Hi there,

I can reproduce this invalid line number problem on the discourse/discourse repository.

Here is the step:

  1. git clone --depth 1 https://github.com/discourse/discourse.git
  2. cd discourse
  3. rails_best_practices app/controllers/groups_controller.rb

Here is the output. We can see the invalid line number method_add_arg in app/controllers/groups_controller.rb:

/tmp/discourse/app/controllers/groups_controller.rb:40 - move model logic into model (groups use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:40 - move model logic into model (Group use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:111 - move model logic into model (group use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:201 - move model logic into model (users use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:201 - move model logic into model (filter use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:298 - move model logic into model (group use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:298 - move model logic into model (users use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:338 - move model logic into model (group use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:428 - move model logic into model (group use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:497 - move model logic into model (groups use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:576 - move model logic into model (users use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:576 - move model logic into model (Discourse::InvalidParameters use_count > 4)
/tmp/discourse/app/controllers/groups_controller.rb:40 - remove unused methods (GroupsController#index)
/tmp/discourse/app/controllers/groups_controller.rb:138 - remove unused methods (GroupsController#edit)
/tmp/discourse/app/controllers/groups_controller.rb:178 - remove unused methods (GroupsController#mentions)
/tmp/discourse/app/controllers/groups_controller.rb:298 - remove unused methods (GroupsController#add_members)
/tmp/discourse/app/controllers/groups_controller.rb:338 - remove unused methods (GroupsController#handle_membership_request)
/tmp/discourse/app/controllers/groups_controller.rb:394 - remove unused methods (GroupsController#remove_member)
/tmp/discourse/app/controllers/groups_controller.rb:method_add_arg - use scope access
/tmp/discourse/app/controllers/groups_controller.rb:method_add_arg - use scope access

Please go to https://rails-bestpractices.com to see more useful Rails Best Practices.

Found 20 warnings.

Environment:

$ rails_best_practices -v
1.20.0

$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
ybiquitous commented 4 years ago

I probably think the Sexp#line_number in the code_analyzer gem is a cause:

https://github.com/flyerhzm/code_analyzer/blob/6de6c9bcbdc9c8f2b05d0619d5506911a6244f5a/lib/code_analyzer/sexp.rb#L27

By adding :unary as follows, I can confirm it works expectedly for some Rails applications:

-when :string_add, :opassign
+when :string_add, :opassign, :unary

I'm preparing to send a patch to flyerhzm/code_analyzer.