Skip to content

Commit cf584e5

Browse files
authored
Merge pull request #575 from koic/add_safety_section_to_docs
Add `Safety` section to documentation
2 parents b0e2c7c + 26d71af commit cf584e5

26 files changed

+281
-56
lines changed

.yardopts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--markup markdown
2+
--hide-void-return
3+
--tag safety:"Cop Safety Information"

docs/modules/ROOT/pages/cops_rails.adoc

Lines changed: 137 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ skip_after_filter :do_stuff
7878
Checks that ActiveRecord aliases are not used. The direct method names
7979
are more clear and easier to read.
8080

81+
=== Safety
82+
83+
This cop is unsafe because custom `update_attributes` method call was changed to
84+
`update` but the method name remained same in the method definition.
85+
8186
=== Examples
8287

8388
[source,ruby]
@@ -309,7 +314,12 @@ after_update_commit :log_update_action
309314
| 2.5
310315
|===
311316

312-
This cop checks that controllers subclass ApplicationController.
317+
This cop checks that controllers subclass `ApplicationController`.
318+
319+
=== Safety
320+
321+
This cop's autocorrection is unsafe because it may let the logic from `ApplicationController`
322+
sneak into a controller that is not purposed to inherit logic common among other controllers.
313323

314324
=== Examples
315325

@@ -338,7 +348,12 @@ end
338348
| 2.5
339349
|===
340350

341-
This cop checks that jobs subclass ApplicationJob with Rails 5.0.
351+
This cop checks that jobs subclass `ApplicationJob` with Rails 5.0.
352+
353+
=== Safety
354+
355+
This cop's autocorrection is unsafe because it may let the logic from `ApplicationJob`
356+
sneak into a job that is not purposed to inherit logic common among other jobs.
342357

343358
=== Examples
344359

@@ -367,7 +382,12 @@ end
367382
| 2.5
368383
|===
369384

370-
This cop checks that mailers subclass ApplicationMailer with Rails 5.0.
385+
This cop checks that mailers subclass `ApplicationMailer` with Rails 5.0.
386+
387+
=== Safety
388+
389+
This cop's autocorrection is unsafe because it may let the logic from `ApplicationMailer`
390+
sneak into a mailer that is not purposed to inherit logic common among other mailers.
371391

372392
=== Examples
373393

@@ -396,7 +416,13 @@ end
396416
| 2.5
397417
|===
398418

399-
This cop checks that models subclass ApplicationRecord with Rails 5.0.
419+
This cop checks that models subclass `ApplicationRecord` with Rails 5.0.
420+
421+
=== Safety
422+
423+
This cop's autocorrection is unsafe because it may let the logic from `ApplicationRecord`
424+
sneak into an Active Record model that is not purposed to inherit logic common among other
425+
Active Record models.
400426

401427
=== Examples
402428

@@ -432,6 +458,13 @@ quoted asterisk (e.g. <tt>`my_model`.`*`</tt>). This causes the
432458
database to look for a column named <tt>`*`</tt> (or `"*"`) as opposed
433459
to expanding the column list as one would likely expect.
434460

461+
=== Safety
462+
463+
This cop's autocorrection is unsafe because it turns a quoted `*` into
464+
an SQL `*`, unquoted. `*` is a valid column name in certain databases
465+
supported by Rails, and even though it is usually a mistake,
466+
it might denote legitimate access to a column named `*`.
467+
435468
=== Examples
436469

437470
[source,ruby]
@@ -637,15 +670,17 @@ end
637670
This cop checks for code that can be written with simpler conditionals
638671
using `Object#blank?` defined by Active Support.
639672

640-
This cop is marked as unsafe auto-correction, because `' '.empty?` returns false,
641-
but `' '.blank?` returns true. Therefore, auto-correction is not compatible
642-
if the receiver is a non-empty blank string, tab, or newline meta characters.
643-
644673
Interaction with `Style/UnlessElse`:
645674
The configuration of `NotPresent` will not produce an offense in the
646675
context of `unless else` if `Style/UnlessElse` is inabled. This is
647676
to prevent interference between the auto-correction of the two cops.
648677

