ruby-on-railsrubocop

disable Rubocop complaint about method


I have a method that goes like this return if amount == 0 and rubocop is complaining that it should be like return if amount.zero?. How can I have it skip over that method? Here is my .rubocop.yml:

rubocop

StringLiterals:
  EnforcedStyle: double_quotes
  Enabled: true

Style/FrozenStringLiteralComment:
  Enabled: false

Style/TrailingCommaInLiteral:
  Enabled: false

Style/TrailingCommaInArguments:
  Enabled: false

Style/CaseEquality:
  Enabled: false

Documentation:
  Description: "Document classes and non-namespace modules."
  Enabled: false

Metrics/MethodLength:
  CountComments: false
  Max: 15

Rails/Delegate:
  Enabled: false

AllCops:
  Exclude:
    - db/**/*
    - config/**/*
    - script/**/*
    - vendor/**/*
    - bin/**/*
    - Rakefile
    - spec/spec_helper.rb
    - spec/rails_helper.rb
    - spec/teaspoon_env.rb

  TargetRubyVersion: 2.3

Rails:
  Enabled: true

submit_ticket_payment.rb

def call
  return if amount == 0
  Stripe::Charge.create(
    amount: amount,
    currency: "usd",
    description: event_name,
    receipt_email: context.rsvp.email,
    source: context.token,
    statement_descriptor: event_name
  )
rescue Stripe::StripeError
  context.fail!
end

So basically how can I have it not care about that particular instance?


Solution

  • The check in question is implemented by the Style/NumericPredicate cop.

    If you generally want it to be enabled but not complain about an individual occurence, you can temporarily disable it with a special comment:

    # rubocop:disable Style/NumericPredicate
    return if amount == 0
    # rubocop:enable Style/NumericPredicate
    

    Note that it needs to be enabled again; otherwise it will skip the check for the whole remainder of the file.