# Painless empty block?

**URL:** <https://discuss.elastic.co/t/painless-empty-block/155213>\
**Category:** Elasticsearch\
**Created:** [November 2, 2018, 5:17pm UTC](https://discuss.elastic.co/t/painless-empty-block/155213 "2018-11-02T17:17:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![m9aertner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m9aertner/32/39588_2.png) [@m9aertner](https://discuss.elastic.co/u/m9aertner)\
**Post date:** [November 2, 2018, 5:17pm UTC](https://discuss.elastic.co/t/painless-empty-block/155213/1 "2018-11-02T17:17:31Z")

</div>

I find it odd that Painless appears to not allow brackets-only empty blocks, e.g. for if statements (ES 5.3).

The following works OK:

```
          "script" : {
             "script" : {
                "inline": "if(doc != null){ /* comment only */ true}",
                "lang": "painless"
             }
          }

```

while removing the "true" causes the snippet to **fail** with "Extraneous if statement."

```
    "script_stack": [
      "if(doc != null){ /* comme ...",
      "^---- HERE"
    ],
    "script": "if(doc != null){ /* comment only */ }",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Extraneous if statement."
    }
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [November 6, 2018, 9:03am UTC](https://discuss.elastic.co/t/painless-empty-block/155213/2 "2018-11-06T09:03:39Z")

</div>

have you tried just `return doc != null` to simplify this piece of code?

---

<div class="post-metadata">

**Author:** ![m9aertner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m9aertner/32/39588_2.png) [@m9aertner](https://discuss.elastic.co/u/m9aertner)\
**Post date:** [November 6, 2018, 10:29am UTC](https://discuss.elastic.co/t/painless-empty-block/155213/3 "2018-11-06T10:29:19Z")

</div>

We're _generating_ painless scripts from higher-level condition representations. One obvious workaround there is to prevent _the generator_ from outputting the condition in the first place, when the block would be empty. Or, add a no-op like "true", as above. Or, like you say, re-write to remove the whole "if".

Certainly, this is not a bug. I think it's just _odd_. Don't have practically all bracket-style languages a definition for "block-statement" like "block-statement := { statement-list }" with "statement-list := empty, or a statement-list followed by a statement".

Well, Painless apparently does not. Fair enough, NP. Of course, the purpose of this note is to help anyone wondering about the error message "Extraneous if statement" (like [Painless is a pain in my head](https://discuss.elastic.co/t/painless-is-a-pain-in-my-head/85612) and [https://stackoverflow.com/q/50211148](https://stackoverflow.com/q/50211148))

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 4, 2018, 10:29am UTC](https://discuss.elastic.co/t/painless-empty-block/155213/4 "2018-12-04T10:29:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