678+
=== Safety
679+
680+
This cop is unsafe auto-correction, because `' '.empty?` returns false,
681+
but `' '.blank?` returns true. Therefore, auto-correction is not compatible
682+
if the receiver is a non-empty blank string, tab, or newline meta characters.
683+
649684
=== Examples
650685

651686
==== NilOrEmpty: true (default)
@@ -1187,6 +1222,11 @@ This cop checks dynamic `find_by_*` methods.
11871222
Use `find_by` instead of dynamic method.
11881223
See. https://rails.rubystyle.guide#find_by
11891224

1225+
=== Safety
1226+
1227+
It is certainly unsafe when not configured properly, i.e. user-defined `find_by_xxx`
1228+
method is not added to cop's `AllowedMethods`.
1229+
11901230
=== Examples
11911231

11921232
[source,ruby]
@@ -2407,6 +2447,8 @@ end
24072447
This cop checks that methods specified in the filter's `only` or
24082448
`except` options are defined within the same class or module.
24092449

2450+
=== Safety
2451+
24102452
You can technically specify methods of superclass or methods added by
24112453
mixins on the filter, but these can confuse developers. If you specify
24122454
methods that are defined in other classes or modules, you should
@@ -2563,6 +2605,11 @@ This cop enforces that mailer names end with `Mailer` suffix.
25632605
Without the `Mailer` suffix it isn't immediately apparent what's a mailer
25642606
and which views are related to the mailer.
25652607

2608+
=== Safety
2609+
2610+
This cop's autocorrection is unsafe because renaming a constant is
2611+
always an unsafe operation.
2612+
25662613
=== Examples
25672614

25682615
[source,ruby]
@@ -2658,8 +2705,10 @@ match 'photos/:id', to: 'photos#show', via: :all
26582705
This cop enforces the use of `collection.exclude?(obj)`
26592706
over `!collection.include?(obj)`.
26602707

2661-
It is marked as unsafe by default because false positive will occur for
2662-
a receiver object that do not have `exclude?` method. (e.g. `IPAddr`)
2708+
=== Safety
2709+
2710+
This cop is unsafe because false positive will occur for
2711+
receiver objects that do not have an `exclude?` method. (e.g. `IPAddr`)
26632712

26642713
=== Examples
26652714

@@ -2769,6 +2818,11 @@ scope :chronological, -> { order(created_at: :asc) }
27692818

27702819
This cop checks for the use of output calls like puts and print
27712820

2821+
=== Safety
2822+
2823+
This cop's autocorrection is unsafe because depending on the Rails log level configuration,
2824+
changing from `puts` to `Rails.logger.debug` could result in no output being shown.
2825+
27722826
=== Examples
27732827

27742828
[source,ruby]
@@ -2886,6 +2940,14 @@ Using `pluck` followed by `first` creates an intermediate array, which
28862940
`pick` avoids. When called on an Active Record relation, `pick` adds a
28872941
limit to the query so that only one value is fetched from the database.
28882942

2943+
=== Safety
2944+
2945+
This cop is unsafe because `pluck` is defined on both `ActiveRecord::Relation` and `Enumerable`,
2946+
whereas `pick` is only defined on `ActiveRecord::Relation` in Rails 6.0. This was addressed
2947+
in Rails 6.1 via rails/rails#38760, at which point the cop is safe.
2948+
2949+
See: https://github.com/rubocop/rubocop-rails/pull/249
2950+
28892951
=== Examples
28902952

28912953
[source,ruby]
@@ -2952,6 +3014,10 @@ Post.published.pluck(:title)
29523014

29533015
This cop enforces the use of `ids` over `pluck(:id)` and `pluck(primary_key)`.
29543016

3017+
=== Safety
3018+
3019+
This cop is unsafe if the receiver object is not an Active Record object.
3020+
29553021
=== Examples
29563022

