Skip to content

Remove negative check for nil#446

Open
ashmaroli wants to merge 3 commits intosporkmonger:mainfrom
ashmaroli:redundant-negative-nil-check
Open

Remove negative check for nil#446
ashmaroli wants to merge 3 commits intosporkmonger:mainfrom
ashmaroli:redundant-negative-nil-check

Conversation

@ashmaroli
Copy link
Copy Markdown
Contributor

Technically, if an object is not nil, it is either a Boolean false or a truthy.

I'm making an assumption here that false can be ignored because this check is used against values set by :parse method.
Therefore, the test for != nil is redundant.

Comment thread lib/addressable/uri.rb
sub_delims = CharacterClasses::SUB_DELIMS
if !self.host.nil? && (self.host =~ /[<>{}\/\\\?\#\@"[[:space:]]]/ ||
(self.host[/^\[(.*)\]$/, 1] != nil && self.host[/^\[(.*)\]$/, 1] !~
(self.host[/^\[(.*)\]$/, 1] && self.host[/^\[(.*)\]$/, 1] !~
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

This comment was marked as spam.

Comment thread lib/addressable/uri.rb
"Cannot have a relative path with an authority set: '#{self.to_s}'"
end
if self.path != nil && !self.path.empty? &&
if self.path && !self.path.empty? &&
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
if self.path != nil && !self.path.empty? && self.path[0..0] != SLASH &&
self.authority != nil
if self.path && !self.path.empty? && self.path[0..0] != SLASH &&
self.authority
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Layout/MultilineOperationIndentation: Align the operands of a condition in an if statement spanning multiple lines.
Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
end
if self.path != nil && !self.path.empty? && self.path[0..0] != SLASH &&
self.authority != nil
if self.path && !self.path.empty? && self.path[0..0] != SLASH &&
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
self.password != nil
if self.port ||
self.user ||
self.password
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Layout/MultilineOperationIndentation: Align the operands of a condition in an if statement spanning multiple lines.
Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
uri_string = String.new
uri_string << "#{self.scheme}:" if self.scheme != nil
uri_string << "//#{self.authority}" if self.authority != nil
uri_string << "#{self.scheme}:" if self.scheme
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
# @return [String] The URI's <code>String</code> representation.
def to_s
if self.scheme == nil && self.path != nil && !self.path.empty? &&
if self.scheme == nil && self.path && !self.path.empty? &&
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.
Style/NilComparison: Prefer the use of the nil? predicate.

Comment thread lib/addressable/uri.rb
# If the base path is empty and an authority segment has been
# defined, use a base path of SLASH
if base_path.empty? && self.authority != nil
if base_path.empty? && self.authority
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
if uri.path == nil || uri.path.empty?
joined_path = self.path
if uri.query != nil
if uri.query
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Metrics/BlockNesting: Avoid more than 3 levels of block nesting.
Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.

Comment thread lib/addressable/uri.rb
site_string << "#{self.normalized_scheme}:"
end
if self.normalized_authority != nil
if self.normalized_authority
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
@normalized_site ||= begin
site_string = "".dup
if self.normalized_scheme != nil
if self.normalized_scheme
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
site_string << "#{self.scheme}:" if self.scheme != nil
site_string << "//#{self.authority}" if self.authority != nil
site_string << "#{self.scheme}:" if self.scheme
site_string << "//#{self.authority}" if self.authority
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
site_string = "".dup
site_string << "#{self.scheme}:" if self.scheme != nil
site_string << "//#{self.authority}" if self.authority != nil
site_string << "#{self.scheme}:" if self.scheme
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
if new_port != nil && !(new_port.to_s =~ /^\d+$/)
raise InvalidURIError,
"Invalid port number: #{new_port.inspect}"
if new_port && !(new_port.to_s =~ /^\d+$/)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/InverseMethods: Use !~ instead of inverting =~.

Comment thread lib/addressable/uri.rb
# @param [String, Integer, #to_s] new_port The new port component.
def port=(new_port)
if new_port != nil && new_port.respond_to?(:to_str)
if new_port && new_port.respond_to?(:to_str)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/SafeNavigation: Use safe navigation (&.) instead of checking if an object exists before calling the method.

Comment thread lib/addressable/uri.rb
end
authority << self.normalized_host
if self.normalized_port != nil
if self.normalized_port
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
@normalized_authority ||= begin
authority = String.new
if self.normalized_userinfo != nil
if self.normalized_userinfo
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
end
authority << self.host
if self.port != nil
if self.port
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
self.host && @authority ||= begin
authority = String.new
if self.userinfo != nil
if self.userinfo
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/RedundantSelf: Redundant self detected.

Comment thread lib/addressable/uri.rb
}
components.each do |key, value|
if value != nil
if value
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Style/Next: Use next to skip iteration.

Comment thread lib/addressable/uri.rb
sub_delims = CharacterClasses::SUB_DELIMS
if !self.host.nil? && (self.host =~ /[<>{}\/\\\?\#\@"[[:space:]]]/ ||
(self.host[/^\[(.*)\]$/, 1] != nil && self.host[/^\[(.*)\]$/, 1] !~
(self.host[/^\[(.*)\]$/, 1] && self.host[/^\[(.*)\]$/, 1] !~

This comment was marked as spam.

@dpep
Copy link
Copy Markdown
Contributor

dpep commented Dec 15, 2022

nice simplifications! in a similar vein, what do you think of swapping return nil if ... for return if ... ?

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.

6 participants