mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Merge pull request #297 from crystal-ameba/Sija/fix-issue-295
Fix the edge case re: free var comparison
This commit is contained in:
		
						commit
						df4b49798e
					
				
					 2 changed files with 30 additions and 1 deletions
				
			
		|  | @ -41,13 +41,26 @@ module Ameba::Rule::Lint | |||
|         CRYSTAL | ||||
|     end | ||||
| 
 | ||||
|     it "reports if there is a static path comparison evaluating to false" do | ||||
|       expect_issue subject, <<-CRYSTAL | ||||
|         String == Nil | ||||
|         # ^^^^^^^^^^^ error: Comparison always evaluates to false | ||||
|         CRYSTAL | ||||
|     end | ||||
| 
 | ||||
|     context "macro" do | ||||
|       it "reports in macro scope" do | ||||
|       pending "reports in macro scope" do | ||||
|         expect_issue subject, <<-CRYSTAL | ||||
|           {{ "foo" == "foo" }} | ||||
|            # ^^^^^^^^^^^^^^ error: Comparison always evaluates to true | ||||
|           CRYSTAL | ||||
|       end | ||||
| 
 | ||||
|       it "passes for free variables comparisons in macro scope" do | ||||
|         expect_no_issues subject, <<-CRYSTAL | ||||
|           {{ T == Nil }} | ||||
|           CRYSTAL | ||||
|       end | ||||
|     end | ||||
| 
 | ||||
|     it "reports rule, pos and message" do | ||||
|  |  | |||
|  | @ -52,6 +52,22 @@ module Ameba::Rule::Lint | |||
|     LITERAL_TYPES = | ||||
|       PRIMITIVE_LITERAL_TYPES + DYNAMIC_LITERAL_TYPES | ||||
| 
 | ||||
|     # Edge-case: `{{ T == Nil }}` | ||||
|     # | ||||
|     # Current implementation just skips all macro contexts, | ||||
|     # regardless of the free variable being present. | ||||
|     # | ||||
|     # Ideally we should only check whether either of the sides | ||||
|     # is a free var | ||||
|     def test(source) | ||||
|       AST::NodeVisitor.new self, source, skip: [ | ||||
|         Crystal::Macro, | ||||
|         Crystal::MacroExpression, | ||||
|         Crystal::MacroIf, | ||||
|         Crystal::MacroFor, | ||||
|       ] | ||||
|     end | ||||
| 
 | ||||
|     def test(source, node : Crystal::Call) | ||||
|       return unless node.name.in?(OP_NAMES) | ||||
|       return unless (obj = node.obj) && (arg = node.args.first?) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue