Skip to content

@discardableResult fix-it is wrong when placed on variable declaration #65705

@dawilliams-gpsw

Description

@dawilliams-gpsw

Description
When placing @discardableResult on a variable declaration, the suggested fix it results in another fix-it, because @discardableResult doesnt seem to work on property declarations:

struct Foo {
    // suggested fix-it: Attribute can only be applied to declarations, not types - Replace 'var foo: @discardableResult ' with '@discardableResult var foo: '
    var foo: @discardableResult () -> Int
}

Using the fix-it results in the following, which requires another fix-it:

struct Foo {
    // suggested fix-it: '@discardableResult' attribute cannot be applied to this declaration - Remove '@discardableResult '
    @discardableResult var foo: () -> Int
}

which finally results in code that compiles, albeit without the result that the developer desires:

struct Foo {
    var foo: () -> Int
}

Steps to reproduce
Place @discardableResult on variable declaration's type.

Expected behavior
Fix it to completely remove @discardableResult, until @discardableResult is supported on property decls.

Screenshots
Screenshot 2023-05-05 at 10 36 57 AM
Screenshot 2023-05-05 at 10 37 33 AM

Environment

  • Swift compiler version info 5.9
  • Xcode version info 14.3
  • Deployment target: any

Metadata

Metadata

Assignees

No one assigned

    Labels

    @discardableResultFeature → attributes: The @discardableResult attributeattributesFeature: Declaration and type attributesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementationfix-itsFeature: diagnostic fix-itsparserArea → compiler: The legacy C++ parserswift 5.9

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions