@@ -5301,7 +5301,7 @@ require 'unloaded_feature'
53015301| No
53025302| Always (Unsafe)
53035303| 0.93
5304- | -
5304+ | 1.79
53055305|===
53065306
53075307Checks for redundant safe navigation calls.
@@ -5321,6 +5321,15 @@ for which to suppress (allow) this cop's check.
53215321In the example below, the safe navigation operator (`&.`) is unnecessary
53225322because `NilClass` has methods like `respond_to?` and `is_a?`.
53235323
5324+ The `InferNonNilReceiver` option specifies whether to look into previous code
5325+ paths to infer if the receiver can't be nil. This check is unsafe because the receiver
5326+ can be redefined between the safe navigation call and previous regular method call.
5327+ It does the inference only in the current scope, e.g. within the same method definition etc.
5328+
5329+ The `AdditionalNilMethods` option specifies additional custom methods which are
5330+ defined on `NilClass`. When `InferNonNilReceiver` is set, they are used to determine
5331+ whether the receiver can be nil.
5332+
53245333[#safety-lintredundantsafenavigation]
53255334=== Safety
53265335
@@ -5339,6 +5348,20 @@ CamelCaseConst&.do_something
53395348# good
53405349CamelCaseConst.do_something
53415350
5351+ # bad
5352+ foo.to_s&.strip
5353+ foo.to_i&.zero?
5354+ foo.to_f&.zero?
5355+ foo.to_a&.size
5356+ foo.to_h&.size
5357+
5358+ # good
5359+ foo.to_s.strip
5360+ foo.to_i.zero?
5361+ foo.to_f.zero?
5362+ foo.to_a.size
5363+ foo.to_h.size
5364+
53425365# bad
53435366do_something if attrs&.respond_to?(:[])
53445367
@@ -5394,6 +5417,59 @@ do_something if attrs.nil_safe_method(:[])
53945417do_something if attrs&.not_nil_safe_method(:[])
53955418----
53965419
5420+ [#infernonnilreceiver_-false-_default_-lintredundantsafenavigation]
5421+ ==== InferNonNilReceiver: false (default)
5422+
5423+ [source,ruby]
5424+ ----
5425+ # good
5426+ foo.bar
5427+ foo&.baz
5428+ ----
5429+
5430+ [#infernonnilreceiver_-true-lintredundantsafenavigation]
5431+ ==== InferNonNilReceiver: true
5432+
5433+ [source,ruby]
5434+ ----
5435+ # bad
5436+ foo.bar
5437+ foo&.baz # would raise on previous line if `foo` is nil
5438+
5439+ # good
5440+ foo.bar
5441+ foo.baz
5442+
5443+ # bad
5444+ if foo.condition?
5445+ foo&.bar
5446+ end
5447+
5448+ # good
5449+ if foo.condition?
5450+ foo.bar
5451+ end
5452+
5453+ # good (different scopes)
5454+ def method1
5455+ foo.bar
5456+ end
5457+
5458+ def method2
5459+ foo&.bar
5460+ end
5461+ ----
5462+
5463+ [#additionalnilmethods_-_present__-lintredundantsafenavigation]
5464+ ==== AdditionalNilMethods: [present?]
5465+
5466+ [source,ruby]
5467+ ----
5468+ # good
5469+ foo.present?
5470+ foo&.bar
5471+ ----
5472+
53975473[#configurable-attributes-lintredundantsafenavigation]
53985474=== Configurable attributes
53995475
@@ -5403,6 +5479,14 @@ do_something if attrs&.not_nil_safe_method(:[])
54035479| AllowedMethods
54045480| `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, `equal?`
54055481| Array
5482+
5483+ | InferNonNilReceiver
5484+ | `false`
5485+ | Boolean
5486+
5487+ | AdditionalNilMethods
5488+ | `present?`, `blank?`, `try`, `try!`
5489+ | Array
54065490|===
54075491
54085492[#lintredundantsplatexpansion]
@@ -5974,7 +6058,7 @@ end
59746058| -
59756059|===
59766060
5977- Check for arguments to `rescue` that will result in a `TypeError`
6061+ Checks for arguments to `rescue` that will result in a `TypeError`
59786062if an exception is raised.
59796063
59806064[#examples-lintrescuetype]
0 commit comments