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 Safety section to documentation #575

Merged
merged 1 commit into from Oct 16, 2021

Conversation

koic
Copy link
Member

@koic koic commented Oct 11, 2021

Follow up to rubocop/rubocop#10094.

This PR adds safety section to documentation for all cops that are Safe: false or SafeAutoCorrect: false.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@koic koic force-pushed the add_safety_section_to_docs branch 2 times, most recently from 2035d0b to e059e3f Compare October 11, 2021 16:32
Comment on lines 29 to 30
# This cop's autocorrection is unsafe because custom `update` method call was changed to `update!`
# but the method name remained same in the method definition.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This cop's autocorrection is unsafe because custom `update` method call was changed to `update!`
# but the method name remained same in the method definition.
# This cop's autocorrection is unsafe because a custom `update` method call would be changed to `update!`,
# but the method name in the definition would be unchanged.

@@ -5,6 +5,10 @@ module Cop
module Rails
# This cop checks that controllers subclass ApplicationController.
#
# @safety
# This cop's autocorrection is unsafe because a controller class which inherits
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble understanding what this means.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pirj's comment text (#575 (comment)) is excellent and I updated it.

@@ -7,6 +7,10 @@ module Rails
# Use `find_by` instead of dynamic method.
# See. https://rails.rubystyle.guide#find_by
#
# @safety
# This cop is unsafe because it may detect user-defined `find_by_xxx` method.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This cop is unsafe because it may detect user-defined `find_by_xxx` method.
# This cop is unsafe because it may detect a user-defined `find_by_xxx` method.

@@ -8,6 +8,10 @@ module Rails
# Without the `Mailer` suffix it isn't immediately apparent what's a mailer
# and which views are related to the mailer.
#
# @safety
# This cop's autocorrection is unsafe because Renaming a constant is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This cop's autocorrection is unsafe because Renaming a constant is
# This cop's autocorrection is unsafe because renaming a constant is

# a receiver object that do not have `exclude?` method. (e.g. `IPAddr`)
# @safety
# This cop is unsafe because false positive will occur for
# a receiver object that do not have `exclude?` method. (e.g. `IPAddr`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# a receiver object that do not have `exclude?` method. (e.g. `IPAddr`)
# receiver objects that do not have an `exclude?` method. (e.g. `IPAddr`)

Comment on lines 13 to 15
# This cop is unsafe because pluck is defined on both `ActiveRecord::Relation` and `Enumerable`,
# whereas `pick` is only defined on `ActiveRecord::Relation` in Rails 6.0. This will be addressed
# in Rails 6.1 via rails/rails#38760, at which point the cop will be safe.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This cop is unsafe because pluck is defined on both `ActiveRecord::Relation` and `Enumerable`,
# whereas `pick` is only defined on `ActiveRecord::Relation` in Rails 6.0. This will be addressed
# in Rails 6.1 via rails/rails#38760, at which point the cop will be safe.
# This cop is unsafe because `pluck` is defined on both `ActiveRecord::Relation` and `Enumerable`,
# whereas `pick` is only defined on `ActiveRecord::Relation` in Rails 6.0. This was addressed
# in Rails 6.1 via rails/rails#38760, at which point the cop is safe.

@@ -14,6 +14,10 @@ module Rails
# * The task does not need application code.
# * The task invokes the `:environment` task.
#
# @safety
# Probably not a problem in most cases, but it is likely that calling `:environment` task
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Probably not a problem in most cases, but it is likely that calling `:environment` task
# Probably not a problem in most cases, but it is possible that calling `:environment` task

(otherwise it seems like the sentence is a contradiction).

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incredible job!

# @safety
# This cop is unsafe because custom `update_attributes` method call was changed to
# `update` but the method name remained same in the method definition.
#
# @example
# #bad
# Book.update_attributes!(author: 'Alice')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated: I believe there's a typo here, should be lowercase book.

# [source,ruby]
# ---
# hash = { "*" => true }
# hash["*"] # This would get corrected to `hash[Arel.star]`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is from a "does not register an offense" example.

Here is a comment that explains why we've kept the cop unsafe.

WDYT of:

