mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Make Lint/NotNilAfterNoBang report calls to #match
				
					
				
			This commit is contained in:
		
							parent
							
								
									0b225da9ba
								
							
						
					
					
						commit
						21a406e56d
					
				
					 2 changed files with 17 additions and 5 deletions
				
			
		|  | @ -11,6 +11,7 @@ module Ameba::Rule::Lint | ||||||
|         (1..3).index { |i| i > 2 }.not_nil!(:foo) |         (1..3).index { |i| i > 2 }.not_nil!(:foo) | ||||||
|         (1..3).rindex { |i| i > 2 }.not_nil!(:foo) |         (1..3).rindex { |i| i > 2 }.not_nil!(:foo) | ||||||
|         (1..3).find { |i| i > 2 }.not_nil!(:foo) |         (1..3).find { |i| i > 2 }.not_nil!(:foo) | ||||||
|  |         /(.)(.)(.)/.match("abc", &.itself).not_nil! | ||||||
|         CRYSTAL |         CRYSTAL | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  | @ -36,6 +37,17 @@ module Ameba::Rule::Lint | ||||||
|         CRYSTAL |         CRYSTAL | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  |     it "reports if there is an `match` call followed by `not_nil!`" do | ||||||
|  |       source = expect_issue subject, <<-CRYSTAL | ||||||
|  |         /(.)(.)(.)/.match("abc").not_nil![2] | ||||||
|  |                   # ^^^^^^^^^^^^^^^^^^^^^ error: Use `match! {...}` instead of `match {...}.not_nil!` | ||||||
|  |         CRYSTAL | ||||||
|  | 
 | ||||||
|  |       expect_correction source, <<-CRYSTAL | ||||||
|  |         /(.)(.)(.)/.match!("abc")[2] | ||||||
|  |         CRYSTAL | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|     it "reports if there is an `index` call with block followed by `not_nil!`" do |     it "reports if there is an `index` call with block followed by `not_nil!`" do | ||||||
|       source = expect_issue subject, <<-CRYSTAL |       source = expect_issue subject, <<-CRYSTAL | ||||||
|         (1..3).index { |i| i > 2 }.not_nil! |         (1..3).index { |i| i > 2 }.not_nil! | ||||||
|  |  | ||||||
|  | @ -1,17 +1,17 @@ | ||||||
| module Ameba::Rule::Lint | module Ameba::Rule::Lint | ||||||
|   # This rule is used to identify usage of `index/rindex/find` calls |   # This rule is used to identify usage of `index/rindex/find/match` calls | ||||||
|   # followed by a call to `not_nil!`. |   # followed by a call to `not_nil!`. | ||||||
|   # |   # | ||||||
|   # For example, this is considered a code smell: |   # For example, this is considered a code smell: | ||||||
|   # |   # | ||||||
|   # ``` |   # ``` | ||||||
|   # %w[Alice Bob].find(&.match(/^A./)).not_nil! |   # %w[Alice Bob].find(&.chars.any?(&.in?('o', 'b'))).not_nil! | ||||||
|   # ``` |   # ``` | ||||||
|   # |   # | ||||||
|   # And can be written as this: |   # And can be written as this: | ||||||
|   # |   # | ||||||
|   # ``` |   # ``` | ||||||
|   # %w[Alice Bob].find!(&.match(/^A./)) |   # %w[Alice Bob].find!(&.chars.any?(&.in?('o', 'b'))) | ||||||
|   # ``` |   # ``` | ||||||
|   # |   # | ||||||
|   # YAML configuration example: |   # YAML configuration example: | ||||||
|  | @ -24,11 +24,11 @@ module Ameba::Rule::Lint | ||||||
|     include AST::Util |     include AST::Util | ||||||
| 
 | 
 | ||||||
|     properties do |     properties do | ||||||
|       description "Identifies usage of `index/rindex/find` calls followed by `not_nil!`" |       description "Identifies usage of `index/rindex/find/match` calls followed by `not_nil!`" | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     BLOCK_CALL_NAMES = %w(index rindex find) |     BLOCK_CALL_NAMES = %w(index rindex find) | ||||||
|     CALL_NAMES       = %w(index rindex) |     CALL_NAMES       = %w(index rindex match) | ||||||
| 
 | 
 | ||||||
|     MSG = "Use `%s! {...}` instead of `%s {...}.not_nil!`" |     MSG = "Use `%s! {...}` instead of `%s {...}.not_nil!`" | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue