swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-245] Missing @warn_unused_result attributes in Standard Library

Open jepers opened this issue 10 years ago • 1 comments

Previous ID SR-245
Radar None
Original Reporter @jepers
Type Improvement
Status Closed
Resolution Done

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement
Assignee None
Priority Medium

md5: 8a2d58a566fb9e188b29e87376a9f71e

relates to:

  • SR-778 Forgetting the in keyword can sometimes lead to compiling code

Issue Description:

The Standard Library currently has almost 400 @warn_unused_result attributes and, as demonstrated here, that is not enough.
(I am a fan of the proposal to make this warning default for all non-Void returning funcs.)

I generated a list of potential missing @warn_unused result attributes by searching the Standard Library's generated interface for non void public funcs without the attribute, using something like:
t\npublic func [^\n][-][>][^\n]

From that list I wrote these two functions to show my findings / some low hanging fruit for contributors:

func asExpectedThisFuncHasWarningsOnEveryLine() {
    1.0 + 2.0
    1.0 - 2.0
    1.0 * 2.0
    1.0 / 2.0
    1.0 % 2.0
    1.0 == 2.0
    1.0 != 2.0
    1.0 < 2.0
    1.0 > 2.0
    1.0 <= 2.0
    1.0 >= 2.0
}
func butSurprisinglyThisFuncHasNoWarningsAtAll() {
    // Missing @warn_unused_result for these Int operators:
    1 + 2
    1 - 2
    1 * 2
    1 / 2
    1 % 2
    1 == 2
    1 != 2
    1 < 2
    1 > 2
    1 <= 2
    1 >= 2
    1 << 2
    1 >> 2
    1 & 2
    1 | 2
    1 ^ 2
    // And it's the same for UInt, UInt8-64 and Int8-64.
    // And also for these:
    class C : NonObjectiveCBase {}; var c = C(); var oc: C? = C()
    typealias MBP = ManagedBufferPointer<Void, Void>
    isUniquelyReferenced(&c)
    isUniquelyReferencedNonObjC(&c)
    isUniquelyReferencedNonObjC(&oc)
    MBP(unsafeBufferObject: c) == MBP(unsafeBufferObject: c)
    zip([1, 2, 3], ["one", "two", "three"])
}

EDIT:
Also, as suggested by @belkadan in the swift evolution mailing list, I'm adding the related issue of missing warning(s) for unused literals, like eg:

func perhapsConfusing() {
    let s =
    "Since this function compiles fine. One might "
    "think that string literals in Swift can be "
    "written like this. But that is not the case. "
    "Only the first string literal will be printed."
    "The rest (including eg this) will be silently "
    "ignored."
    12.34 // As will this.
    print(s) // Will print: "Since this function compiles fine. One might "
}

jepers avatar Dec 16 '15 10:12 jepers

@warn_unused_result is the default now.

gribozavr avatar Jul 01 '16 01:07 gribozavr