29573023
[source,ruby]
@@ -2995,11 +3061,13 @@ and can be replaced with `select`.
29953061
Since `pluck` is an eager method and hits the database immediately,
29963062
using `select` helps to avoid additional database queries.
29973063

2998-
This cop has two different enforcement modes. When the EnforcedStyle
2999-
is conservative (the default) then only calls to `pluck` on a constant
3064+
This cop has two different enforcement modes. When the `EnforcedStyle`
3065+
is `conservative` (the default) then only calls to `pluck` on a constant
30003066
(i.e. a model class) in the `where` is used as offenses.
30013067

3002-
When the EnforcedStyle is aggressive then all calls to `pluck` in the
3068+
=== Safety
3069+
3070+
When the `EnforcedStyle` is `aggressive` then all calls to `pluck` in the
30033071
`where` is used as offenses. This may lead to false positives
30043072
as the cop cannot replace to `select` between calls to `pluck` on an
30053073
`ActiveRecord::Relation` instance vs a call to `pluck` on an `Array` instance.
@@ -3231,6 +3299,12 @@ following conditions:
32313299
* The task does not need application code.
32323300
* The task invokes the `:environment` task.
32333301

3302+
=== Safety
3303+
3304+
Probably not a problem in most cases, but it is possible that calling `:environment` task
3305+
will break a behavior. It's also slower. E.g. some task that only needs one gem to be
3306+
loaded to run will run significantly faster without loading the whole application.
3307+
32343308
=== Examples
32353309

32363310
[source,ruby]
@@ -3533,7 +3607,10 @@ end
35333607

35343608
This cop checks if the value of the option `class_name`, in
35353609
the definition of a reflection is a string.
3536-
It is marked as unsafe because it cannot be determined whether
3610+
3611+
=== Safety
3612+
3613+
This cop is unsafe because it cannot be determined whether
35373614
constant or method return value specified to `class_name` is a string.
35383615

35393616
=== Examples
@@ -4206,10 +4283,20 @@ foo&.bar { |e| e.baz }
42064283
This cop checks to make sure safe navigation isn't used with `blank?` in
42074284
a conditional.
42084285

4286+
=== Safety
4287+
42094288
While the safe navigation operator is generally a good idea, when
42104289
checking `foo&.blank?` in a conditional, `foo` being `nil` will actually
42114290
do the opposite of what the author intends.
42124291

4292+
For example:
4293+
4294+
[source,ruby]
4295+
----
4296+
foo&.blank? #=> nil
4297+
foo.blank? #=> true
4298+
----
4299+
42134300
=== Examples
42144301

42154302
[source,ruby]
@@ -4257,6 +4344,26 @@ that behavior can be turned off with `AllowImplicitReturn: false`.
42574344
You can permit receivers that are giving false positives with
42584345
`AllowedReceivers: []`
42594346

4347+
=== Safety
4348+
4349+
This cop's autocorrection is unsafe because a custom `update` method call would be changed to `update!`,
4350+
but the method name in the definition would be unchanged.
4351+
4352+
[source,ruby]
4353+
----
4354+
# Original code
4355+
def update_attributes
4356+
end
4357+
4358+
update_attributes
4359+
4360+
# After running rubocop --safe-auto-correct
4361+
def update_attributes
4362+
end
4363+
4364+
update
4365+
----
4366+
42604367
=== Examples
42614368

42624369
[source,ruby]
@@ -4552,6 +4659,9 @@ user.touch
45524659
|===
45534660

45544661
Checks SQL heredocs to use `.squish`.
4662+
4663+
=== Safety
4664+
45554665
Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
45564666
to be preserved in order to work, thus auto-correction for this cop is not safe.
45574667

@@ -4617,6 +4727,10 @@ then only use of `Time.zone` is allowed.
46174727
When EnforcedStyle is 'flexible' then it's also allowed
46184728
to use `Time#in_time_zone`.
46194729

