-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
{Assert,Refute}Match
OnlyRegexpLiteral
config
`AssertMatch` & `RefuteMatch` enforce that assert matcher.match?(object) refute matcher.match?(object) be replaced with assert_match(matcher, object) refute_match(matcher, object) respectively, which use `=~` under the hood. This makes the cops highly succeptible to false positives, in cases where the matcher object responds to `match?`, but not to `=~` class SuitColorMatcher BLACK = [:clubs, :spades] RED = [:diamonds, :hearts] SUITS = BLACK + RED def initialize(suit) raise ArgumentError, suit.inspect unless SUITS.include?(suit) @suit = suit end def match?(other) case @suit when *BLACK then BLACK.include?(other) when *RED then RED .include?(other) end end end SuitColorMatcher.new(:clubs).match?(:spades) # => true SuitColorMatcher.new(:clubs) =~ :spades # NoMethodError or cases where the matcher object does also respond to `=~`, but in an incompatible way "".match? "" # => true "" =~ "" # TypeError Given RuboCop doesn't have runtime type information, the only case we can reliably identify is the case where a `Regexp` literal is used. Therefore, this adds an `OnlyRegexpLiteral` config (false by default), which consumers can enable if their codebase has many false positives.
- Loading branch information
1 parent
f2fe8bd
commit 909b1e9
Showing
7 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#323](https://github.com/rubocop/rubocop-minitest/pull/323): Add `{Assert,Refute}Match` `OnlyRegexpLiteral` config. ([@sambostock][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters