Repository: Konstantin8105/c4go
Pull request #266 (2019-02-09 15:57:51)

transpileIf: empty if body

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/266?src=pr&el=h1) Report
> Merging [#266](https://codecov.io/gh/Konstantin8105/c4go/pull/266?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/e73c7362d18a57c500b281b4afd01bda718a1f16?src=pr&el=desc) will **decrease** coverage by `0.03%`.
> The diff coverage is `55.55%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/266/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/266?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #266      +/-   ##
==========================================
- Coverage      87%   86.96%   -0.04%     
==========================================
  Files         176      176              
  Lines        9673     9676       +3     
==========================================
- Hits         8416     8415       -1     
- Misses        869      871       +2     
- Partials      388      390       +2
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/266?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/branch.go](https://codecov.io/gh/Konstantin8105/c4go/pull/266/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9icmFuY2guZ28=) | `79.32% <55.55%> (-0.53%)` | :arrow_down: |
| [transpiler/scope.go](https://codecov.io/gh/Konstantin8105/c4go/pull/266/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9zY29wZS5nbw==) | `87.75% <0%> (-4.09%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/266?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/266?src=pr&el=footer). Last update [e73c736...ae3a5de](https://codecov.io/gh/Konstantin8105/c4go/pull/266?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/transpiler/branch.go b/transpiler/branch.go
index 759ac4b..90b93aa 100644
--- a/transpiler/branch.go
+++ b/transpiler/branch.go
@@ -97,16 +97,21 @@ func transpileIfStmt(n *ast.IfStmt, p *program.Program) (
 		boolCondition = util.NewNil()
 	}
 
-	body, newPre, newPost, err := transpileToBlockStmt(children[2], p)
-	if err != nil {
-		return nil, nil, nil, err
-	}
+	body := new(goast.BlockStmt)
 
-	preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
+	if children[2] != nil {
+		var newPre, newPost []goast.Stmt
+		body, newPre, newPost, err = transpileToBlockStmt(children[2], p)
+		if err != nil {
+			return nil, nil, nil, err
+		}
 
-	if body == nil {
-		return nil, nil, nil, fmt.Errorf("Body of If cannot by nil")
+		preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
+		if body == nil {
+			return nil, nil, nil, fmt.Errorf("Body of If cannot by nil")
+		}
 	}
+
 	if boolCondition == nil {
 		return nil, nil, nil, fmt.Errorf("Bool Condition in If cannot by nil")
 	}


Test patch:
diff --git a/tests/if.c b/tests/if.c
index 3fbdee1..29fed43 100644
--- a/tests/if.c
+++ b/tests/if.c
@@ -8,10 +8,17 @@ int d(int v)
 
 int main()
 {
-    plan(21);
+    plan(22);
 
     int x = 1;
 
+	// empty if body
+	if (x == 0)
+		;
+	else if (x != 0) {
+		pass("ok: empty if body");
+	}
+
     // Without else
     if (x == 1)
         pass("%s", "x is equal to one");