Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jepers opened this issue Dec 16, 2015 · 1 comment
Closed

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

jepers opened this issue Dec 16, 2015 · 1 comment
Labels
improvement standard library Area: Standard library umbrella

Comments

@jepers
Copy link

jepers commented Dec 16, 2015

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 "
}
@gribozavr
Copy link
Collaborator

@warn_unused_result is the default now.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants