LoginSignup
212
150

More than 5 years have passed since last update.

RuboCopの警告をコメントで無効化する方法

Last updated at Posted at 2014-06-08

RuboCopの警告をコメントで無効化する方法

概要

RuboCopの警告をコメントで無効化する方法

詳細

基本的には .rubocop.yml の設定に応じて規約をチェックするが、
一部だけ例外的に警告対象外にしたいケースがあると思います。

例えば通常の処理は1行80文字でチェックしたいが、
長文のリテラルを定数化する場合など、どうしても80文字を超えてしまうような
場合だけ警告対象外にしたい、など。

指定範囲の全ての警告を無効化したい場合

# rubocop:disable all
[...]
# rubocop:enable all

指定範囲の特定の警告を無効化したい場合

Style/LineLength, Style/StringLiteralsの部分は無効化する警告です。

# rubocop:disable Style/LineLength, Style/StringLiterals
[...]
# rubocop:enable Style/LineLength, Style/StringLiterals

単一行の全ての警告を無効化したい場合

for x in (0..19) # rubocop:disable all

単一行の特定の警告を無効化したい場合

Style/AvoidForの部分は無効化する警告です。

for x in (0..19) # rubocop:disable Style/AvoidFor

サンプル コメント無効化前

# Hoge
class Hoge
  def A
    b=1+2
    c=2*3
    d=3/4
    "hoge"
  end
end

rubocop実行

Inspecting 1 file
W

Offenses:

before.rb:3:7: C: Use snake_case for methods.
  def A
      ^
before.rb:4:5: W: Useless assignment to variable - a.
    a=1+2
    ^
before.rb:4:6: C: Surrounding space missing for operator '='.
    a=1+2
     ^
before.rb:4:8: C: Surrounding space missing for operator '+'.
    a=1+2
       ^
before.rb:5:5: W: Useless assignment to variable - b.
    b=1-2
    ^
before.rb:5:6: C: Surrounding space missing for operator '='.
    b=1-2
     ^
before.rb:5:8: C: Surrounding space missing for operator '-'.
    b=1-2
       ^
before.rb:6:5: W: Useless assignment to variable - c.
    c=2*3
    ^
before.rb:6:6: C: Surrounding space missing for operator '='.
    c=2*3
     ^
before.rb:6:8: C: Surrounding space missing for operator '*'.
    c=2*3
       ^
before.rb:7:5: W: Useless assignment to variable - d.
    d=3/4
    ^
before.rb:7:6: C: Surrounding space missing for operator '='.
    d=3/4
     ^
before.rb:7:8: C: Surrounding space missing for operator '/'.
    d=3/4
       ^
before.rb:8:5: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
    "hoge"
    ^^^^^^

1 file inspected, 14 offenses detected

サンプル コメント無効化後

下記の設定を加えました。

  • Aメソッドの1行警告コメント無視
  • a=1+2,b=1-2 の範囲の「SpaceAroundOperators」警告のみ無効化しました
  • c=2*3,d=3/4 の範囲のすべての警告を無効化しました

※"hoge"の行は全ての警告が有効です

この設定により、1+2,b=1-2 の範囲の「SpaceAroundOperators」以外の警告と
"hoge"の行の警告のみが出力されることを確認します。

# Hoge
class Hoge
  def A # rubocop:disable MethodName
    # rubocop:disable SpaceAroundOperators
    a=1+2
    b=1-2
    # rubocop:enable SpaceAroundOperators
    # rubocop:disable all
    c=2*3
    d=3/4
    # rubocop:enable all
    "hoge"
  end
end

rubocop実行

Inspecting 1 file
W

Offenses:

after.rb:5:5: W: Useless assignment to variable - a.
    a=1+2
    ^
after.rb:6:5: W: Useless assignment to variable - b.
    b=1-2
    ^
after.rb:12:5: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
    "hoge"
    ^^^^^^

1 file inspected, 3 offenses detected
212
150
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
212
150