4730+
=== Safety
4731+
4732+
This cop's autocorrection is unsafe because it may change handling time.
4733+
46204734
=== Examples
46214735

46224736
[source,ruby]
@@ -4743,6 +4857,8 @@ as the cop cannot distinguish between calls to pluck on an
47434857
ActiveRecord::Relation vs a call to pluck on an
47444858
ActiveRecord::Associations::CollectionProxy.
47454859

4860+
=== Safety
4861+
47464862
This cop is unsafe because the behavior may change depending on the
47474863
database collation.
47484864
Autocorrect is disabled by default for this cop since it may generate
@@ -4993,6 +5109,11 @@ validates :foo, uniqueness: true
49935109
This cop identifies places where manually constructed SQL
49945110
in `where` can be replaced with `where(attribute: value)`.
49955111

5112+
=== Safety
5113+
5114+
This cop's autocorrection is unsafe because is may change SQL.
5115+
See: https://github.com/rubocop/rubocop-rails/issues/403
5116+
49965117
=== Examples
49975118

49985119
[source,ruby]
@@ -5036,6 +5157,8 @@ then the cop enforces `exists?(...)` over `where(...).exists?`.
50365157
When EnforcedStyle is 'where' then the cop enforces
50375158
`where(...).exists?` over `exists?(...)`.
50385159

5160+
=== Safety
5161+
50395162
This cop is unsafe for auto-correction because the behavior may change on the following case:
50405163

50415164
[source,ruby]

lib/rubocop/cop/rails/active_record_aliases.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module Rails
66
# Checks that ActiveRecord aliases are not used. The direct method names
77
# are more clear and easier to read.
88
#
9+
# @safety
10+
# This cop is unsafe because custom `update_attributes` method call was changed to
11+
# `update` but the method name remained same in the method definition.
12+
#
913
# @example
1014
# #bad
1115
# book.update_attributes!(author: 'Alice')

lib/rubocop/cop/rails/application_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
module RuboCop
44
module Cop
55
module Rails
6-
# This cop checks that controllers subclass ApplicationController.
6+
# This cop checks that controllers subclass `ApplicationController`.
7+
#
8+
# @safety
9+
# This cop's autocorrection is unsafe because it may let the logic from `ApplicationController`
10+
# sneak into a controller that is not purposed to inherit logic common among other controllers.
711
#
812
# @example
913
#

lib/rubocop/cop/rails/application_job.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
module RuboCop
44
module Cop
55
module Rails
6-
# This cop checks that jobs subclass ApplicationJob with Rails 5.0.
6+
# This cop checks that jobs subclass `ApplicationJob` with Rails 5.0.
7+
#
8+
# @safety
9+
# This cop's autocorrection is unsafe because it may let the logic from `ApplicationJob`
10+
# sneak into a job that is not purposed to inherit logic common among other jobs.
711
#
812
# @example
913
#

lib/rubocop/cop/rails/application_mailer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
module RuboCop
44
module Cop
55
module Rails
6-
# This cop checks that mailers subclass ApplicationMailer with Rails 5.0.
6+
# This cop checks that mailers subclass `ApplicationMailer` with Rails 5.0.
7+
#
8+
# @safety
9+
# This cop's autocorrection is unsafe because it may let the logic from `ApplicationMailer`
10+
# sneak into a mailer that is not purposed to inherit logic common among other mailers.
711
#
812
# @example
913
#

lib/rubocop/cop/rails/application_record.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
module RuboCop
44
module Cop
55
module Rails
6-
# This cop checks that models subclass ApplicationRecord with Rails 5.0.
6+
# This cop checks that models subclass `ApplicationRecord` with Rails 5.0.
7+
#
8+
# @safety
9+
# This cop's autocorrection is unsafe because it may let the logic from `ApplicationRecord`
10+
# sneak into an Active Record model that is not purposed to inherit logic common among other
11+
# Active Record models.
712
#
813
# @example
914
#

0 commit comments

Comments
 (0)