# Intended behavior with @discardableResult?

**URL:** <https://forums.swift.org/t/intended-behavior-with-discardableresult/20343>\
**Category:** Using Swift\
**Created:** [February 11, 2019, 2:33pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343 "2019-02-11T14:33:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenox](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jenox/32/5811_2.png) [@jenox](https://forums.swift.org/u/jenox)\
**Post date:** [February 11, 2019, 2:33pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343/1 "2019-02-11T14:33:57Z")

</div>

In the following snippet, I expect to be able to discard the return value after invoking `bar`, but the compiler even allows me to discard a reference to the _function_, which makes no sense to me.

```
@discardableResult func baz() -> Int {
    return 0
}

class Foo {
    @discardableResult func bar() -> Int {
        return 0
    }

    func test() {
        self.bar()
        self.bar // I'd expect an error here

        baz()
        baz // I do get an error here as expected
    }
}

```

This is also inconsistent with non-method functions: for the free function `baz`, the compiler generates an error:

> Expression resolves to an unused function

---

<div class="post-metadata">

**Author:** ![nuclearace](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nuclearace](https://forums.swift.org/u/nuclearace)\
**Post date:** [February 11, 2019, 2:48pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343/2 "2019-02-11T14:48:20Z")

</div>

I don't think this is intended, probably a bug.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [February 11, 2019, 7:03pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343/3 "2019-02-11T19:03:12Z")

</div>

Yes, please do file this at [https://bugs.swift.org](https://bugs.swift.org)!

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [February 11, 2019, 7:50pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343/4 "2019-02-11T19:50:24Z")

</div>

I am able to reproduce this without the `@discardableResult` attribute as well (Swift 4.2/Xcode 10.1 playgrounds)

---

<div class="post-metadata">

**Author:** ![jenox](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jenox/32/5811_2.png) [@jenox](https://forums.swift.org/u/jenox)\
**Post date:** [February 11, 2019, 8:09pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343/5 "2019-02-11T20:09:34Z")

</div>

> I am able to reproduce this without the `@discardableResult` attribute as well (Swift 4.2/Xcode 10.1 playgrounds)

That's because in a playground nothing is ever really discarded but always printed to the sidebar.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [February 11, 2019, 8:43pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343/6 "2019-02-11T20:43:42Z")

</div>

Ah ok. Seems like the `DeclRefExpr` we want (i.e. `bar`) is being wrapped in a `DotSyntaxCallExpr` node (makes sense as we're doing `self.foo`), hence why the error is missing. Here's a PR: [[Typechecker] Fix an issue with @discardableResult error not being emitted by theblixguy · Pull Request #22518 · apple/swift · GitHub](https://github.com/apple/swift/pull/22518)

---

<div class="post-metadata">

**Author:** ![jenox](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jenox/32/5811_2.png) [@jenox](https://forums.swift.org/u/jenox)\
**Post date:** [February 11, 2019, 9:21pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343/7 "2019-02-11T21:21:26Z")

</div>

Thanks for the quick fix. The error is not being emitted if I leave out the `self.` too though. Does your PR address this as well?

I would test it myself, but I never built the Swift compiler locally — I might look into that tomorrow.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [February 11, 2019, 9:25pm UTC](https://forums.swift.org/t/intended-behavior-with-discardableresult/20343/8 "2019-02-11T21:25:10Z")

</div>

Yes it does. In the other case, it gets wrapped in an _implicit_ `DotSyntaxCallExpr`.
