swift:S2201 implicit return not detected

We’re using Sonarcloud. With our latest analysis sonar reports a false-positive with implicit returns:

swift:S2201: Return values from functions without side effects should not be ignored

In our project we enforce implicit returns, that is when a method or computed property is only one line and has a return value that value is implicitly returned without the need of the return keyword

var canonicalID: String {
    id.uppercased()
}

Here, id.uppercased() is implicitly returned, because the property has a return value of type String and the implementation is a one-liner that produces a value of type String.
This can be found all over swift and would probably produce hundreds of warnings, but it seems this is the only occurrence in our code base where we use one of the methods that trigger this rule as mentioned in the rule definition.

Hey @rd-david-wahlandt

Implicit returns should be handled well by our analysis engine ([SONARSWIFT-429] - Jira)

…and I can reproduce this. Given the following code:

let id = "test";

var canonicalID: String {
    id.uppercased()
}

print(canonicalID)

The value of canonicalID is TEST.

I’ll flag this for the appropriate team. For now – it’s best you mark these as false-positives.

Hey colin, thanks for the response. The Jira ticket only mentions one-line functions. may be computed properties are not handled the same way? Thank you for looking into it. I’ve marked it as a false positive.

Hi,

Thanks for bringing this to our attention. Indeed you are right, computed properties are not handled like functions. I have thus created a ticket to improve this: SONARSWIFT-528

Regards,
François

1 Like

@francois.mora
Glad i can help :slight_smile: Keep in mind that computed properties may also have multiple ways of defining them, if that is important. the get and set can also be marked as get async or get async throws :

// getter only
var canonicalID: String {
    id.uppercased()
}

// explicit getter only
var canonicalID: String {
    get {
        id.uppercased()
    }
}

// getter and setter
va canonicalID: String {
    get {
        id.uppercased()
    }
    set {
        id = newValue
    }
}
1 Like