Skip to content

Commit

Permalink
[rubocop#1611] Add MissingElse cop
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed Feb 23, 2015
1 parent 76e8f88 commit 134b0a6
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#1641](https://github.com/bbatsov/rubocop/pull/1641): Add ruby19_no_mixed_keys style to `HashStyle` cop. ([@iainbeeston][])
* [#1604](https://github.com/bbatsov/rubocop/issues/1604): Add `IgnoreClassMethods` option to `TrivialAccessors` cop. ([@bbatsov][])
* [#1651](https://github.com/bbatsov/rubocop/issues/1651): The `Style/SpaceAroundOperators` cop now also detects extra spaces around operators. A list of operators that *may* be surrounded by multiple spaces is configurable. ([@bquorning][])
* [#1611](https://github.com/bbatsov/rubocop/issues/1611): Add new `MissingElse` cop. Default is to have this cop be disabled. ([@rrosenblum][])

## 0.29.1 (13/02/2015)

Expand Down
9 changes: 9 additions & 0 deletions config/disabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ Style/MethodCalledOnDoEndBlock:
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#single-line-blocks'
Enabled: false

Style/MissingElse:
Description: >-
Require if statements to have an else condition.
If enabled, it is recommended that
Style/UnlessElse and Style/EmptyElse be enabled.
This will conflict with Style/EmptyElse if
Style/EmptyElse is configured to both
Enabled: false

Style/SymbolArray:
Description: 'Use %i or %I for arrays of symbols.'
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#percent-i'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
require 'rubocop/cop/style/each_with_object'
require 'rubocop/cop/style/else_alignment'
require 'rubocop/cop/style/empty_else'
require 'rubocop/cop/style/missing_else'
require 'rubocop/cop/style/empty_line_between_defs'
require 'rubocop/cop/style/empty_lines'
require 'rubocop/cop/style/empty_lines_around_access_modifier'
Expand Down
45 changes: 45 additions & 0 deletions lib/rubocop/cop/style/missing_else.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# encoding: utf-8

module RuboCop
module Cop
module Style
# Checks for `if` statements that do not have an `else` statement.
class MissingElse < Cop
include OnNormalIfUnless

MSG = '`if` condition requires an `else`-clause.'
MSG_NIL = '`if` condition requires an `else`-clause with `nil` in it.'
MSG_EMPTY = '`if` condition requires an empty `else`-clause.'

def on_normal_if_unless(node)
unless_else_cop = config.for_cop('Style/UnlessElse')
unless_else_enabled = unless_else_cop['Enabled'] if unless_else_cop
return if unless_else_enabled &&
node.loc.keyword &&
node.loc.keyword.is?('unless')
check(node, if_else_clause(node))
end

def on_case(node)
check(node, case_else_clause(node))
end

private

def check(node, _else_clause)
return if node.loc.else
empty_else = config.for_cop('Style/EmptyElse')
if empty_else && empty_else['Enabled']
case empty_else['EnforcedStyle']
when 'empty'
add_offense(node, node.location, MSG_NIL)
when 'nil'
add_offense(node, node.location, MSG_EMPTY)
end
end
add_offense(node, node.location, MSG)
end
end
end
end
end
Loading

0 comments on commit 134b0a6

Please sign in to comment.