This cop's autocorrection is unsafe because it turns a quoted * into an SQL *, unquoted. * is a valid column name in certain databases supported by Rails, and even though it is usually a mistake, it might denote legitimate access to a column named *.

Comment on lines 9 to 10
# This cop's autocorrection is unsafe because a controller class which inherits
# `ActionController::Base` directly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and similar job/mailer/model)'s descriptions are hard to understand.
Maybe something like:

This cop's autocorrection is unsafe because it may let the logic from ApplicationContoller sneak into a controller that is not purposed to inherit logic common among other controllers.

@@ -7,6 +7,10 @@ module Rails
# Use `find_by` instead of dynamic method.
# See. https://rails.rubystyle.guide#find_by
#
# @safety
# This cop is unsafe because it may detect user-defined `find_by_xxx` method.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue that the cop is unsafe. It is certainly unsafe when not configured properly, i.e. user-defined find_by_xxx method is not added to cop's AllowedMethods.

@@ -8,6 +8,10 @@ module Rails
# Without the `Mailer` suffix it isn't immediately apparent what's a mailer
# and which views are related to the mailer.
#
# @safety
# This cop's autocorrection is unsafe because Renaming a constant is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosmetic: lowercase "Renaming"

Comment on lines 18 to 19
# Probably not a problem in most cases, but it is likely that calling `:environment` task
# will break a behavior.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also slower. E.g. some task that only needs one gem to be loaded to run will run significantly faster without loading the whole application.

Comment on lines +10 to +11
# This cop is unsafe because it cannot be determined whether
# constant or method return value specified to `class_name` is a string.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in MODEL_CLASS_REGISTRY.names['account']?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything I need to add about this?

#
# [source,ruby]
# ----
# foo&.blank? #=> nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that someone would write this intentionally?

I would argue that the cop is unsafe for flagging this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes total sense to me to draw attention, but not autocorrect 👍

My apologies, I've missed the point - thought that it's Safe: false, not SafeAutoCorrect: false.

(I lack permissions to resolve this thread).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that it's Safe: false, not SafeAutoCorrect: false.

Sure! I've opened #577.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion, I meant that its current config is fine. I mistakenly thought it is Safe: false, so I was worried that those running rubocop --safe would miss its offences.

Comment on lines 29 to 30
# This cop's autocorrection is unsafe because custom `update` method call was changed to `update!`
# but the method name remained same in the method definition.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain in a little more detail?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the example code of #98.

Comment on lines 17 to 19
# @safety
# This cop is unsafe because it may change handling time.
#
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. It's a pity it's marked as unsafe. Do you think it's possible to make it autocorrect-unsafe instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! It makes sense to me! This does not seem to cause false positives. I look rubocop/rubocop#6966, which should have used SafeAutoCorrect: false correctly. So I've opened PR #576 (I adjust the text of the @safety for the next release) .

koic added a commit that referenced this pull request Oct 12, 2021
koic added a commit to koic/rubocop-rails that referenced this pull request Oct 12, 2021
Follow up to rubocop#575 (comment)

This PR marks `Rails/TimeZone` as unsafe auto-correction from unsafe.
@koic
Copy link
Member Author

koic commented Oct 12, 2021

@andyw8 @pirj Thank you for the many great reviews! I've updated most of it except for #575 (comment) and #575 (comment).

@koic koic force-pushed the add_safety_section_to_docs branch 2 times, most recently from e9ac684 to a1e8986 Compare October 12, 2021 01:58
Follow up to rubocop/rubocop#10094.

This PR adds safety section to documentation for all cops that are
`Safe: false` or `SafeAutoCorrect: false`.
@koic koic force-pushed the add_safety_section_to_docs branch from a1e8986 to 26d71af Compare October 12, 2021 02:00
koic added a commit to koic/rubocop-rails that referenced this pull request Oct 13, 2021
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@koic koic merged commit cf584e5 into rubocop:master Oct 16, 2021
@koic koic deleted the add_safety_section_to_docs branch October 16, 2021 18:24
@koic koic mentioned this pull request Jul 20, 2022
9 tasks
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 this pull request may close these issues.

None yet

3 participants